top of page

1.5.23

  • owenmichael12333
  • Jan 5, 2023
  • 2 min read

This week I continued working on my current project. I was able to figure out how to use a gif on the OLED display. To do this, I download gif from icons8.com and create a folder and place the gif and a python file with code that converts it into a bmp file.

from os import listdir
from PIL import Image

OUTPUT_SIZE = (
    64,
    64,
)  # The output size of each frame (or tile or Sprite) of the animation
MONOCHROME = False  # Do you want the output file to be b/w?

for file in listdir():
    if file.endswith(".gif"):
        gif = Image.open(file)
        print(f"Size: {gif.size}")
        print(f"Frames: {gif.n_frames}")

        if MONOCHROME:
            output = Image.new("1", (OUTPUT_SIZE[0] * gif.n_frames, OUTPUT_SIZE[1]), 0)
        else:
            output = Image.new("RGB", (OUTPUT_SIZE[0] * gif.n_frames, OUTPUT_SIZE[1]))
        output_filename = f"icon_{gif.n_frames}_frames.bmp"

        for frame in range(0, gif.n_frames):
            gif.seek(frame)
            extracted_frame = gif.resize(OUTPUT_SIZE)
            position = (OUTPUT_SIZE[0] * frame, 0)
            output.paste(extracted_frame, position)
        if not MONOCHROME:
            output = output.convert("P", colors=8)
        output.save(output_filename)

I then edit the converted file if necessary, for example if I need to invert the colors, and then move it to the Pi Pico.

# Raspberry Pi Pico: http://educ8s.tv/part/RaspberryPiPico
# OLED DISPLAY: https://educ8s.tv/part/OLED096

import board, busio, displayio, time
import adafruit_displayio_ssd1306
import adafruit_imageload

IMAGE_FILE = "sunny_28.bmp"
SPRITE_SIZE = (64, 64)
FRAMES = 28

def invert_colors():
    temp = icon_pal[0]
    icon_pal[0] = icon_pal[1]
    icon_pal[1] = temp

displayio.release_displays()

sda, scl = board.GP0, board.GP1
i2c = busio.I2C(scl, sda)

display_bus = displayio.I2CDisplay(i2c, device_address=0x3D)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)

group = displayio.Group()

#  load the spritesheet
icon_bit, icon_pal = adafruit_imageload.load(IMAGE_FILE,
                                                 bitmap=displayio.Bitmap,
                                                 palette=displayio.Palette)
invert_colors()

icon_grid = displayio.TileGrid(icon_bit, pixel_shader=icon_pal,
                                 width=1, height=1,
                                 tile_height=SPRITE_SIZE[1], tile_width=SPRITE_SIZE[0],
                                 default_tile=0,
                                 x=32, y=0)

group.append(icon_grid)

display.show(group)

timer = 0
pointer = 0

while True:
  if (timer + 0.1) < time.monotonic():
    icon_grid[0] = pointer
    pointer += 1
    timer = time.monotonic()
    if pointer > FRAMES-1:
      pointer = 0

Currently I am converting different weather types so that I am able to use them on the display.




 
 
 

Recent Posts

See All
5.11.23

This week I did not get much done due to very few class periods this week. I spent the beginning of the week working on cleaning up the...

 
 
 

Comentários


bottom of page