Black Lives Matter - Action and Equality.
0

Read bmp image file from sd card
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Read bmp image file from sd card

by MikeB35 on Mon May 11, 2020 6:49 pm

Good evening,
I'm doing some tests with a Feather nRF52840 Sense and a 2.4" TFT Featherwing.
I manage to display bmp images stored in the 'CIRCUITPY' space.
But when I store these same images in an SD card inserted in the Featherwing's reader, nothing is displayed with the same code.
However, I can list the files contained on the SD card, so it's not a problem of connection or access to the SD card.

Does anyone have a lead for me?
In advance, thank you.

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by adafruit_support_carter on Mon May 11, 2020 7:51 pm

Once you've created the SD card file system and mounted it, you can pass an SD folder location in as a parameter to slideshow. Example:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import storage
import adafruit_sdcard
from adafruit_slideshow import SlideShow

sd_cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
ss = SlideShow(board.DISPLAY, folder="/sd", loop=True)

while ss.update():
    pass

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

Re: Read bmp image file from sd card

by MikeB35 on Tue May 12, 2020 5:36 am

adafruit_support_carter wrote:Once you've created the SD card file system and mounted it, you can pass an SD folder location in as a parameter to slideshow. Example:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import storage
import adafruit_sdcard
from adafruit_slideshow import SlideShow

sd_cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
ss = SlideShow(board.DISPLAY, folder="/sd", loop=True)

while ss.update():
    pass


Hi,
thank you so much for your concern.
I tried your solution with some modifications because the board I use is the Feather nRF52840 and 'board.SD_CS' or 'board.DISPLAY' are not defined.
But it still doesn't work. I get an error but I don't know how to fix it.

Can you help me clear it up?

This is my code :
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import storage
import adafruit_sdcard
import displayio
import adafruit_ili9341
from adafruit_slideshow import SlideShow

# Release any resources currently in use for the displays
displayio.release_displays()

tft_cs = board.D9
tft_dc = board.D10
display_bus = displayio.FourWire(board.SPI(), command=tft_dc, chip_select=tft_cs, reset=board.D6)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

sd_cs = digitalio.DigitalInOut(board.D5)
sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
ss = SlideShow(display, folder="/sd", loop=True)

while ss.update():
    pass
Attachments
Code (7).jpg
Code (7).jpg (176.42 KiB) Viewed 24 times

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by adafruit_support_carter on Tue May 12, 2020 11:18 am

What hardware has the SD card? Are you using a TFT FeatherWing?

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

Re: Read bmp image file from sd card

by MikeB35 on Tue May 12, 2020 11:43 am

adafruit_support_carter wrote:What hardware has the SD card? Are you using a TFT FeatherWing?


Yes, I'm using the 2.4" TFT Featherwing.

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by adafruit_support_carter on Tue May 12, 2020 11:52 am

Try just listing the contents of the SD card and see if that works:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather nRF52840 Express with nRF52840
>>> import os
>>> import board
>>> import storage
>>> import digitalio
>>> import adafruit_sdcard
>>> sd_cs = digitalio.DigitalInOut(board.D5)
>>> sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
>>> vfs = storage.VfsFat(sdcard)
>>> storage.mount(vfs, "/sd")
>>> os.listdir("/sd")

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

Re: Read bmp image file from sd card

by MikeB35 on Tue May 12, 2020 2:41 pm

adafruit_support_carter wrote:Try just listing the contents of the SD card and see if that works:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather nRF52840 Express with nRF52840
>>> import os
>>> import board
>>> import storage
>>> import digitalio
>>> import adafruit_sdcard
>>> sd_cs = digitalio.DigitalInOut(board.D5)
>>> sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
>>> vfs = storage.VfsFat(sdcard)
>>> storage.mount(vfs, "/sd")
>>> os.listdir("/sd")


All the files of the sd card are listed in the REPL.
Attachments
REPL listdir.jpg
REPL listdir.jpg (80.12 KiB) Viewed 17 times

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by MikeB35 on Tue May 12, 2020 2:52 pm

