Zum Hauptinhalt springen

Image from microSD

Was dieses Beispiel zeigt​

Binden Sie die microSD-Karte ein, listen Sie ihre Dateien in der Shell auf und zeichnen Sie image.jpg mit Dithering von der Karte. Kopieren Sie vor dem Ausführen eine JPG-Datei als image.jpg in das Stammverzeichnis der Karte, oder ändern Sie IMAGE_PATH. Am Ende wird die Versorgung der Karte abgeschaltet.

Beispielcode​

"""Initialize an SD card and render an image located on it."""

# Include needed libraries
from inkplate7spectra import Inkplate

from os import listdir, stat

# Create Inkplate object
inkplate = Inkplate()

# Initialize the display, needs to be called only once
inkplate.begin()

# Initializes the SD card.
#
# Parameters:
# - fastboot (bool, default=False):
# If True, performs a soft reboot immediately after SD card initialization
# (only on cold start or hard reset). This significantly improves SD card
# read speeds—typically doubling performance.
#
# Note:
# - This function must be called before accessing files on the SD card.
# - The fastboot option has no effect if the device is already running.
inkplate.init_sd_card(fast_boot=True)

# This prints all the files on card
print(listdir("/sd"))


# Draw an image on the screen.
#
# Parameters:
# - path: File path to the image. Supports local paths (e.g., from SD card) or URLs.
# Supported formats: JPG, PNG, BMP.
#
# - x0: X-coordinate of the top-left corner where the image will be displayed.
#
# - y0: Y-coordinate of the top-left corner where the image will be displayed.
#
# - invert (bool, default=False): If True, inverts the image colors.
#
# - dither (bool, default=False): If True, applies a dithering algorithm to
# the image for better grayscale rendering.
#
# - kernel_type (int): Specifies the dithering algorithm to use.
# Available options:
# Inkplate.KERNEL_FLOYD_STEINBERG = 0
# Inkplate.KERNEL_JJN = 1
# Inkplate.KERNEL_STUCKI = 2
# Inkplate.KERNEL_BURKES = 3
#
# Performance Notes:
# - JPG: ~52 seconds (or ~90s with dithering)
#
# Example usage:
IMAGE_PATH = "sd/image.jpg"
try:
stat(IMAGE_PATH)
except OSError:
print("Image not found on SD card: {}".format(IMAGE_PATH))
print("Copy an image to that path on the SD card, or change IMAGE_PATH above.")
else:
inkplate.draw_image(
IMAGE_PATH,
0,
0,
invert=False,
dither=True,
kernel_type=Inkplate.KERNEL_FLOYD_STEINBERG,
)

# Show the image from the buffer
inkplate.display()

inkplate.sd_card_sleep()
# To turn it back on, use:
# inkplate.sd_card_wake()

Verwendete Funktionen​

Inkplate()returns Inkplate

Erzeugt das Display-Objekt aus dem Modul inkplate7spectra.

ReturnsDas Display-Objekt.

inkplate.begin()returns bool

Richtet das Board und einen weißen Framebuffer ein. Rufen Sie sie nur einmal auf.

ReturnsTrue.

inkplate.init_sd_card(fast_boot=False)returns None

Schaltet die Versorgung der Karte ein und bindet die Karte unter /sd ein. Mit fast_boot=True führt sie nach einem Power-on-, Hard- oder Watchdog-Reset außerdem einen Soft-Reset durch, der laut Beispiel die Lesegeschwindigkeit etwa verdoppelt. Kann die Karte nicht gelesen werden, gibt sie 'Sd card could not be read' aus.

ReturnsNichts.

inkplate.draw_image(path, x0=0, y0=0, invert=False, dither=False, kernel_type=0)returns None

Zeichnet ein BMP-, JPG- oder PNG-Bild in den Framebuffer. Ein Pfad ohne http:// oder https:// wird als lokale Datei gelesen, hier von der Karte. Das Format ergibt sich aus der Dateiendung.

ReturnsNichts. Eine nicht unterstützte Dateiendung löst einen ValueError aus.

inkplate.display(leave_on=False)returns None

Sendet den Framebuffer an das Panel und aktualisiert es.

ReturnsNichts.

inkplate.sd_card_sleep()returns None

Schaltet die Versorgung der Karte ab.

ReturnsNichts.

inkplate.sd_card_wake()returns None

Schaltet die Versorgung der Karte nach sd_card_sleep() wieder ein. Das Beispiel erwähnt sie nur in einem Kommentar.

ReturnsNichts.

listdir() und stat() stammen aus dem Modul os von MicroPython.

Was Sie erwartet​

Die Shell gibt die Liste der Dateien auf der Karte aus. Ist image.jpg vorhanden, wird der Bildschirm aktualisiert und zeigt das Bild ab der oberen linken Ecke. Fehlt die Datei, gibt die Shell Image not found on SD card: sd/image.jpg und den Hinweis aus, ein Bild zu kopieren, und der Bildschirm wird auf Weiß aktualisiert.

🔗Vollständiges Beispiel öffnen

examples/inkplate7spectra/displayimagesd/display_image_sd.py

→