Giter Site home page Giter Site logo

Image palette is ignored about inky HOT 5 OPEN

pimoroni avatar pimoroni commented on June 12, 2024
Image palette is ignored

from inky.

Comments (5)

mattdesl avatar mattdesl commented on June 12, 2024 2

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()

from inky.

rferrese avatar rferrese commented on June 12, 2024 1

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")

from inky.

Gadgetoid avatar Gadgetoid commented on June 12, 2024

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.

from inky.

RubenLagrouw avatar RubenLagrouw commented on June 12, 2024

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 😁

from inky.

aghster avatar aghster commented on June 12, 2024

The old inky-phat library had a colswap parameter in set_image that could be used to swap colors (see here). Of course, this does not really help honor the palette, but would make it easier to manually reorder the colors. IMO, reintroducing the colswap functionality would not conflict with the aim to make inky image-library-agnostic.

from inky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.