MikeB35 wrote:
adafruit_support_carter wrote:Try just listing the contents of the SD card and see if that works:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather nRF52840 Express with nRF52840
>>> import os
>>> import board
>>> import storage
>>> import digitalio
>>> import adafruit_sdcard
>>> sd_cs = digitalio.DigitalInOut(board.D5)
>>> sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
>>> vfs = storage.VfsFat(sdcard)
>>> storage.mount(vfs, "/sd")
>>> os.listdir("/sd")


All the files of the sd card are listed in the REPL.


Just to give it another try. I also tested this code with a Feather M4 Express + 2.4" TFT FeatherWing and I get the result on the attached image.
It's the same SD card and I can perfectly list the files on it with 'os.listdir()'. And if these image files are in 'Circuitpy', I can display them on the screen.

This is the code that i used for the test :
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import storage
import adafruit_sdcard
import displayio
import adafruit_ili9341
from adafruit_slideshow import SlideShow

# Release any resources currently in use for the displays
displayio.release_displays()

tft_cs = board.D9
tft_dc = board.D10
display_bus = displayio.FourWire(board.SPI(), command=tft_dc, chip_select=tft_cs, reset=board.D6)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

sd_cs = digitalio.DigitalInOut(board.D5)
sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
ss = SlideShow(display, folder="/sd", loop=True)

while ss.update():
    pass
Attachments
REPL slideshow.jpg
REPL slideshow.jpg (42.59 KiB) Viewed 16 times

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by adafruit_support_carter on Tue May 12, 2020 3:01 pm

It seems to be reading the SD card OK. So not sure why you got the OS Error 5 when trying to run Slideshow. I just tested this with the same hardware you are using and it works OK here.

Where are you getting the BMP files? Does the same OS Error repeat if you try to run slide show again?

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather nRF52840 Express with nRF52840
>>> import board
>>> import storage
>>> import digitalio
>>> import displayio
>>> import adafruit_ili9341
>>> import adafruit_sdcard
>>> from adafruit_slideshow import SlideShow
>>> displayio.release_displays()
>>> tft_cs = board.D9
>>> tft_dc = board.D10
>>> display_bus = displayio.FourWire(board.SPI(), command=tft_dc, chip_select=tft_cs, reset=board.D6)
>>> display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)
>>> sd_cs = digitalio.DigitalInOut(board.D5)
>>> sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
>>> vfs = storage.VfsFat(sdcard)
>>> storage.mount(vfs, "/sd")
>>> ss = SlideShow(display, folder="/sd", loop=True)


ss_test.jpg
ss_test.jpg (166.96 KiB) Viewed 16 times

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

Re: Read bmp image file from sd card

by MikeB35 on Tue May 12, 2020 5:21 pm

adafruit_support_carter wrote:It seems to be reading the SD card OK. So not sure why you got the OS Error 5 when trying to run Slideshow. I just tested this with the same hardware you are using and it works OK here.

Where are you getting the BMP files? Does the same OS Error repeat if you try to run slide show again?

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather nRF52840 Express with nRF52840
>>> import board
>>> import storage
>>> import digitalio
>>> import displayio
>>> import adafruit_ili9341
>>> import adafruit_sdcard
>>> from adafruit_slideshow import SlideShow
>>> displayio.release_displays()
>>> tft_cs = board.D9
>>> tft_dc = board.D10
>>> display_bus = displayio.FourWire(board.SPI(), command=tft_dc, chip_select=tft_cs, reset=board.D6)
>>> display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)
>>> sd_cs = digitalio.DigitalInOut(board.D5)
>>> sdcard = adafruit_sdcard.SDCard(board.SPI(), sd_cs)
>>> vfs = storage.VfsFat(sdcard)
>>> storage.mount(vfs, "/sd")
>>> ss = SlideShow(display, folder="/sd", loop=True)


The attachment ss_test.jpg is no longer available


I'm really lost !
It should not be a problem of bmp files because when I put them in the 'images' folder of 'CircuitPy', I manage to display them in the slideshow.
In fact if I take the previous code :
* with "ss = SlideShow(display, folder="/images", loop=True)" everything works fine
* with "ss = SlideShow(display, folder="/sd", loop=True)", I get a 'OSError' error message on a bmp file (see attached screenshot)

