How to Resize Images with Python


With this example, we can learn how to reset a picture’s size with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from PIL import Image
import os.path
import glob

def resizejpg(jpgfile, outdir, width=160, height=60):
    img = Image.open(jpgfile)

    try:
        new_img = img.resize((width,height), Image.BILINEAR)
        new_img.save(os.path.join(outdir, os.path.basename(jpgfile)))
    except Exception as e:
        print(e)

for jpgfile in glob.glob("/picture_source/*.jpg"):
    resizejpg(jpgfile,"/picture_result/*.jpg")

After running the codes above, we can see the comparison of the resized image and the original image.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments