简体   繁体   English

Python PIL 图像裁剪增加文件大小

[英]Python PIL image crop increases file size

I am trying to crop an image via PIL Image.crop() .我正在尝试通过 PIL Image.crop()裁剪图像。 The image is cropped well but the file size increased from 211 kB to 24 MB.图像裁剪得很好,但文件大小从 211 kB 增加到 24 MB。 What does the file size increase so much?什么文件大小增加了这么多?

This is the code I'm using:这是我正在使用的代码:

from PIL import Image
import os.path, sys

path = "\\PythonPlot\\plot\\images"
dirs = os.listdir(path)
def crop():
    for item in dirs:
        fullpath = os.path.join(path,item)
        if os.path.isfile(fullpath):
            im = Image.open(fullpath)
            print(im.size)
            f, e = os.path.splitext(fullpath)
            imCrop = im.crop((500, 300, 4000, 2100))
            imCrop.save(f + 'Crop.jpg', "BMP", quality=50, optimize=True)
            print(imCrop.size)
            
crop()

This is the image I'm trying to crop:这是我要裁剪的图像:

示例图像

It looks like you're using a JPEG file extension, but actually saving as a BMP :看起来您使用的是 JPEG 文件扩展名,但实际上保存为BMP

imCrop.save(f + 'Crop.jpg', "BMP", quality=50,optimize=True)

BMP isn't a very efficient format, but JPEG isn't good for this kind of image either. BMP 不是一种非常有效的格式,但 JPEG 也不适合这种图像。 I suggest using a PNG:我建议使用PNG:

imCrop.save(f + 'Crop.png', quality=50, optimize=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM