4

I have used PIL to convert and resize JPG/BMP file to PNG format. I can easily resize and convert it to PNG, but the file size of the new image is too big.

im = Image.open('input.jpg')
im_resize = im.resize((400, 400), Image.ANTIALIAS)  # best down-sizing filter
im.save(`output.png')

What do I have to do to reduce the image file size?

wovano
  • 4,543
  • 5
  • 22
  • 49
hangman
  • 865
  • 5
  • 20
  • 31

2 Answers2

4

PNG Images still have to hold all data for every single pixel on the image, so there is a limit on how far you can compress them.

One way to further decrease it, since your 400x400 is to be used as a "thumbnail" of sorts, is to use indexed mode:

im_indexed = im_resize.convert("P") im_resize.save(... )

*wait * Just saw an error in your example code: You are saving the original image, not the resized image:

im=Image.open(p1.photo)
im_resize = im.resize((400, 400), Image.ANTIALIAS)    # best down-sizing filter
im.save(str(merchant.id)+'_logo.'+'png')

When you should be doing:

im_resize.save(str(merchant.id)+'_logo.'+'png')

You are just saving back the original image, that is why it looks so big. Probably you won't need to use indexed mode them.

Aother thing: Indexed mode images can look pretty poor - a better way out, if you come to need it, might be to have your smalle sizes saved as .jpg instead of .png s - these can get smaller as you need, trading size for quality.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

You can use other tools like PNGOUT

Vivek S
  • 5,384
  • 8
  • 51
  • 72