python batch change picture size

foreword

  We often need to change the size of pictures in batches. Use the following code to modify them in batches as needed. You only need to replace the file path and the size you want to modify.
  The use of resize to change the size here is to use the resize method in the Image class in the pillow package. The resize method can convert the size of the original image, size is the size after conversion, and resample is the method used for resampling, including Image.BICUBIC, PIL.Image.LANCZOS, PIL.Image.BILINEAR, PIL.Image.NEAREST, etc. Sampling method, the default is PIL.Image.NEAREST, box is the specified image area to be resized.
  In addition, you can also use the resize method in opencv, use the resize method in the transform class in the skimage package, and use matplotlib to scale the picture. You can refer to python picture resize() method summary

1. The code is as follows (example):

from PIL import Image
import os

# 原始文件夹路径
original_folder = '/path/to/original/folder'
# 保存的新文件夹路径
new_folder = '/path/to/new/folder'

# 遍历原始文件夹中的图像
for filename in os.listdir(original_folder):
    img = Image.open(os.path.join(original_folder, filename))
    # 改变尺寸
    img_resized = img.resize((684, 348))   #这里是你要转换的尺寸
    # 保存到新文件夹
    img_resized.save(os.path.join(new_folder, filename))

Guess you like

Origin blog.csdn.net/weixin_43788282/article/details/128894830