Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent png converted to green #4941

Closed
German-Corpaz opened this issue Oct 2, 2020 · 8 comments · Fixed by #5089
Closed

Transparent png converted to green #4941

German-Corpaz opened this issue Oct 2, 2020 · 8 comments · Fixed by #5089

Comments

@German-Corpaz
Copy link

German-Corpaz commented Oct 2, 2020

Hi, I'm using libimagequant library to optimize a png, and the result is perfect, the only problem is that pngs with transparent background are converted to a green background, i don't know why this is happening, the palette generated is correct is there a solution for this, apart from replacing colors?

Code:

img = img.convert('RGBA')
attr = liq.Attr()
input_image = attr.create_rgba(img.tobytes(), img.width, img.height, 0)
result = input_image.quantize(attr)
out_pixels = result.remap_image(input_image)
out_palette = result.get_palette()
out_img = Image.frombytes('P', (img.width, img.height), out_pixels)
palette_data = []
for color in out_palette:
    palette_data.append(color.r)
    palette_data.append(color.g)
    palette_data.append(color.b)
out_img.putpalette(palette_data)
out_img.save('output.png')
@hugovk
Copy link
Member

hugovk commented Oct 2, 2020

Please can you share an example image that causes this?

Your code isn't self-contained. Please share runnable example code. For example, we don't know what's in img, or what liq is.

@German-Corpaz
Copy link
Author

German-Corpaz commented Oct 2, 2020

Sorry about that

from PIL import Image
import libimagequant as liq

img = Image.open("test.png")
img = img.convert('RGBA')
attr = liq.Attr()
input_image = attr.create_rgba(img.tobytes(), img.width, img.height, 0)
result = input_image.quantize(attr)
out_pixels = result.remap_image(input_image)
out_palette = result.get_palette()
out_img = Image.frombytes('P', (img.width, img.height), out_pixels)
palette_data = []
for color in out_palette:
    palette_data.append(color.r)
    palette_data.append(color.g)
    palette_data.append(color.b)
out_img.putpalette(palette_data)
out_img.save('output.png')

test

output

@wiredfool
Copy link
Member

wiredfool commented Oct 2, 2020

Have you tried something like this:

img = Image.open("test.png")
quantized = img.quantize(method=Image.LIBIMAGEQUANT)
quantized.save('out.png')

https://github.com/python-pillow/Pillow/blob/master/src/PIL/Image.py#L1039

@German-Corpaz
Copy link
Author

I didnt build pillow to use libimagequant, thats why im doing it the other way.

But i think the error of the bg from transparent to green is coming in the putpalette method, i dont know why but its not using transparency

@wiredfool
Copy link
Member

There are a few ways to do transparency, either using 'PA' mode (paletted w/ alpha) or setting the im.info['transparency'] value.

But the basic palletted image doesn't have any transparency, as you can see by passing in the rgb values, but not alpha.

@ghost
Copy link

ghost commented Oct 10, 2020

Issue fixed by RoadrunnerWMC. Please see RoadrunnerWMC/libimagequant-python#1.

@gofr
Copy link
Contributor

gofr commented Oct 10, 2020

putpalette doesn't look like it supports alpha. But there's an apparently undocumented function that does. Add the following line before the save() at the end and I think it works:

out_img.im.putpalettealphas(bytes(c.a for c in out_palette))

@radarhere
Copy link
Member

Using the suggestion from @gofr as a starting point, I've put together PR #5089. If that is merged, then the last few lines of your code could be changed to

palette_data = []
for color in out_palette:
    palette_data.append(color.r)
    palette_data.append(color.g)
    palette_data.append(color.b)
    palette_data.append(color.a)
out_img.putpalette(palette_data, "RGBA")
out_img.save('output.png')

and the correct result would be generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants