Skip to main content

Image from the web

What this example shows​

Connect to Wi-Fi, download a JPG from a URL and draw it with Floyd-Steinberg dithering, which mixes the six panel colors to approximate the colors of the photo. Put your network name and password in SSID and PASSWORD before you run it. The example's notes give about 52 seconds for a JPG, or about 90 seconds with dithering.

This example also uses MicroPython's own network module, which is not part of the Inkplate modules.

Example code​

"""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()

Functions used​

network.WLAN(network.STA_IF)returns WLAN

MicroPython: the Wi-Fi station interface. do_connect() calls isconnected(), active(True), connect() and ifconfig() on it and gives up after 30 seconds.

ReturnsThe interface object.

Inkplate()returns Inkplate

Creates the display object from the inkplate7spectra module.

ReturnsThe display object.

inkplate.begin()returns bool

Sets up the board and a white frame buffer. Call it once.

ReturnsTrue.

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

Draws a BMP, JPG or PNG image into the frame buffer. A path starting with http:// or https:// is downloaded; anything else is read as a local file. The format comes from the file extension.

ReturnsNothing. An unsupported extension raises ValueError.

Parameters

NameDescription
pathURL or local path ending in .bmp, .jpg, .jpeg or .png.
x0, y0Top-left corner in pixels. Default 0.
invertInvert the colors. Default False.
ditherDither to the six panel colors. Default False.
kernel_typeDithering kernel: Inkplate.KERNEL_FLOYD_STEINBERG (0), KERNEL_JJN (1), KERNEL_STUCKI (2) or KERNEL_BURKES (3). Default 0.
inkplate.display(leave_on=False)returns None

Sends the frame buffer to the panel and refreshes it.

ReturnsNothing.

What to expect​

The shell prints Connecting to network... and then Network config: with the board's network settings. When the download and decoding finish, the screen refreshes and shows the picture from the top-left corner. If the board cannot connect within 30 seconds, the shell prints Failed to connect within timeout and the script stops with WiFi connection failed.

🔗Open the complete example

examples/inkplate7spectra/display_image_web.py

→