Python - use cv2.resize to change image size (including code)

Many times we need to process images into images of a specific size, such as changing an image of 800*800 to 512*512. Using python's cv2 module, we can quickly complete this process in batches.

The function of the following code is to resize all the pictures in the old folder to 512*512 and save them in the new folder. Go directly to the code:


import os
import cv2

olddir = 'C:/Users/Admin/Desktop/jay/'
newdir = 'C:/Users/Admin/Desktop/jay512/'
os.makedirs(newdir,exist_ok=True)
filelist=os.listdir(olddir)
count=0
for file in filelist:
    try:
        count += 1
        if count % 100==0:
            print(count)

        filename=olddir+file

        img = cv2.imread(filename)
        target_img = cv2.resize(img, (512,512))
        newpath=newdir+file
        cv2.imwrite(newpath, target_img)
    except:
        pass




Guess you like

Origin blog.csdn.net/wenqiwenqi123/article/details/128836487