Python implements batch modification of image size

In deep learning tasks, since the number of image datasets is very large, if you want to uniformly modify the size of all images, it is best to use code for batch processing

code show as below:

from PIL import Image
import os

file_path = r"D:\SAR数据集\origin"    # 原始图像路径
save_path = r"D:\SAR数据集\new"  # 修改后图像存储的路径

if not os.path.exists(save_path):           # 如果没有这个文件夹,就新建
    os.makedirs(save_path)

for root, dirs, files in os.walk(file_path):
    for file in files:                      # 展现各文件
        picture_path = os.path.join(root, file)    # 得到图像的绝对路径
        pic_org = Image.open(picture_path)               # 打开图像
        pic_new = pic_org.resize((256, 256), Image.ANTIALIAS)   # 图像尺寸修改
        pic_new_path = os.path.join(save_path, file)  # 新图像存储绝对路径
        pic_new.save(pic_new_path)  # 存储文件
        print("%s 已裁切完成!" %pic_new_path)

Among them, the " Image.ANTIALIAS " parameter means anti-aliasing, which can get higher quality images

Guess you like

Origin blog.csdn.net/qq_42691298/article/details/129547342