Frustrated Python Coder
Frustrated Python Coder

Reputation: 1593

Python: I want to make a screenshot script that consecutively names files

So far, I have written a script to take screenshots and save them. But I want the files to be named "snap", followed by a number (e.g. snap1.jpg, snap2.jpg, snap3.jpg). The plan is to make a ew picture every time the script executes. Here is the current script:

import ImageGrab
img = ImageGrab.grab()
img.save('snap1.jpg','JPEG')

Upvotes: 2

Views: 2869

Answers (4)

Rose Kunkel
Rose Kunkel

Reputation: 3268

If you're saving to the same directory every time you run the script, then you can check the directory for files named /snap[0-9]+.jpeg/ (That is, files named snap, followed by a number, followed by ".jpg") and name your new file the next available file name. So something like this should work:

import os
import ImageGrab
import re

snapPattern = re.compile('snap([0-9]+)\.jpeg')

usedNumbers = []
fileList = os.listdir('.')
for filename in fileList:
    m = snapPattern.match(filename)
    if( m ):
        usedNumbers.append( m.group(1) )
usedNumbers.sort()
i = 0
while usedNumbers.count( str(i) ):
    i++
img = ImageGrab.grab()
img.save('snap'+str(i)+'.jpg','JPEG')

I apologize in advance if I screwed this up, I'm not extremely proficient with Python.

Upvotes: 1

chthonicdaemon
chthonicdaemon

Reputation: 19770

The generation of the filename could be simple

i = 1 # somewhere above the grabbing

filename = 'snap%i.jpg' % i
i += 1
img.save(filename, 'JPEG')

itertools provides a nice counter generator, so you can do

import itertools
filecounter = itertools.count(1)

filename = 'snap%i.jpg' % filecounter.next()

The advantage of having the counter is that you can send it around to other functions without bothering to transfer state back. You might also want to construct a function to figure out the next filename based on what files already exist, but that's a little more involved.

Upvotes: 1

Linell
Linell

Reputation: 749

Just put this into a loop, and each time you take a screenshot increment a counter.

i = 1
while (condition):
    ImageGrab.grab()
    img.save('snap'+str(i)+'.jpg','JPEG')

However, since you want to simply run the application and have it know what to name it, you could always create another file to hold the number you're on. Let's say you call it screen.txt and put nothing but a zero in this file. Now modify your code to be something like:

f = open('screen.txt')
i = int(f.read())
f.close()
print ('snap'+str(i)+'.jpg')
f = open('screen.txt', 'w')
i+=1
f.write(str(i))

You'll want to replace that print statement with your img.save statement. This should work and is pretty darn easy to understand.

Upvotes: 1

Sean Perry
Sean Perry

Reputation: 3886

import os
import sys

basename = sys.argv[1]

# for each file in the current directory, check if its name starts with basename
# if it does, split on basename this will yield ['', N] where N is the number in the filename
# call int on the number since it is currently a string
numbers = [int(f.split(basename)[1]) for f in os.listdir('.') if f.startswith(basename)]
last_number = max(numbers) # I broke this up so it was easier to see
new_name = "%s%03d.jpg" % (basename, last_number + 1)
print new_name

Notes.

  • uses '.' aka current dir as the location, update as needed.
  • the new_name is written like snap003.jpg. Again, adjust as needed. The zeroes help a file listing line up

Enjoy.

Upvotes: 4

Related Questions