How to batch change image size to uniform size in python

In image processing, we often need to first obtain the size of the data, so how to do it?

imagepython
3 votesavatar15.00
1 Answers
avatar15.00
3

There are certainly many ways to do that, Here is a way to use Python:

from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=128,height=128):
    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("E:\\img\\*.jpg"):
    convertjpg(jpgfile,"E:\\new_img")
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?