Created
October 5, 2013 13:59
-
-
Save saulshanabrook/6841266 to your computer and use it in GitHub Desktop.
Create a range of JPEG image sizes and save them to the disk. I wrote this to test my image uploading and processing capabilities for https://github.com/saulshanabrook/django-canadanewyork
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 os | |
from PIL import Image | |
IMAGE_SIZES = range(500, 20000, 2000) | |
JPEG_SAVE_QUALITY = 100 | |
DIRECTORY = '~/Desktop/temp/' | |
COLOR = 'red' | |
def save_pil_image(size, color=COLOR, directory=DIRECTORY, quality=JPEG_SAVE_QUALITY): | |
directory = os.path.expanduser(directory) | |
file_name = str(size) + '.jpg' | |
path = os.path.join(directory, file_name) | |
make_directory_if_not_exists(directory) | |
image = Image.new('RGB', (size, size,), color) | |
with open(path, 'w+') as file: | |
image.save(path, format='JPEG', quality=quality) | |
print 'saved ' + path | |
def make_directory_if_not_exists(directory): | |
try: | |
os.makedirs(directory) | |
except OSError: | |
if not os.path.isdir(directory): | |
raise | |
for size in IMAGE_SIZES: | |
save_pil_image(size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment