siva
siva

Reputation: 15

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:

example image

Upvotes: 0

Views: 712

Answers (1)

Chris
Chris

Reputation: 136880

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)

Upvotes: 2

Related Questions