codekasten/gallery.py

183 lines
5.8 KiB
Python
Raw Normal View History

2006-06-07 11:03:58 +02:00
#!/usr/bin/env python
# see: http://www.amk.ca/python/code/
import sys, os, getopt
try:
from PIL import Image
except ImportError:
print >>sys.stderr, ("Unable to import PIL.Image; "
2006-10-23 16:12:00 +02:00
"is the Python Imaging Library installed?"
"\(e.g.: apt-get install python-image\)")
2006-06-07 11:03:58 +02:00
sys.exit(0)
__version__ = '1.01'
__doc__ = """%s [option] file1 file2
Options:
--help Display this usage message
--html Output HTML to stdout
--resize x y Resize specified images to x,y
2007-02-22 01:18:33 +01:00
--resize-ratio x Resize specified images with correct aspect ratio
2012-07-20 00:08:01 +02:00
--resize-ratio-y y Resize specified images with correct aspect ratio
2007-02-22 01:18:33 +01:00
--rotate x Rotate the image counter clockwise for x degrees
2006-06-07 11:03:58 +02:00
--size Display the image sizes
--thumbnail x y Make thumbnails of specified images, ignoring existing
thumbnails that happen to be listed among the files.
If a filename is of the form *_thumb.*, it's assumed to be a thumbnail.
""" % sys.argv[0]
def is_thumbnail (filename):
"Returns true if the filename is for a thumbnail"
root, ext = os.path.splitext(filename)
return root.endswith('_thumb')
def thumbnail_name (filename):
"""Return the thumbnail form of a filename,
converting foo.jpg to foo_thumb.jpg.
"""
assert not is_thumbnail(filename)
root, ext = os.path.splitext(filename)
return root + '_thumb' + ext
def output_html (args):
for filename in args:
if is_thumbnail(filename): continue
thumbnail = thumbnail_name(filename)
if not os.path.exists(thumbnail):
print >>sys.stderr, ("%s: thumbnail %s doesn't exist" %
(sys.argv[0], thumbnail) )
im = Image.open(thumbnail)
width, height = im.size
print ('<p><a href="%s"><img src="%s" width="%i" height="%i" '
'alt="XXX" align="XXX"></a>'
'\n\nXXX\n'
'<br clear=all><hr>\n\n'
% (filename, thumbnail, width, height) )
def make_thumbnails (args):
x, y = int(args[0]), int(args[1])
args = args[2:] ; args.sort()
for filename in args:
if is_thumbnail(filename): continue
thumbnail = thumbnail_name(filename)
print >>sys.stderr, filename
im = Image.open(filename)
im.thumbnail((x,y))
im.save(thumbnail)
def rotate_images (args):
x = int(args[0])
args = args[1:] ; args.sort()
for filename in args:
im = Image.open(filename)
im2 = im.rotate(x)
im2.save(filename)
print "rotated %s %i degrees" % (filename, x)
def resize_images (args):
x, y = int(args[0]), int(args[1])
args = args[2:] ; args.sort()
for filename in args:
im = Image.open(filename)
w,h = im.size
im2 = im.resize((x,y))
print >>sys.stderr, ('%s: was %i,%i; resizing to %i,%i'
% (filename, w,h, x,y) )
2012-07-20 00:08:01 +02:00
root, ext = os.path.splitext(filename)
filename = ('%s_%s%s%s%s%s') % (root, 'x', w, 'y', h , ext)
2006-06-07 11:03:58 +02:00
im2.save(filename)
def resize_images_aspectratio (args):
x = int(args[0])
args = args[1:] ; args.sort()
for filename in args:
im = Image.open(filename)
w,h = im.size
y = x*h/w
im2 = im.resize((x,y))
print >>sys.stderr, ('%s: was %i,%i; resizing to %i,%i'
% (filename, w,h, x,y) )
2012-07-20 00:08:01 +02:00
root, ext = os.path.splitext(filename)
filename = ('%s_%s%s%s') % (root, 'x', x , ext)
im2.save(filename)
def resize_images_aspectratio_y (args):
y = int(args[0])
args = args[1:] ; args.sort()
for filename in args:
im = Image.open(filename)
w,h = im.size
x = y*w/h
im2 = im.resize((x,y))
print >>sys.stderr, ('%s: was %i,%i; resizing to %i,%i'
% (filename, w,h, x,y) )
root, ext = os.path.splitext(filename)
filename = ('%s_%s%s%s') % (root, 'y', y , ext)
im2.save(filename)
2006-06-07 11:03:58 +02:00
def main ():
opts, args = getopt.getopt(sys.argv[1:],
'h',
['help',
'html',
'resize',
'resize-ratio',
2012-07-20 00:08:01 +02:00
'resize-ratio-y',
2006-06-07 11:03:58 +02:00
'size',
'thumbnail',
2012-07-20 00:08:01 +02:00
'rotate'])
2006-06-07 11:03:58 +02:00
# Remove the unused option arguments
opts = [opt for opt,arg in opts]
# Print usage message if requested
if '-h' in opts or '--help' in opts:
print >>sys.stderr, __doc__
sys.exit(0)
# Ensure that exactly one option is supplied
if len(opts) == 0:
print >> sys.stderr, ("%s: must specify one of --size, --help"
"\n --html, --resize, --resize-ratio, --thumbnail --rotate" % sys.argv[0] )
2006-06-07 11:03:58 +02:00
sys.exit(0)
elif len(opts) > 1:
print >> sys.stderr, ("%s: cannot specify multiple options" %
sys.argv[0] )
sys.exit(0)
# Perform each of the possible actions
opt = opts[0]
if opt == '--html':
output_html(args)
elif opt == '--resize':
resize_images(args)
elif opt == '--resize-ratio':
resize_images_aspectratio(args)
2012-07-20 00:08:01 +02:00
elif opt == '--resize-ratio-y':
resize_images_aspectratio_y(args)
2006-06-07 11:03:58 +02:00
elif opt == '--rotate':
rotate_images(args)
elif opt == '--size':
for filename in args:
image = Image.open(filename)
x, y = image.size
print filename, ':', x,y
elif opt == '--thumbnail':
make_thumbnails(args)
else:
print >>sys.stderr, ("%s: unknown option" % sys.argv[0])
if __name__ == '__main__':
main()