MagTag Online Image

Posted on March 15, 2023

Here is a bit of code that pulls a random image from the internet and displays it on a MagTag screen.

import board
import displayio
import adafruit_imageload
import adafruit_requests as requests
import adafruit_magtag.magtag as magtag
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
from adafruit_imageload import dither


# Set up the MagTag display and buttons
magtag = magtag.MagTag()

# Get the image from picsum.photos
image_url = "https://picsum.photos/g/296/128"
response = requests.get(image_url)
image_file = response.content
image, palette = adafruit_imageload.load(image_file, bitmap=displayio.Bitmap, palette=displayio.Palette)

# Dither the image
dither.dither_bitmask(image, bitmap=displayio.Bitmap)

# Create a new display group and show the image on the screen
group = displayio.Group()
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
group.append(tile_grid)
magtag.display.show(group)

# Wait for button press to exit
while not magtag.peripherals.button_a:
    pass

Comments are closed.