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
Comments
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 |
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') |
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 |
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 |
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. |
Issue fixed by RoadrunnerWMC. Please see RoadrunnerWMC/libimagequant-python#1. |
out_img.im.putpalettealphas(bytes(c.a for c in out_palette)) |
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. |
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:
The text was updated successfully, but these errors were encountered: