Skip to main content

How to scale pictures in bulk with python3

First, we have to make sure we have the Pillow library installed. Open the command terminal, and type python, when we have the interpreter running, we type:-

from PIL import Image

If all goes well, we will get the out put of >>>, otherwise, we have to install pillow by type:
Pip install Pillow

Once we have Pillow readay, we could use the image.resize method to scale images.

from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir):
    img=Image.open(jpgfile)
    w,h = img.size
    if w>h: #decide the size by which is the smaller one, width or hight
        height=2013
        width = int(height/h*w)#calculation
    else:
        width =2013
        height = int(width/w*h)
    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("F:\\Original\\*.jpg"):
    convertjpg(jpgfile,"F:\\Scaling\\ResizeOne")



    




Comments