0

CLUE OnDiskBitmap returning strange values
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CLUE OnDiskBitmap returning strange values

by charleseliot on Tue Aug 11, 2020 2:57 pm

Environment: CLUE with CPython

I've been struggling to understand the same color palette management issues others have discussed in this forum. As an experiment I tried to load some BMP files directly with OnDiskBitmap. The bitmaps appeared to load fine, but nothing showed up on the CLUE display.

The load code looked like this:

image_file = open("/images/image.bmp", "rb")
image = displayio.OnDiskBitmap(image_file)

Interrogating the image object produced some obviously wrong results.

# This was a 240x240 BMP downloaded from Adafruit
print(image.width) >>> 240
print(image.height) >>> 65296

# This was a 215x195 BMP
print(image.width) >>> 215
print(image.height) >>> 65341

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by kevinjwalters on Tue Aug 11, 2020 5:04 pm

If you are happy to share the images might be worth attaching them to a reply in the forum with File Upload feature. I think it doesn't like bmp so you'll either have to add a fake extension or put them in something like a zip file.

kevinjwalters
 
Posts: 960
Joined: Sun Oct 01, 2017 3:15 pm

Re: CLUE OnDiskBitmap returning strange values

by charleseliot on Tue Aug 11, 2020 6:00 pm

bitmaps.zip
(244.7 KiB) Downloaded 5 times


Two bitmaps in zip.

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by kevinjwalters on Tue Aug 11, 2020 6:36 pm

I've got some CLUEs plugged in. They show same thing for your Charles.bmp which curiously loads into GIMP fine.

There's a reasonable chance it's the phenomena/feature described in https://stackoverflow.com/questions/764 ... ive-height

I'll ask around for you. This is just a guess but if you convert to an interim format in your favourite image editor/tool you can probably convert your file into a positive height bmp, e.g. save as png, load that, save back as a bmp.

kevinjwalters
 
Posts: 960
Joined: Sun Oct 01, 2017 3:15 pm

Re: CLUE OnDiskBitmap returning strange values

by charleseliot on Tue Aug 11, 2020 6:57 pm

Thank you @kevinjwalters!

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by adafruit_support_carter on Tue Aug 11, 2020 6:59 pm

I also get the same values if I load the BMP's from your zip.
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.1 on 2020-07-13; Adafruit CLUE nRF52840 Express with nRF52840
>>> import displayio
>>> fp = open("adafruit_products_adabot240.bmp", "rb")
>>> image = displayio.OnDiskBitmap(fp)
>>> image.width
240
>>> image.height
65296
>>>


The image viewing software on any PC, smart phone, etc. is going to be *way* more tolerant to various image format variations than CircuitPython, which is very limited. I think what is happening here (and similar cases) is some form of image file manipulation, either via the OS or some image viewing software. This is happening automatically behind the scenes for who knows what reasons.

I took the adafruit_products_adabot240.bmp image from your zip, loaded it into GIMP, and it showed up fine. I then converted it to an Indexed image and re-exported it as a new BMP file. That new BMP file then behaves as expected in CP:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.1 on 2020-07-13; Adafruit CLUE nRF52840 Express with nRF52840
>>> import displayio
>>> fp = open("adafruit_products_adabot240_2.bmp", "rb")
>>> image = displayio.OnDiskBitmap(fp)
>>> image.width
240
>>> image.height
240
>>>


What operating system are you using?
How did you download the original images?
Did you do anything with the images on the PC before copying to the CLUE?
How did you copy the image file to the CLUE?

adafruit_support_carter
 
Posts: 20311
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

by charleseliot on Tue Aug 11, 2020 7:19 pm

What operating system are you using? >>> macOS Catalina (10.15.6)

How did you download the original images? >>> Charles.bmp was converted from a JPG (see below for more). The other bitmap was downloaded directly from https://learn.adafruit.com/assets/79222 and used unchanged.

Did you do anything with the images on the PC before copying to the CLUE? >>> Nothing to the adafruit image. The Charles.bmp file was created using the Export feature of macOS's Preview program.

How did you copy the image file to the CLUE? >>> Drag-and-drop to CIRCUITPY file share.

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by adafruit_support_carter on Tue Aug 11, 2020 7:26 pm

OK, I'm seeing the same thing for that BMP of Adabot. Was that referenced in a guide?

adafruit_support_carter
 
Posts: 20311
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

by charleseliot on Tue Aug 11, 2020 7:59 pm

Yes, the Adabot image was used in a guide somewhere, but not specifically for the CLUE. (I spent some time looking for the URL, but so far I've failed. Sorry.)

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by adafruit_support_carter on Tue Aug 11, 2020 8:41 pm

No worries. We you find it let us know. It could be that the same thing is happening on our side with some of the hosted images.

adafruit_support_carter
 
Posts: 20311
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

by kevinjwalters on Wed Aug 12, 2020 6:17 am

Perhaps it was this one Adafruit Circuit Playground TFT Gizmo: Drawing Bitmaps but the page update time and file content suggests this might have been updated/fixed already.

kevinjwalters
 
Posts: 960
Joined: Sun Oct 01, 2017 3:15 pm

Re: CLUE OnDiskBitmap returning strange values

by charleseliot on Wed Aug 12, 2020 12:01 pm

Thank you for all your help so far.

Using GIMP I was able to make a version of the Charles.bmp file that loaded with correct positive height (195). Alas, the image still doesn't display on the CLUE (blank screen).

My test code looks like this (Charles2.bmp is the GIMP-created version of Charles.bmp)

Code: Select all | TOGGLE FULL SIZE
display = board.DISPLAY

disp_height = display.height
disp_width = display.width

display.auto_brightness = True

group = displayio.Group(max_size = 1)
group.x = disp_width
group.y = disp_height

image_file = open('/images/Charles2.bmp', 'rb')
image = displayio.OnDiskBitmap(image_file)

image_sprite = displayio.TileGrid(image, pixel_shader = displayio.ColorConverter())
group.append(image_sprite)

display.refresh(target_frames_per_second = 60)

while True:
    display.show(group)

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by adafruit_support_carter on Wed Aug 12, 2020 12:11 pm

Try this:
Code: Select all | TOGGLE FULL SIZE
import board
import displayio

image_file = open('/images/Charles2.bmp', 'rb')
image = displayio.OnDiskBitmap(image_file)

image_sprite = displayio.TileGrid(image, pixel_shader=displayio.ColorConverter())

group = displayio.Group(max_size = 1)
group.append(image_sprite)

board.DISPLAY.show(group)

while True:
    pass

adafruit_support_carter
 
Posts: 20311
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

by charleseliot on Wed Aug 12, 2020 1:47 pm

Cool! That worked (with the expected 888 to 565 palette degradation).

Which parts of the code did you suspect? All the code was borrowed from your on-line articles, but not all from CLUE samples.

charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

Re: CLUE OnDiskBitmap returning strange values

by adafruit_support_carter on Wed Aug 12, 2020 2:17 pm

Turning on auto brightness was one thing. Not that it doesn't work, but can maybe cause unexpected behavior. For simple testing, just leave it off and run full brightness. After that works, can enable it and play around with it and see what happens, etc.

The other was continually calling show in the loop. Do we have code that looks like this in a guide?
Code: Select all | TOGGLE FULL SIZE
while True:
    display.show(group)

adafruit_support_carter
 
Posts: 20311
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.