Sunday, 6 March 2011

Super Useful Screenshot Code

Don't ask me how this works, the save function isn't even on the Processing reference as far as I can tell. It must have been forged long ago by some very clever dwarves, because it even contains the power to encode in a specific image format based solely on the extension you chuck on the file name. I think Ben Jack told us about this save() function in 142, and I made my own version to create unique file names based on a timestamp. Here is the code.

Firstly, the special keyPressed() function to capture key presses (83 is 's'):
def keyPressed():
        k = keyCode
        if k == 83:
                screenshot()
Then, the screenshot function itself:
 def screenshot():
        days = str(day())
        hours = str(hour())
        mins = str(minute())
        secs = str(second())
        filename = "screen" + days + hours + mins + secs + ".png"
        save(filename)
Screenshots will show up in the same folder as the code file you are running.

No comments:

Post a Comment