修改图片分辨率,python实现修改图片分辨率(大小)

1.修改一张图片分辨率

将input.jpg和output.jpg改成自己的输入输出文件路径

from PIL import Image

def resize_image(input_image_path, output_image_path, new_width, new_height):
    image = Image.open(input_image_path)
    resized_image = image.resize((new_width, new_height))
    resized_image.save(output_image_path)

# 使用示例
input_image_path = "input.jpg"
output_image_path = "output.jpg"
new_width = 800
new_height = 600

resize_image(input_image_path, output_image_path, new_width, new_height)

2.将一个目录(文件夹)上的所有图片修改成相同的分辨率

import os
from PIL import Image

def resize_images_in_folder(input_folder, output_folder, new_width, new_height):
    # 遍历输入文件夹中的所有文件
    for filename in os.listdir(input_folder):
        if filename.endswith(".jpg") or filename.endswith(".png"):
            # 构建输入和输出文件的路径
            input_image_path = os.path.join(input_folder, filename)
            output_image_path = os.path.join(output_folder, filename)
            
            # 调用resize_image函数修改图片分辨率
            resize_image(input_image_path, output_image_path, new_width, new_height)

def resize_image(input_image_path, output_image_path, new_width, new_height):
    image = Image.open(input_image_path)
    resized_image = image.resize((new_width, new_height))
    resized_image.save(output_image_path)

# 使用示例
input_folder = "input_folder"
output_folder = "output_folder"
new_width = 800
new_height = 600

resize_images_in_folder(input_folder, output_folder, new_width, new_height)

3.等比例修改图片分辨率(大小)

from PIL import Image

def resize_image(input_path, output_path, new_width, new_height):
    # 打开图片
    image = Image.open(input_path)

    # 计算缩放比例
    width, height = image.size
    aspect_ratio = width / height

    if new_width is None:
        new_width = int(new_height * aspect_ratio)
    elif new_height is None:
        new_height = int(new_width / aspect_ratio)

    # 缩放图片
    resized_image = image.resize((new_width, new_height), Image.ANTIALIAS)

    # 保存缩放后的图片
    resized_image.save(output_path)

if __name__ == "__main__":
    input_path = "input.jpg"  # 输入图片的文件路径
    output_path = "output.jpg"  # 输出图片的文件路径
    new_width = 800  # 新的宽度,设置为None以根据高度自动计算
    new_height = None  # 新的高度,设置为None以根据宽度自动计算

    resize_image(input_path, output_path, new_width, new_height)

4.递归实现等比例修改一个目录下所有目录的图片分辨率,并且覆盖之前图片

import os
from PIL import Image

def resize_image(input_path, new_width, new_height):
    # 打开图片
    image = Image.open(input_path)

    # 计算缩放比例
    width, height = image.size
    aspect_ratio = width / height

    if new_width is None:
        new_width = int(new_height * aspect_ratio)
    elif new_height is None:
        new_height = int(new_width / aspect_ratio)

    # 缩放图片
    resized_image = image.resize((new_width, new_height), Image.ANTIALIAS)

    # 覆盖原始图片
    resized_image.save(input_path)

def resize_images_in_directory(directory, new_width, new_height):
    for root, dirs, files in os.walk(directory):
        for filename in files:
            if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
                input_path = os.path.join(root, filename)
                resize_image(input_path, new_width, new_height)

if __name__ == "__main__":
    target_directory = "your_directory_path"  # 指定要处理的目录的路径
    new_width = 800  # 新的宽度,设置为None以根据高度自动计算
    new_height = None  # 新的高度,设置为None以根据宽度自动计算

    resize_images_in_directory(target_directory, new_width, new_height)

  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓小邰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值