sedthh / pyxelate

Python class that generates pixel art from images
MIT License
1.58k stars 108 forks source link

Transparent images output less colours #41

Open shuZro opened 2 weeks ago

shuZro commented 2 weeks ago

I used the trex example on a another png file with transparent background. But when I run it the colours are much more reduced. If I use the same image with non transparent background the output is fine. Any ideas?

sedthh commented 2 weeks ago

Try to remove the transparency in the image via something like Photoshop and see if the transparent areas are actually have different colors. The palette is calculated using all pixels, and transparent areas are usually all black with transparency, but that is not always the case.

shuZro commented 2 weeks ago

I was using https://github.com/danielgatis/rembg to remove the background of an image. Then was passing it to pyxelate. Here is some of my code `def process_image(image, size, colours, remove_bg=False) -> Image:

image = image.resize((64, 64), Image.Resampling.NEAREST) # # LANCZOS

if remove_bg:
    image = remove(image)

    image.save("removed.png")

sk_img = np.array(image)
pyx = Pyx(factor=512 / size, palette=colours)
pyx.fit(sk_img)
new_image = pyx.transform(sk_img)
image = Image.fromarray(new_image)
image.save("pixel.png")

return image`

Here is the source image, transparent image and the pixelated image. See the colours.

new removed pixel