Skip to content

Instantly share code, notes, and snippets.

@todbot
Created April 3, 2024 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todbot/224c690a9fc2c15e972a2e963deb13fa to your computer and use it in GitHub Desktop.
Save todbot/224c690a9fc2c15e972a2e963deb13fa to your computer and use it in GitHub Desktop.
bitmapfilter playing w/ channel ops and blur on random lines in CircuitPython
# bitmapfilter_play3_code.py -- chops + blur on random lines
# 2 Apr 2024 - @todbot / Tod Kurt
import time, math, random
import board
import rainbowio
import vectorio
import displayio
import bitmaptools
import bitmapfilter
import gc
display = board.DISPLAY
display.auto_refresh = False
dw,dh = display.width, display.height
cx,cy = dw//2, dh//2
display.root_group = displayio.Group()
bitmap = displayio.Bitmap(dw, dh, 65535)
pixel_shader = displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED)
tile_grid = displayio.TileGrid(bitmap, pixel_shader=pixel_shader)
display.root_group.append(tile_grid)
color_converter = displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB888)
def swap(v):
"""swap high & low bytes of 16-bit number, since RGB565_SWAPPED"""
return ((v & 0xff) << 8) | ((v >> 8) & 0xff)
def to_bitmap_color(c):
"""convert RGB888 to RGB565_SWAPPED, surely there's a better way for this, gotta chat w/jepler"""
return swap(color_converter.convert(c))
def make_random_lines(bitmap, xo,yo, sx,sy, n, colr):
for i in range(n):
x = xo + random.randint(-sx//2, sx//2)
y = yo + random.randint(-sy//2, sy//2)
x1 = xo + random.randint(-sx,sx)
y1 = yo + random.randint(-sy,sy)
bitmaptools.draw_line(bitmap, x,y, x1,y1, colr)
chops = [
bitmapfilter.ChannelScale(0.9999, 0.9, 0.9999), # dim
#bitmapfilter.ChannelScale(1.2, 0.8, 1.2), # cyber
bitmapfilter.ChannelScale(1.1, 1.0, 1.1), # bright
bitmapfilter.ChannelMixerOffset( .393, .769, .189, .01, # sepia sorta
.349, .686, .168, .01,
.272, .534, .131, .01),
bitmapfilter.ChannelMixer( 0.5, 0.25, 0.25, 0, 1, 0, 0, 1, 0), # mix-into-red
]
last_time = time.monotonic()
last_chop_time = last_time
last_blur_time = last_time
chop_i=0
tic = 100 # position in phase space, arbitrary starting point
x,y = 0,0
colr = to_bitmap_color(rainbowio.colorwheel(time.monotonic()*10))
tic_speed = 0.01
while True:
gc.collect()
now = time.monotonic()
for i in range(15):
tic += tic_speed
#print("%.2f lines! %3.2f %.2f, %.2f" %(now, tic, x,y))
x = int( cx + dw/2.5 * math.sin(tic*2.9) )
y = int( cy + dh/2.5 * math.sin(tic*2.2333) )
make_random_lines(bitmap, x, y, 12, 8, n=5, colr=colr)
if now - last_time > 0.1:
last_time = now
tile_grid.bitmap = bitmapfilter.mix(bitmap, chops[chop_i])
colr = to_bitmap_color(rainbowio.colorwheel(time.monotonic()*10))
if now - last_blur_time > 0.2:
last_blur_time = now
bitmapfilter.morph(bitmap, (1, 2, 1, 2, 4, 2, 1, 2, 1) ) # 3x3 gaussian blur
if now - last_chop_time > 5:
last_chop_time = now
chop_i = (chop_i+1) % len(chops)
print("new chop!",chop_i)
display.refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment