Created
January 15, 2016 05:16
-
-
Save kogecoo/c4639c96aea8043e2c45 to your computer and use it in GitHub Desktop.
Generating staff roll movie (images) from vertically long image(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -* | |
from PIL import Image | |
import argparse | |
import os | |
def make(image_names, target_height, seconds, imgs_per_seconds, outfile_prefix, outdir): | |
try: | |
if not os.path.exists(outdir): | |
os.makedirs(outdir) | |
images = [Image.open(image_name) for image_name in image_names] | |
width, height = images[0].size | |
remain = height - target_height | |
step = remain / seconds / imgs_per_seconds | |
c = 1 | |
print "src width: {0}, height: {1}, remain: {2}, step: {3}".format(width, height, remain, step) | |
print "target widt: {0}, height: {1}".format(width, target_height) | |
for y in range(0, height - target_height, step): | |
print "{0}, {1}, {2}, {3}, {4}".format(0, y, width, y + target_height - 1, image_names[c % len(images)]) | |
img = images[c % len(images)] | |
cropped = img.crop((0, y, width, y + target_height - 1)) | |
cropped.save("{0}/{1}_{2}.jpg".format(outdir, outfile_prefix, c)) | |
c += 1 | |
except Exception as e: | |
print e | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='generate staff roll movie by cropping vertically long image(s)') | |
parser.add_argument('--images', metavar='IMGS', nargs='+', type=str, required=True, help='vertically long image(s)') | |
parser.add_argument('--height', metavar='SIZE', type=int, required=True, help='output screen height') | |
parser.add_argument('--sec', metavar='SEC', type=int, required=True, help='how long sec output staff roll is') | |
parser.add_argument('--images_per_sec', metavar='IMGS_PER_SEC', type=int, default=4, help='how many images shown in a sec') | |
parser.add_argument('--outfile_prefix', metavar='OUTFILE_PREFIX', type=str, default="staffroll", help='output file name is "prefix"_00xx.jpg') | |
parser.add_argument('--outdir', metavar='OUTPUT_DIRECTORY', type=str, default="staffroll", help='output directory') | |
args = parser.parse_args() | |
make(args.images, args.height, args.sec, args.image_per_sec, args.outfile_prefix, args.outdir) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment