0

So what I'm doing is taking screenshots of screen, getting the pixel RGB value, and putting it in another image. The code can be found here or below. Everything works fine, except it's slow, and makes the system a little slow too (when sleep time is 0).

import time

import Image
import ImageGrab

X = 0 # seconds to pause before taking next pixel
WIDTH = 300 # the width of the image drawn, higher number = slower
HEIGHT = 300 # the height of the image drawn, higher number = slower

if __name__ == '__main__':

    #start = time.time()

    pixels_img = Image.new('RGB', (WIDTH, HEIGHT), 'black') # create a new Image instance
    pixels = pixels_img.load()

    for w in xrange(WIDTH):
        for h in xrange(HEIGHT):
            img = ImageGrab.grab() # take a screenshot
            img = img.resize((WIDTH, HEIGHT)) # create a thumbnail
            pixels[w, h] = img.getpixel((w, h)) # pixels(x, y) = (r, g, b)
            time.sleep(X)

    pixels_img.save('pixel.png', 'PNG')
    pixels_img.show()
    #print time.time() - start

I tried with image size 100 px, and it took about 19 minutes. This is a lot considering I'd need a much bigger image to get a better and usable script.

How do I:

  • Make the script much, much faster?
  • Make it use less system resources?
KGo
  • 18,536
  • 11
  • 31
  • 47
  • 1
    What are you trying to do? AFAIK you *cannot* take thousands of screenshots in an efficient way. Period. You must find an alternative way to achieve what you want, and we cannot help you with this without more context. – Bakuriu Aug 04 '13 at 15:44
  • 1
    Why are you grabbing a screenshot once for every pixel in the image? That seems really inefficient- shouldn't you grab once, outside of your nested for loop? – mdscruggs Aug 04 '13 at 15:53
  • What I'm doing is getting 1 RGB value per screenshot. All I want to do is map out those in a new image. Is there another module to achieve the same thing efficiently? – KGo Aug 04 '13 at 15:57
  • 2
    Like @Bakuriu says: this is probably one of those times when you could get a much better answer if you tried to explain what problem you are trying to solve and what the end result is supposed to be. – Mattias Nilsson Aug 04 '13 at 16:27
  • 1
    No problem. Just a concept I want to try. So what I want to do is get 1 pixel from 1 screen(shot), and do this for as many time as user wants. And then create a new image with all those pixels placed at respective x, y positions. Does that make sense? – KGo Aug 04 '13 at 16:35
  • 1
    The module probably isn't the issue- the design itself is inefficient. Taking one screenshot of possibly millions of pixels, resizing it, and then getting just 1 pixel has huge overhead for almost no output. – mdscruggs Aug 04 '13 at 16:37
  • 2
    Profile your code and determine where it's spending most of its time. – martineau Aug 04 '13 at 17:56
  • 4
    Rather than editing your question with "EDIT: nvm, I changed my algorithm, and things work just fine now", please could you post your solution as an answer? It might help others who stumble across it. Thanks! – Hugo May 11 '14 at 20:39
  • As you don't want to share your solution, I've voted to close as "... was resolved in a manner unlikely to help future readers". – Jongware Jan 16 '18 at 09:58

0 Answers0