-
Notifications
You must be signed in to change notification settings - Fork 122
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
Image palette is ignored #10
Comments
I just ran into this with my own B&W inkypHAT. Here's what I'm using to take an arbitrary image and convert it to black or white pixels based on a threshold. def reindex_image (other):
rgb_im = other.convert('RGB')
img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
for x in range(inky_display.WIDTH):
for y in range(inky_display.HEIGHT):
(r, g, b) = rgb_im.getpixel((x, y))
color = inky_display.WHITE if r > 127 else inky_display.BLACK
img.putpixel((x, y), color)
return img
# Read your indexed PNG image ...
img = Image.open("foo.png")
# Reindex into black and white colors
img = reindex_image(img)
# buffer & show
inky_dispay.set_image(img)
inky_display.show() |
We should probably whip up an example with this approach, or perhaps build it in. I'm conflicted! I was deliberately trying to make Inky image-library-agnostic, but that does make it difficult to re-introduce features such as palette-order. Right now converting the image with PIL before displaying it on Inky is the "right" approach, but I'm not convinced it's the best. |
I cannot do the conversion with PIL because it cannot handle my input images. Since i'm using imagemagick for conversion anyway, i can just as well do everything there. Except palette order, unfortunately. My workaround works for me, i hope it is useful for someone else too 😁 |
The old |
This caused me a bit of trouble since I was trying to load PNGs and didn't have a good way to change the palette ordering. I made a python script to convert a PNG to the correct Black/White/Red colors for the 3 color display. I figured I'd share to save anyone else the trouble: from PIL import Image
source = "old.png"
destination = "new.png"
# Open source file
oldimage = Image.open(source)
# Create palette. Must contain 768 integer values
palettedata = [
255, 255, 255,
0, 0, 0,
255, 0, 0
]
palettedata += [0] * (768 - len(palettedata))
# Create palette image
palimage = Image.new('P', (1, 1))
palimage.putpalette(palettedata)
# Quantize (change to dither=1 if you want dithering)
newimage = oldimage.quantize(palette=palimage, dither=0)
# Save
newimage.save(destination, "PNG") |
All input images are interpreted as having a white-black-red palette. Unfortunately lots of software does not let you choose the palette order.
I worked around it like this, but it would be nicer to honor the palette instead. (Of course red should be yellow if you have the yellow version...)
The text was updated successfully, but these errors were encountered: