1

Right now I'm in need of a really fast screenshotter to feed screenshot into a CNN that will update mouse movement based on the screenshot. I'm looking to model the same kind of behavior presented in this paper, and similarly do the steps featured in Figure 6 (without the polar conversion). As a result of needing really fast input, I've searched around a bit and have been able to get this script from here slightly modified that outputs 10fps

from PIL import ImageGrab
from datetime import datetime

while True:
    im = ImageGrab.grab([320, 180, 1600, 900])
    dt = datetime.now()
    fname = "pic_{}.{}.png".format(dt.strftime("%H%M_%S"), dt.microsecond // 100000)
    im.save(fname, 'png') 

Can I expect anything faster? I'd be fine with using a different program if it's available.

  • Do you need to take a full screenshot? There is probably a way to just capture the mouse coordinates directly if that's all you need. – dimo414 Jul 04 '17 at 04:59
  • Ah thanks :P couldn't spot it. The cursor's movement is going to be moved based on the other elements on the screenshot, much like the player in the paper changes their position based on the walls in the game. – Dinoswarleafs Jul 04 '17 at 05:02
  • Are you using an SSD? – cup Jul 04 '17 at 05:17
  • No, I'm not sure why I didn't think about a disk being a bottleneck ... DUH ._. I'm very dumb. In my case I just need to learn it into my script for just brief use in a CNN so I don't need to save it to my disk which I already know how to do. Eeeee I'm embarrassed for asking this now :P – Dinoswarleafs Jul 04 '17 at 05:19

1 Answers1

3

Writing to disk is very slow, and is probably a big part of what's making your loop take so long. Try commenting out the im.save() line and seeing how many screenshots can be captured (add a counter variable or something similar to count how many screenshots are being captured).

Assuming the disk I/O is the bottleneck, you'll want to split these two tasks up. Have one loop that just captures the screenshots and stores them in memory (e.g. to a dictionary with the timestamp as the key), then in a separate thread pull elements out of the dictionary and write them to disk.

See this question for pointers on threading in Python if you haven't done much of that before.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Oh you're right, the serious bottleneck in my disk. In my case, I actually don't need to save the files to a disk, I just need to read them in tensorflow. I guess I could just use image grab directly in a tf script for use, which seems to be getting around 20-30 fps which is all I need :) I'm not sure why I didn't think about this, but now it all seems obvious. Thanks a lot for your answer! It probably just saved me tons of time – Dinoswarleafs Jul 04 '17 at 05:17
  • Glad I could help :) – dimo414 Jul 04 '17 at 05:24