Created
August 2, 2015 04:52
-
-
Save lionelyoung/9466514937404eba057d to your computer and use it in GitHub Desktop.
Generate thumbnails
This file contains hidden or 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 | |
# With the provided src and dest locations, will generate all the remaining | |
# thumbnails in the desired size. Use --force to regenerate | |
import argparse | |
from PIL import Image | |
import glob, os | |
def debug_display_files(files, debug_level=1): | |
files = list(files) | |
if args.debug >= 1: | |
print('Display files') | |
if args.debug >= 2: | |
print('\n'.join(files[0:10])) | |
print("... %d total ..." % len(files)) | |
elif args.debug >= 3: | |
print('\n'.join(files)) | |
print("===") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Generate square thumbnails for folder') | |
parser.add_argument('--src', dest='src', help='Location of large images') | |
parser.add_argument('--dest', dest='dest', help='Location of thumbnails') | |
parser.add_argument('--size', dest='size', default="small", help='small 150x150, medium 300x300') | |
parser.add_argument('--force', dest='force', action="store_true", help='Regenerate thumbnails for all files, even if it exists') | |
parser.add_argument('--debug', dest="debug", type=int, default=1, help='Show debug level messages') | |
args = parser.parse_args() | |
if args.debug: | |
print("===") | |
print("Large: %s" % args.src) | |
print("Thumbs: %s" % args.dest) | |
print("===") | |
# Select size | |
if args.size == "small": | |
size = 150, 150 | |
elif args.size == "medium": | |
size = 300, 300 | |
else: | |
size = 150, 150 | |
if args.debug >= 1: | |
print("Selecting size: %s" % args.size, size) | |
print("===") | |
# Identify list of large files | |
large_files = os.listdir(args.src) | |
debug_display_files(large_files, debug_level=args.debug) | |
existing_thumbs = os.listdir(args.dest) | |
debug_display_files(existing_thumbs, debug_level=args.debug) | |
# Find the list of thumbnails that have not been generated yet | |
thumbnails_to_generate = set(large_files) - set(existing_thumbs) | |
debug_display_files(thumbnails_to_generate, debug_level=args.debug) | |
if args.force: | |
thumbnails_to_generate = large_files | |
else: | |
thumbnails_to_generate = list(thumbnails_to_generate) | |
# Generate the thumbs | |
if args.debug >= 1: | |
print ("Generating %d thumbnails!" % len(thumbnails_to_generate)) | |
for f in thumbnails_to_generate: | |
#file, ext = os.path.splitext(large_file) | |
infile = os.path.join(args.src, f) | |
outfile = os.path.join(args.dest, f) | |
if args.debug >= 2: | |
print("Generating thumbnails for %s" % f) | |
print("Output %s" % outfile) | |
im = Image.open(infile) | |
im.thumbnail(size) | |
im.save(outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment