更改图片的分辨率

Python程序

from PIL import Image
import os

def resize_images_in_folder(folder_path, output_folder, new_width, new_height):
    """
    更改指定文件夹中所有图片的分辨率。

    :param folder_path: 包含原始图片的文件夹路径
    :param output_folder: 存储更改分辨率后的图片的文件夹路径
    :param new_width: 新图片的宽度
    :param new_height: 新图片的高度
    """
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(('.png', '.jpg', '.JPG', '.jpeg', '.bmp', '.gif')):
            # 构建完整的文件路径
            file_path = os.path.join(folder_path, filename)
            # 打开图片
            with Image.open(file_path) as img:
                # 调整图片大小
                img_resized = img.resize((new_width, new_height), Image.ANTIALIAS)
                # 构建输出文件的路径
                output_path = os.path.join(output_folder, filename)
                # 保存更改分辨率后的图片
                img_resized.save(output_path)
                print(f"图片已更改分辨率并保存:{output_path}")


# 使用示例
folder_path = ''  # 原始图片文件夹路径
output_folder = ''  # 更改分辨率后图片的存储文件夹路径
new_width = 360  # 新宽度
new_height = 360  # 新高度

resize_images_in_folder(folder_path, output_folder, new_width, new_height)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值