Bild aus dem Web
Was dieses Beispiel zeigt
Das Beispiel verbindet sich mit dem WLAN, lädt ein JPG von einer URL herunter und zeichnet es mit Floyd-Steinberg-Dithering. Dabei werden die sechs Farben des Panels gemischt, um die Farben des Fotos anzunähern. Tragen Sie Ihren Netzwerknamen und Ihr Passwort in SSID und PASSWORD ein, bevor Sie das Beispiel ausführen. Laut den Hinweisen des Beispiels dauert ein JPG etwa 52 Sekunden, mit Dithering etwa 90 Sekunden.
Dieses Beispiel verwendet außerdem das eigene network-Modul von MicroPython, das nicht zu den Inkplate-Modulen gehört.
Beispielcode
"""Connect to WiFi and render an image from a URL."""
# Include needed libraries
import network
import time
from inkplate7spectra import Inkplate
# WiFi credentials (replace with your own)
SSID = "YOUR_SSID_HERE"
PASSWORD = "YOUR_PASSWORD_HERE"
# Connects to a WiFi network using given SSID and PASSWORD.
#
# Returns:
# - True if successfully connected
# - False if connection fails within the timeout period
#
# Notes:
# - Timeout is set to 30 seconds
# - Prints network IP config on success
def do_connect():
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print("Connecting to network...")
sta_if.active(True)
sta_if.connect(SSID, PASSWORD)
timeout = 30 # seconds
start = time.ticks_ms()
while not sta_if.isconnected():
if time.ticks_diff(time.ticks_ms(), start) > timeout * 1000:
print("Failed to connect within timeout")
return False
time.sleep(0.5)
print("Network config:", sta_if.ifconfig())
return True
# Create Inkplate object
inkplate = Inkplate()
# Initialize the display, needs to be called only once
inkplate.begin()
# Connect to WiFi
if not do_connect():
raise SystemExit("WiFi connection failed")
# 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:
inkplate.draw_image(
"https://i.imgur.com/ESkX8xU.jpeg", # URL to image
0,
0, # X, Y position
invert=False, # Do not invert colors
dither=True, # Enable dithering
kernel_type=Inkplate.KERNEL_FLOYD_STEINBERG, # Dithering algorithm
)
# Show the image from the internal buffer
inkplate.display()
Verwendete Funktionen
network.WLAN(network.STA_IF)returns WLANMicroPython: die WLAN-Station-Schnittstelle. do_connect() ruft darauf isconnected(), active(True), connect() und ifconfig() auf und gibt nach 30 Sekunden auf.
ReturnsDas Schnittstellenobjekt.
Inkplate()returns InkplateErstellt das Display-Objekt aus dem Modul inkplate7spectra.
ReturnsDas Display-Objekt.
inkplate.begin()returns boolRichtet das Board und einen weißen Framebuffer ein. Rufen Sie sie nur einmal auf.
ReturnsTrue.
inkplate.draw_image(path, x0=0, y0=0, invert=False, dither=False, kernel_type=0)returns NoneZeichnet ein BMP-, JPG- oder PNG-Bild in den Framebuffer. Ein Pfad, der mit http:// oder https:// beginnt, wird heruntergeladen; alles andere wird als lokale Datei gelesen. Das Format ergibt sich aus der Dateiendung.
ReturnsNichts. Eine nicht unterstützte Dateiendung löst einen ValueError aus.
Parameter
| Name | Beschreibung |
|---|---|
path | URL oder lokaler Pfad, der auf .bmp, .jpg, .jpeg oder .png endet. |
x0, y0 | Obere linke Ecke in Pixeln. Standard 0. |
invert | Invertiert die Farben. Standard False. |
dither | Dithering auf die sechs Farben des Panels. Standard False. |
kernel_type | Dithering-Kernel: Inkplate.KERNEL_FLOYD_STEINBERG (0), KERNEL_JJN (1), KERNEL_STUCKI (2) oder KERNEL_BURKES (3). Standard 0. |
inkplate.display(leave_on=False)returns NoneSendet den Framebuffer an das Panel und aktualisiert es.
ReturnsNichts.
Was Sie erwarten können
Die Shell gibt Connecting to network... aus und danach Network config: mit den Netzwerkeinstellungen des Boards. Wenn Download und Dekodierung abgeschlossen sind, wird der Bildschirm aktualisiert und zeigt das Bild ab der oberen linken Ecke. Kann sich das Board nicht innerhalb von 30 Sekunden verbinden, gibt die Shell Failed to connect within timeout aus und das Skript endet mit WiFi connection failed.
🔗Vollständiges Beispiel öffnen
examples/inkplate7spectra/display_image_web.py