以下是一个降低JPEG图片分辨率的Python代码示例:

from PIL import Image

def resize_jpeg(image_path, output_path, max_size):
    with Image.open(image_path) as img:
        if img.mode != 'RGB':
            img = img.convert('RGB')

        width, height = img.size
        if width > max_size or height > max_size:
            ratio = min(max_size/width, max_size/height)
            new_width = int(width * ratio)
            new_height = int(height * ratio)
            img = img.resize((new_width, new_height), Image.ANTIALIAS)

        img.save(output_path, optimize=True, quality=85)

if __name__ == '__main__':
    image_path = 'example.jpg'
    output_path = 'example_resized.jpg'
    max_size = 1024

    resize_jpeg(image_path, output_path, max_size)

这个函数使用Pillow库中的Image模块打开JPEG图片,检查它的分辨率是否超过了指定的最大值。如果是,它将根据比例缩小图像,并将其保存为新的JPEG文件。该函数还使用optimize和quality参数来优化输出文件的大小和质量

降低jpeg图片分辨率python代码

原文地址: https://www.cveoy.top/t/topic/gAhM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录