In both cases, the same files are used (on the SD card and in the images folder).
Attachments
REPL slideshow.jpg
REPL slideshow.jpg (42.59 KiB) Viewed 16 times

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by adafruit_support_carter on Tue May 12, 2020 5:26 pm

I'm lost too. Not sure why it's working here but not for you. See if you can load a single image from the SD card using this approach:
https://learn.adafruit.com/circuitpytho ... geload-9-8
Do the file system set up and mounting same as above, and then pass in something like "/sd/001.bmp" as the first parameter for adafruit_imageload.load().

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

Re: Read bmp image file from sd card

by MikeB35 on Wed May 13, 2020 12:25 pm

adafruit_support_carter wrote:I'm lost too. Not sure why it's working here but not for you. See if you can load a single image from the SD card using this approach:
https://learn.adafruit.com/circuitpytho ... geload-9-8
Do the file system set up and mounting same as above, and then pass in something like "/sd/001.bmp" as the first parameter for adafruit_imageload.load().


I've tried two kind of tests :
The first with the lirary 'ImageLoad'
Here is my code :
Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import os
from digitalio import *
import adafruit_ili9341
from adafruit_sdcard import *
from storage import *
import adafruit_imageload

# Release any resources currently in use for the displays
displayio.release_displays()

spi = board.SPI()
# while not spi.try_lock():
#     pass
tft_cs = board.D9
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D6)
time.sleep(1)  # Wait a bit
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

# Instanciation de la carte SD et montage du volume avec le nom 'sd'
SD_CS = DigitalInOut(board.D5)
carte_sd = SDCard(spi, SD_CS)
vfs = VfsFat(carte_sd)
mount(vfs, "/sd")
print (os.listdir("/sd"))

bitmap, palette = adafruit_imageload.load("/sd/006.bmp",
                                         bitmap=displayio.Bitmap,
                                         palette=displayio.Palette)

# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)

# Create a Group to hold the TileGrid
group = displayio.Group()

# Add the TileGrid to the Group
group.append(tile_grid)

# Add the Group to the Display
display.show(group)
#display.refresh()
print ("fin")
# Loop forever so you can enjoy your image
while True:
    pass


When I test with an image (006.bmp from the Adafruit site) on 'Circuitpy', I get an error like 'True color BMP unsupported'.
code - Imageload - files on circuitpy.png
code - Imageload - files on circuitpy.png (31.74 KiB) Viewed 14 times

When I test this same code but providing the path to the SD card that contains the same image, I also get an I/O error ('OSError : [Errno 5])

The second test use 'OnDiskBitmap'.
Here is my code :
Code: Select all | TOGGLE FULL SIZE
import board
import time
import displayio
import os
from storage import *
from digitalio import *
import adafruit_ili9341
from adafruit_sdcard import *

# Release any resources currently in use for the displays
displayio.release_displays()

spi = board.SPI()
# while not spi.try_lock():
#     pass
tft_cs = board.D9
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D6)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

# Instanciation de la carte SD et montage du volume avec le nom 'sd'
SD_CS = DigitalInOut(board.D5)
carte_sd = SDCard(spi, SD_CS)
vfs = VfsFat(carte_sd)
mount(vfs, "/sd")
print (os.listdir("/sd"))

time.sleep(1)  # Wait a bit

with open("/sd/006.bmp", "rb") as bitmap_file:
    bitmap = displayio.OnDiskBitmap(bitmap_file)
    tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
    group = displayio.Group()
    group.append(tile_grid)
    display.show(group)
    display.refresh()

print ("fin")

while True:
    pass



When I try with the image file on 'CircuitPy' (the same image '006.bmp'), IT WORKS !!
But when I try from the SD Card, I've received an OS Error like with 'ImageLoad'.

Here's what my latest tests showed. I hope it can shed some light.

Sincerely, Michael.

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by MikeB35 on Wed May 13, 2020 12:26 pm

