4

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.

Sagnik Sinha
  • 873
  • 1
  • 11
  • 22
  • Do you know for certain what the original JPEG settings were? (quality etc.) – SuperBiasedMan Nov 24 '15 at 11:37
  • 1
    File size of a compressed image is not directly related to image size. A compression method can be less efficient when you modify the image, even a simple resize. – Jean-Baptiste Yunès Nov 24 '15 at 11:39
  • @SuperBiasedMan, how does one know that on an Ubuntu machine? I have the file on my hard-disk – Sagnik Sinha Nov 24 '15 at 11:44
  • 1
    @SagnikSinha I'm not familiar with PIL but there's an extensive answer on that [here](http://stackoverflow.com/questions/4354543/determining-jpg-quality-in-python-pil) – SuperBiasedMan Nov 24 '15 at 11:52
  • @SagnikSinha You can find out image properties using the [`identify` command-line tool](http://www.imagemagick.org/script/identify.php). It is possible that your original image had a lower quality than the resized one. – user4815162342 Nov 25 '15 at 11:16
  • @user4815162342, I am getting the quaity using `wand` and you can see in the code, `Wand(filename="input.jpg").compression_quality`. Even after maintaining the quality and reducing the size, the output file size ends up larger than that of the input file. – Sagnik Sinha Nov 25 '15 at 11:21

1 Answers1

0

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.

user3344003
  • 20,574
  • 3
  • 26
  • 62