Created
March 25, 2014 18:20
-
-
Save legenderrys/9767919 to your computer and use it in GitHub Desktop.
Generate thumbnails from files in a directory using Python PILkit Library
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
import logging | |
import os | |
import pilkit.processors | |
import sys | |
import getopt | |
from PIL import Image | |
from PIL import ImageDraw, ImageOps | |
from pilkit.processors import Transpose, Adjust | |
from pilkit.utils import save_image | |
# dictionary list of sizes to be outputed | |
sizes = {'small': (100,100), 'med': (400,400), 'large': (600,400)} | |
def makeFolders(): | |
"create separate folders for images to be placed based on dictionary size keys" | |
for size in sizes: | |
if not os.path.exists(size): | |
# make the path | |
os.makedirs(size) | |
makeFolders() | |
# foldername is the name of folder or path to folder you want to process images | |
for root, dirs, files in os.walk('clients'): | |
for file in files: | |
# get all files with jpg extensions for processing only | |
if file.endswith('.jpg'): | |
#iterate the dictionary and grab keys and value pairs | |
for size, dimensions in sizes.items(): | |
img = Image.open(os.path.join(root,file)) | |
img.thumbnail(dimensions) | |
# save each image into separate folders according to dimensions in dictionary | |
name = '{0}x{1}_resized_{2}'.format(dimensions[0], | |
dimensions[1], file) | |
img.save(os.path.join(size, name)) | |
print (os.path.join(size, name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment