question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pngsave: how to cancel quantizing when there are more than 256 colors in the image?

See original GitHub issue

When pngsave() is used with palette=True and an image with more than 256 colors the save process becomes lossy without any warnings.

It can be workarounded by counting number of colors prior to saving but I haven’t found a way to do that with pyvips directly yet, any suggestions? The closest thing I’ve found is https://github.com/libvips/php-vips/issues/65 but it’s not the precise count that is required here.

I can imagine a solution to this problem being a new pngsave parameter that signifies that only a lossless conversion is acceptable. When it is set to true simultaneously with palette the palette will not be used when there are more than 256 colors in the image.

Do the alpha channel limitations mentioned in http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.5.2 make the conversion lossy in any case?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jcupittcommented, Mar 27, 2021

Oh, interesting idea, I’d never thought of that. Something like this, I guess?

def __bool__(self):
    return (self != 0).min() != 0

ie. we’re True if all elements are non-zero. Does that sound right?

1reaction
jcupittcommented, Mar 25, 2021

Hi @int-ua, sorry, I missed this issue 😦

I think PNG palettisation is almost always lossy. I think you’re right, the alpha is included.

The safest way to tell would probably be to load the image back again and test for equality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reduce the Number of Colors in an Image - MATLAB & ...
If an indexed image has more than 256 colors, MATLAB cannot store the image ... An important term in discussions of image quantization...
Read more >
More than 256 colors on indexed mode.
Open the image in SnesGFX. Choose the Format - any 8bit format will work. We are not interested in the bitmap file output,...
Read more >
Effective gif/image color quantization? - java
The problem is, it doesn't seem to be very "smart." If I pass in an image with more than 256 colors, it does...
Read more >
Quantization -- IM v6 Examples
For example to convert a JPEG or PNG image containing millions of colors, into a GIF image containing a maximum of 256 color,...
Read more >
Color quantization
However, even with the best set of 256 colors, there are many images that look bad. They have visible contouring in regions where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found