Skip to content

Instantly share code, notes, and snippets.

@looselycoupled
Last active August 29, 2015 14:12
Show Gist options
  • Save looselycoupled/39054aaf9b437adc123a to your computer and use it in GitHub Desktop.
Save looselycoupled/39054aaf9b437adc123a to your computer and use it in GitHub Desktop.
Quick script to resize a folder of images
##########################################################################
## Imports
##########################################################################
import os
from PIL import Image
##########################################################################
## Constants
##########################################################################
TARGET_SIZE = [75,75]
ORIGINALS_DIRECTORY = '[PATH HERE]/original_logos'
RESIZED_DIRECTORY = '[PATH HERE]/resized_logos'
##########################################################################
## Code
##########################################################################
def _create_square_thumbnail(image, size, fill_color=(255, 255, 255, 0)):
resized_image = image.copy()
resized_image.thumbnail(size)
background = Image.new('RGBA', size, fill_color)
background.paste(
resized_image,
((size[0] - resized_image.size[0]) / 2, (size[1] - resized_image.size[1]) / 2))
return background
def main():
for filename in os.listdir(ORIGINALS_DIRECTORY):
full_path = ORIGINALS_DIRECTORY + '/' + filename
if os.path.isfile(full_path):
print filename
im = Image.open(full_path)
resized = _create_square_thumbnail(im, TARGET_SIZE)
save_path = RESIZED_DIRECTORY + '/' + filename
resized.save(save_path, "JPEG")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment