Python 修改图片分辨率(单张/批量/等比例/递归覆盖)

使用 Python PIL 库实现图片分辨率修改,支持单张图片、批量文件夹、等比例缩放、递归目录覆盖原图,4种场景完整代码,一键调整图片尺寸大小。

1. 修改单张图片分辨率

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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. 批量修改文件夹内所有图片分辨率

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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(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. 等比例修改图片分辨率

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
    new_height = None

    resize_image(input_path, output_path, new_width, new_height)

4. 递归等比例修改并覆盖原图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
    new_height = None

    resize_images_in_directory(target_directory, new_width, new_height)

使用说明

  • 安装依赖:pip install pillow
  • 根据需求选择对应脚本
  • 修改文件路径、宽度、高度参数
  • 直接运行即可
  • 支持格式:jpg、jpeg、png、gif

每日更新学习资料、行业资讯、技术干货与疑难解答,免费分享资源下载,助力学习者高效提升。