Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created October 5, 2013 13:59
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 saulshanabrook/6841266 to your computer and use it in GitHub Desktop.
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
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