Slika s weba
Što ovaj primjer prikazuje
Povežite se na Wi-Fi, preuzmite JPG s URL-a i nacrtajte ga s Floyd-Steinbergovim ditheringom, koji miješa šest boja zaslona kako bi aproksimirao boje fotografije. Prije pokretanja upišite naziv svoje mreže i lozinku u SSID i PASSWORD. Bilješke primjera navode oko 52 sekunde za JPG, odnosno oko 90 sekundi s ditheringom.
Ovaj primjer koristi i MicroPythonov vlastiti modul network, koji nije dio Inkplate modula.
Primjer koda
"""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()
Korištene funkcije
network.WLAN(network.STA_IF)returns WLANMicroPython: sučelje Wi-Fi stanice. do_connect() poziva isconnected(), active(True), connect() i ifconfig() na njemu i odustaje nakon 30 sekundi.
ReturnsObjekt sučelja.
Inkplate()returns InkplateStvara objekt zaslona iz modula inkplate7spectra.
ReturnsObjekt zaslona.
inkplate.begin()returns boolPostavlja pločicu i bijeli međuspremnik okvira. Pozovite ga jednom.
ReturnsTrue.
inkplate.draw_image(path, x0=0, y0=0, invert=False, dither=False, kernel_type=0)returns NoneCrta BMP, JPG ili PNG sliku u međuspremnik okvira. Putanja koja počinje s http:// ili https:// se preuzima; sve ostalo čita se kao lokalna datoteka. Format se određuje prema ekstenziji datoteke.
ReturnsNišta. Nepodržana ekstenzija izaziva ValueError.
Parametri
| Naziv | Opis |
|---|---|
path | URL ili lokalna putanja koja završava na .bmp, .jpg, .jpeg ili .png. |
x0, y0 | Gornji lijevi kut u pikselima. Zadano 0. |
invert | Invertira boje. Zadano False. |
dither | Dithering na šest boja zaslona. Zadano False. |
kernel_type | Dithering kernel: Inkplate.KERNEL_FLOYD_STEINBERG (0), KERNEL_JJN (1), KERNEL_STUCKI (2) ili KERNEL_BURKES (3). Zadano 0. |
inkplate.display(leave_on=False)returns NoneŠalje međuspremnik okvira na zaslon i osvježava ga.
ReturnsNišta.
Što očekivati
Shell ispisuje Connecting to network..., a zatim Network config: s mrežnim postavkama pločice. Kada preuzimanje i dekodiranje završe, zaslon se osvježi i prikaže sliku od gornjeg lijevog kuta. Ako se pločica ne uspije povezati unutar 30 sekundi, shell ispisuje Failed to connect within timeout, a skripta se zaustavlja s porukom WiFi connection failed.
🔗Otvorite cijeli primjer
examples/inkplate7spectra/display_image_web.py