adafruit / Adafruit_CircuitPython_ImageLoad

Super-slim image decoder that loads code as needed to minimize memory use
MIT License
15 stars 20 forks source link

Imageload palette has no len() #80

Open 7ensation opened 4 months ago

7ensation commented 4 months ago

Im trying to dynamically index a palette that is being loaded via imageload in circuitpython, however, when i try to get the length of the palette an error occurs saying "object of type 'ColorConverter' has no len()" code sample below:

source_bitmap, source_palette = adafruit_imageload.load("bitmap.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette) test = len(source_palette)

ch4nsuk3 commented 1 month ago

I was unable to reproduce this on CircuitPython 9.0.4, instead getting the expected behavior. If you are still experiencing can you please provide the version of CircuitPython you are using? It can be found by reading the boot_out.txt file in your CIRCUITPY drive.

FoamyGuy commented 1 month ago

I think this might boil down to a difference between Palette which does have a length, and ColorConverter which does not have length.

imageload will return a Palette if the loaded image is indexed, and that palette will have a length representing the number of different colors in the Palette.

However if the image loaded is RGB (not indexed) then imageload would return a ColorConverter instance not a palette. ColorConverter does not use a specific subset of colors like Palette so it doesn't have a length. It's function is to convert RGB888 color values (which is what it assumes to find in RGB non-indexed images) into RGB565 colorspace because that is what displayio uses internally.

ch4nsuk3 commented 1 month ago

That makes sense. In that case I noticed RTD is missing information on adafruit_imageload.bmp.truecolor, so there is no mention of getting a ColorConverter object back instead of a Palette. The information given for adafruit_imageload.bmp simply states it returns a tuple of a Bitmap and a Palette so getting a ColorConverter back could be a bit of a surprise, especially for someone who isnt aware there are differences between indexed and non-indexed bitmaps.

A method allowing the user to check which type of image they have (and whether or not its a supported type) may be handy, though I dont think that this is a big enough issue to merit adding code.

7ensation commented 1 month ago

Its been a while since i was messing with this but from what i remember, theres multiple types of bitmaps and you have to convert the image to the correct type of bitmap for this to work.