酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Python Pillow 库:批量改 PS 图片分辨率且不改变尺寸的实用方法

以下是使用 Python 的 `pillow` 库来批量修改图片分辨率但不改变尺寸的示例代码: ```python from PIL import Image import os def batch_modify_resolution(image_folder, new_resolution): # 遍历文件夹中的图片 for filename in os.listdir(image_folder): if filename.endswith(('.jpg', '.jpeg', '.png')): image_path = os.path.join(image_folder, filename) # 打开图片 with Image.open(image_path) as img: # 修改分辨率 img_resized = img.resize((img.width, img.height), Image.ANTIALIAS) img_resized.save(image_path) # 指定图片文件夹路径和新的分辨率 image_folder = 'your_image_folder' new_resolution = (300, 300) batch_modify_resolution(image_folder, new_resolution) ``` ``` # `batch_modify_resolution` 函数用于批量修改图片分辨率 # `os.listdir` 用于获取文件夹中的文件列表 # 根据文件扩展名筛选图片文件 # `Image.open` 打开图片 # `resize` 方法修改分辨率,`Image.ANTIALIAS` 用于抗锯齿 # 最后保存修改后的图片 ``` 额外需要注意的逻辑点: 1. 确保安装了 `pillow` 库。 2. 提供的图片文件夹路径要准确。 3. 新的分辨率值要根据实际需求合理设置。 ######[AI写代码神器 | 332点数解答 | 2025-01-12 21:08:24]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]