ps批量修改图片大小的方法是什么
使用Python的Pillow库可以批量修改图片大小。具体步骤如下:
- 安装Pillow库,在命令行中输入以下命令:
pip install Pillow
- 编写Python脚本,导入Pillow库和os库。
from PIL import Image
import os
- 遍历指定文件夹中的所有图片文件,并进行大小修改。
# 指定文件夹路径
folder_path = 'path/to/folder'
# 遍历文件夹中的所有文件
for file_name in os.listdir(folder_path):
# 判断是否为图片文件
if file_name.endswith('.jpg') or file_name.endswith('.png'):
# 打开图片文件
image = Image.open(os.path.join(folder_path, file_name))
# 修改图片大小
new_image = image.resize((new_width, new_height))
# 保存修改后的图片
new_image.save(os.path.join(folder_path, 'new_' + file_name))
其中,new_width
和new_height
为新的图片宽度和高度。new_image.save()
中的new_
前缀是为了避免覆盖原始图片文件。
- 运行Python脚本,即可批量修改图片大小。
python resize_images.py