Skip to content

Instantly share code, notes, and snippets.

@blech
Last active September 14, 2015 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blech/4742c702f86e548ad68a to your computer and use it in GitHub Desktop.
Save blech/4742c702f86e548ad68a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import os
import sys
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument('--reverse',
action='store_true',
help='Assemble slides with the oldest on the right' )
parser.add_argument('--slices',
action='store',
type=int,
help='Number of slices' )
parser.add_argument('--width',
action='store',
type=int,
help='Width of slices' )
parser.add_argument('--max',
action='store',
help='Filename to stop after' )
parser.add_argument('directory')
args = parser.parse_args()
if not (args.slices or args.width):
print "Either slices or width argument is required"
sys.exit()
dir = args.directory
# TODO arguments
offset = 0
images = os.listdir(dir)
images = [image for image in images if image.lower().endswith('.jpg')]
if max:
remove = False
for image in list(images):
if image == args.max:
remove = True
if remove:
images.remove(image)
# TODO generate new blank image?
composite = Image.open(os.path.join(dir, images[-1]))
(width, height) = composite.size
x = 0
if args.slices:
slice_count = args.slices
slice_width = width/args.slices
if args.width:
slice_count = width/args.width
slice_width = args.width
image_step = len(images)/(slice_count-1)
if args.reverse:
slice_width = -slice_width
print "Got width %s split into %s slices width %s; image_step %s for %s images" % (
width, slice_count, slice_width, image_step, len(images))
for x in range(0, slice_count):
file = os.path.join(dir, images[(x*image_step)+offset])
print "%s/%s (%s)" % (x+1, slice_count, file)
im = Image.open(file)
if not args.reverse:
coords = (x*slice_width, 0, (x*slice_width)+slice_width, height)
if args.reverse:
coords = (width+slice_width+(x*slice_width), 0, width+(x*slice_width), height)
# print " Pasting slice to (%s)" % ", ".join([str(c) for c in coords])
slice = im.crop(coords)
composite.paste(slice, coords)
filename = os.path.join(dir, '..', "timeslice-%s.jpg" % slice_count)
composite.save(filename)
print "Saved %s" % filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment