简体   繁体   中英

Python PIL image crop increases file size

I am trying to crop an image via PIL Image.crop() . The image is cropped well but the file size increased from 211 kB to 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 :

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. I suggest using a PNG:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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