MikeB35 wrote:
adafruit_support_carter wrote:I'm lost too. Not sure why it's working here but not for you. See if you can load a single image from the SD card using this approach:
https://learn.adafruit.com/circuitpytho ... geload-9-8
Do the file system set up and mounting same as above, and then pass in something like "/sd/001.bmp" as the first parameter for adafruit_imageload.load().


I've tried two kind of tests :
The first with the lirary 'ImageLoad'
Here is my code :
Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import os
from digitalio import *
import adafruit_ili9341
from adafruit_sdcard import *
from storage import *
import adafruit_imageload

# Release any resources currently in use for the displays
displayio.release_displays()

spi = board.SPI()
# while not spi.try_lock():
#     pass
tft_cs = board.D9
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D6)
time.sleep(1)  # Wait a bit
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

# Instanciation de la carte SD et montage du volume avec le nom 'sd'
SD_CS = DigitalInOut(board.D5)
carte_sd = SDCard(spi, SD_CS)
vfs = VfsFat(carte_sd)
mount(vfs, "/sd")
print (os.listdir("/sd"))

bitmap, palette = adafruit_imageload.load("/sd/006.bmp",
                                         bitmap=displayio.Bitmap,
                                         palette=displayio.Palette)

# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)

# Create a Group to hold the TileGrid
group = displayio.Group()

# Add the TileGrid to the Group
group.append(tile_grid)

# Add the Group to the Display
display.show(group)
#display.refresh()
print ("fin")
# Loop forever so you can enjoy your image
while True:
    pass


When I test with an image (006.bmp from the Adafruit site) on 'Circuitpy', I get an error like 'True color BMP unsupported'.
The attachment code - Imageload - files on circuitpy.png is no longer available

When I test this same code but providing the path to the SD card that contains the same image, I also get an I/O error ('OSError : [Errno 5])

The second test use 'OnDiskBitmap'.
Here is my code :
Code: Select all | TOGGLE FULL SIZE
import board
import time
import displayio
import os
from storage import *
from digitalio import *
import adafruit_ili9341
from adafruit_sdcard import *

# Release any resources currently in use for the displays
displayio.release_displays()

spi = board.SPI()
# while not spi.try_lock():
#     pass
tft_cs = board.D9
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D6)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

# Instanciation de la carte SD et montage du volume avec le nom 'sd'
SD_CS = DigitalInOut(board.D5)
carte_sd = SDCard(spi, SD_CS)
vfs = VfsFat(carte_sd)
mount(vfs, "/sd")
print (os.listdir("/sd"))

time.sleep(1)  # Wait a bit

with open("/sd/006.bmp", "rb") as bitmap_file:
    bitmap = displayio.OnDiskBitmap(bitmap_file)
    tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
    group = displayio.Group()
    group.append(tile_grid)
    display.show(group)
    display.refresh()

print ("fin")

while True:
    pass



When I try with the image file on 'CircuitPy' (the same image '006.bmp'), IT WORKS !!
But when I try from the SD Card, I've received an OS Error like with 'ImageLoad'.

Here's what my latest tests showed. I hope it can shed some light.

Sincerely, Michael.
Attachments
code - Imageload - file on SD card.png
code - Imageload - file on SD card.png (29.7 KiB) Viewed 14 times

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by MikeB35 on Wed May 13, 2020 12:27 pm

And the last screen capture with OnDiskBitmap from SD Card
Attachments
code - OnDiskBitmap - file on SD Card.png
code - OnDiskBitmap - file on SD Card.png (27.54 KiB) Viewed 14 times

MikeB35
 
Posts: 21
Joined: Thu Feb 05, 2015 10:18 am

Re: Read bmp image file from sd card

by adafruit_support_carter on Wed May 13, 2020 1:24 pm

When I test with an image (006.bmp from the Adafruit site) on 'Circuitpy', I get an error like 'True color BMP unsupported'.

You are getting these from here?
https://learn.adafruit.com/creating-sli ... ctures-4-6

Describe how you are downloading and copying them to your CIRCUITPY folder. It seems like something is altering the image format.

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

Please be positive and constructive with your questions and comments.