I see two options which you can both achieve using PIL library given for instance an RGB image.
1 – Reduce the image resolution (and therefore quality – you might not want that). Use the Image.thumbnail() or Image.resize() method provided by PIL.
2 – Reduce the color range of the image (the number of colors will affect file size accordingly). This is what many online converters will do.
img_path = "img.png"
img = Image.open(img_path)
img = img.convert("P", palette=Image.ADAPTIVE, colors=256)
img.save("comp.png", optimize=True)
Answer from Joe Web on Stack OverflowI see two options which you can both achieve using PIL library given for instance an RGB image.
1 – Reduce the image resolution (and therefore quality – you might not want that). Use the Image.thumbnail() or Image.resize() method provided by PIL.
2 – Reduce the color range of the image (the number of colors will affect file size accordingly). This is what many online converters will do.
img_path = "img.png"
img = Image.open(img_path)
img = img.convert("P", palette=Image.ADAPTIVE, colors=256)
img.save("comp.png", optimize=True)
The PNG format only supports lossless compression, for which the compression ratio is usually limited and not freely adjustable.
If I am right, there is a variable parameter that tells the compressor to spend more or less time finding a better compression scheme. But without a guarantee to succeed.