Skip to content

Instantly share code, notes, and snippets.

@peramsathyam
Last active March 4, 2016 13:27
Show Gist options
  • Save peramsathyam/5835e574c524d72e989a to your computer and use it in GitHub Desktop.
Save peramsathyam/5835e574c524d72e989a to your computer and use it in GitHub Desktop.
Script that resize the .jpg images in the folder and saves

Install Python Image Library(PIL)

We are installing Pillow forked from PIL (as it is compatible with setuptools)

  • pip install pillow
  • Navigate to any folder contains images of type JPEG
  • Download by Right-click and save as resize_image.py

OR

You can create new python file and paste the code

from PIL import Image
import glob, os

size = 250, 250 # 250 by 250 square sized
directory = 'thumbnails'

for infile in glob.glob("*.jpg"):
    print infile

    if not os.path.exists(directory):
        os.makedirs(directory)
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.thumbnail(size)
    im.save(directory + '/' + file + "-thumbnail.jpg", "JPEG")
    print "Your thumbnails are saved inside thumbnails folder"
  • Naviage to the location where resize_image.py lives in terminal and run
  • python resize_image.py

Navigate to the thumnails folder and check the resized images

from PIL import Image
import glob, os
size = 250, 250 # 250 by 250 square sized
directory = 'thumbnails'
for infile in glob.glob("*.jpg"):
print infile
if not os.path.exists(directory):
os.makedirs(directory)
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.thumbnail(size)
im.save(directory + '/' + file + "-thumbnail.jpg", "JPEG")
print "Your thumbnails are saved inside thumbnails folder"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment