0x2620-jpg/py/jpg.py

48 lines
2.1 KiB
Python

from __future__ import division
import os
import Image
import simplejson
sourcePath = "../jpg/"
json = {}
for dirname, dirs, files in os.walk(sourcePath):
for filename in files:
if len(filename) == 12 and filename[-4:] == ".JPG" and not "90" in dirname and not "720" in dirname:
split = dirname.split("/")
username = split[-2]
albumname = split[-1]
if not username in json:
json[username] = {}
if not albumname in json[username]:
json[username][albumname] = []
sourceFile = dirname + "/" + filename
sourceImage = Image.open(sourceFile)
sourceWidth = sourceImage.size[0]
sourceHeight = sourceImage.size[1]
print "reading " + sourceFile + " (" + str(sourceWidth) + "x" + str(sourceHeight) + ")"
for targetSize in [90, 720]:
targetPath = dirname + "/" + str(targetSize) + "/"
if not os.path.exists(targetPath):
os.makedirs(targetPath)
targetFile = targetPath + filename
if not os.path.exists(targetFile):
if sourceWidth > sourceHeight:
targetWidth = int(targetSize * sourceWidth / sourceHeight)
targetHeight = targetSize
else:
targetWidth = targetSize
targetHeight = int(targetSize * sourceHeight / sourceWidth)
print "writing " + targetFile + " (" + str(targetWidth) + "x" + str(targetHeight) + ")"
targetImage = Image.new("RGB", (targetWidth, targetHeight))
targetImage.paste(sourceImage.resize((targetWidth, targetHeight), Image.ANTIALIAS), (0, 0, targetWidth, targetHeight))
targetImage.save(targetFile)
json[username][albumname].append({
"file": sourceFile[3:],
"width": sourceWidth,
"height": sourceHeight
})
f = open("../json/jpg.json", "w")
f.write(simplejson.dumps(json, sort_keys=True, indent=4))
f.close()