Instantly share code, notes, and snippets.

What would you like to do?
from datetime import datetime
from os import path
import sys
import shutil
from PIL import Image
__author__ = 'Darien Hager'
class SteamScreenImport:
Helps import existing image files into a Steam screenshots directory,
choosing timestamp-appropriate names, saving as JPEG, saving a thumbnail,
and copying over time-based metadata.
MAX_SEQ = 99999
NAME_FORMAT = "%(year)04d%(month)02d%(day)02d%(hour)02d%(min)02d%(sec)02d_%(seq)d.jpg"
THUMBNAIL_FOLDER = 'thumbnails'
def __init__(self, destFolder):
destFolder (string) : Path to destination folder.
Takes a destination folder for the Steam screenshots folder, e.g.
C:\Program Files\Steam\userdata\111111\222\remote\333\screenshots\
self.destFolder = destFolder
self.thumbFolder = path.join(targetDir, SteamScreenImport.THUMBNAIL_FOLDER)
if not path.isdir(self.destFolder):
raise Exception("Destination folder does not exist: %s" % destFolder)
if not path.isdir(self.thumbFolder):
raise Exception("Destination has no thumbnail sub-folder: %s" % destFolder)
def transfer(self, srcPath, dateTime=None):
Imports the given file to the destination folder. If no datetime is
provided, it is inferred from the source-file's modification time.
srcPath (string): Path to file
dateTime (datetime.datetime, optional): Date to use when setting the name of the file.
If no date-time is provided, uses the file's last-modified time.
if dateTime is None:
dateTime = datetime.fromtimestamp(path.getmtime(srcPath))
destPath, thumbPath = self._pickFileName(dateTime)
self._copyWithThumbnail(srcPath, destPath, thumbPath)
return destPath
def _pickFileName(self, dt, minSeq=1):
dt (datetime.datetime)
minSeq (int)
Attempts to determine a valid (unused) filename inside the target
folder, based on a timestamp and sequence-number.
if minSeq < 1:
raise Exception("Sequence numbers must be positive")
seq = max(1, minSeq)
destPath, thumbPath = self._generatePaths(dt, seq)
while path.isfile(destPath) or path.isfile(thumbPath):
if seq > SteamScreenImport.MAX_SEQ:
raise IOError("Cannot generate filename with sequence %d" % seq)
seq += 1
destPath, thumbPath = self._generatePaths(dt, seq)
return destPath, thumbPath
def _generatePaths(self, dt, seq):
dt (datetime.datetime)
seq (int)
fileName = SteamScreenImport.GenerateFileName(dt, seq)
destPath = path.join(self.destFolder, fileName)
thumbPath = path.join(self.thumbFolder, fileName)
return destPath, thumbPath
def GenerateFileName(dt, seq):
dt (datetime.datetime)
seq (int)
Generates a candidate file name based on a datetime and sequence number.
return SteamScreenImport.NAME_FORMAT % {"year": dt.year,
"month": dt.month,
"hour": dt.hour,
"min": dt.minute,
"sec": dt.second,
"seq": seq
def _copyWithThumbnail(self, srcPath, destPath, thumbPath):
srcPath (string)
destpPath (string)
thumbPath (string)
assert path.isfile(srcPath)
assert not path.isfile(destPath)
assert not path.isfile(thumbPath)
im =, "JPEG") # Converting, if not already Jpeg
shutil.copystat(srcPath, destPath) # duplicate create/mod metadata
# Reuse object to make a thumbnail and save again
size = (SteamScreenImport.THUMBNAIL_WIDTH,
im.thumbnail(size, Image.ANTIALIAS), "JPEG")
shutil.copystat(srcPath, thumbPath)
if __name__ == "__main__":
script = sys.argv.pop(0)
targetDir = path.dirname(path.realpath(script))
print "Destination dir: %s" % targetDir
if len(sys.argv) == 0:
print "No files given to process"
app = SteamScreenImport(targetDir)
for srcPath in sys.argv:
print "Copying %s " % srcPath,
destPath = app.transfer(srcPath)
print "-> %s" % destPath
raw_input("Press enter to continue...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment