Sep 14
Picture database recursive copier/resizer
That a little script I did yesterday because I needed to copy my 35gig picture gallery in a new directory then resize all the pictures.
Of course, this MUST not be done “à la mano”! So i did a little python script. : ) Here it is, hope you find it of any interest.
#!/usr/bin/env python
#
# Dependencies:
# - ImageMagick (for Ubuntu user, sudo apt-get install imagemagick :) )
# - EXIF.py (http://sourceforge.net/projects/exif-py/)
from sys import argv, stderr, stdout, exit
import os
import EXIF
# removes the slash at end of path if entered
def test_slash(entry):
if entry[len(entry) - 1] == ‘/’ : return entry[0:-1]
else: return entry
# usage function
def usage():
if len(argv) != 3:
print “Usage: ./amarino.py /path/to/source/ /path/to/target/”
exit(0)
# EXIF info extracting
def read_exif(picture):
p = open(source_image, “rb”)
tags = EXIF.process_file(p)
orientation_info = {’Horizontal (normal)’:”,
‘Mirrored horizontal’:’ -rotate ??’,
‘Rotated 180′:’ -rotate 180′,
‘Mirrored vertical’:’ -rotate ??’,
‘Mirrored horizontal then rotated 90 CCW’:’ -rotate ??’,
‘Rotated 90 CW’:’ -rotate 90′,
‘Mirrored horizontal then rotated 90 CW’:’ -rotate ??’,
‘Rotated 90 CCW’:’ -rotate 270′}
return orientation_info[str(tags['Image Orientation'])]
##########
## main ##
##########
if __name__ == “__main__” :
usage()
source = argv[1]
target = argv[2]
source = test_slash(source)
target = test_slash(target)
content = [ ['PATH', 'CONTENT'] ]
stderr = stdout
for root, dirs, files in os.walk(source):
content.append([root, files])
for array in content[1:]:
folder = array[0].replace(source, ”)
if os.path.isdir(target + folder) == False:
os.makedirs(target + folder)
print “creating & processing folder”, target+folder
else : print “folder %s already exists..” % folder
for item in array[1]:
if item.split(’.').pop().upper() in ['JPEG', 'JPG', 'BMP', 'GIF']: # add extension needed
source_image = os.path.join(source + folder, item)
target_image = os.path.join(target + folder, item)
tags = ”
try:
rotate = read_exif(source_image)
except:
print “Error occured when extractinf EXIF info. Continuing..”
rotate = ”
output = os.popen(’convert -resize “640×480″ -quality 60 %s %s %s’ % (rotate, source_image, target_image))
print “resizing”, item
print “#######################”
