CLUE OnDiskBitmap returning strange values

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
charleseliot
 
Posts: 10
Joined: Tue Aug 11, 2020 11:47 am

CLUE OnDiskBitmap returning strange values

Post by charleseliot »

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

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: CLUE OnDiskBitmap returning strange values

Post by kevinjwalters »

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.

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

Re: CLUE OnDiskBitmap returning strange values

Post by charleseliot »

bitmaps.zip
(244.7 KiB) Downloaded 10 times
Two bitmaps in zip.

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: CLUE OnDiskBitmap returning strange values

Post by kevinjwalters »

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.

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

Re: CLUE OnDiskBitmap returning strange values

Post by charleseliot »

Thank you @kevinjwalters!

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

Post by adafruit_support_carter »

I also get the same values if I load the BMP's from your zip.

Code: Select all

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

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?

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

Re: CLUE OnDiskBitmap returning strange values

Post by charleseliot »

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.

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

Post by adafruit_support_carter »

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

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

Re: CLUE OnDiskBitmap returning strange values

Post by charleseliot »

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

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

Post by adafruit_support_carter »

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.

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: CLUE OnDiskBitmap returning strange values

Post by kevinjwalters »

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.

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

Re: CLUE OnDiskBitmap returning strange values

Post by charleseliot »

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

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)

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

Post by adafruit_support_carter »

Try this:

Code: Select all

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

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

Re: CLUE OnDiskBitmap returning strange values

Post by charleseliot »

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.

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: CLUE OnDiskBitmap returning strange values

Post by adafruit_support_carter »

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

while True:
    display.show(group)

Locked
Please be positive and constructive with your questions and comments.

Return to “CLUE Board”