简体   繁体   中英

Why does resizing a JPEG image using PIL, increases file size?

I have written a Python code which resizes an image into a max 1200 x 1200 frame, maintaining the aspect ratio. I am coming across this case where the input image(1080 x 1350) is 249.8KB whereas the output image(960 x 1200) is 317.2KB. This is happening in spite of optimize = True and maintaining the quality. My code is as below:

from PIL import Image
from wand.image import Image as Wand

MAX_RES = 1200
photo = Image.open("input.jpg")
breadth,height = photo.size
qual = Wand(filename="input.jpg").compression_quality
if(not((breadth <= MAX_RES) and (height <= MAX_RES))):
    resizeRatio = max (float(breadth)/MAX_RES, float(height)/MAX_RES)
    photo = photo.resize((int(breadth/resizeRatio),int(height/resizeRatio)))
    photo.save("output.jpg",optimization = True,quality=qual) 

Using Image.ANTIALIAS increases the size even more.

There are a number of factors that affect JPEG compression:

  1. Subsampling of Cb and Cr components.
  2. Quantization tables use (sometimes dumbed down to "quality" settings)
  3. Whether optimized huffman tables are used.

It is highly likely your input settings were much different from the output settings.

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