parent
0dbaf6b6e6
commit
1571d11322
@ -0,0 +1,93 @@ |
|||||||
|
from PIL import Image |
||||||
|
STATIC = 1 |
||||||
|
ANIMATION = 2 |
||||||
|
|
||||||
|
startx, starty = 16,25 |
||||||
|
|
||||||
|
image_on = Image.open("file_on_m.bmp") |
||||||
|
image_off = Image.open("file_off_m.bmp") |
||||||
|
# im = image.convert(mode='') |
||||||
|
# im_pixels = im.load() |
||||||
|
# # access pixels via [x, y] |
||||||
|
# for col in range(im.size[0]): |
||||||
|
# for row in range(im.size[1]): |
||||||
|
# brightness = sum(im_pixels[col, row]) |
||||||
|
# if brightness < 255*2: |
||||||
|
# im_pixels[col, row] = (0, 0, 0) |
||||||
|
# else: |
||||||
|
# im_pixels[col, row] = (255,255,255) |
||||||
|
# im.save('file_on_m.bmp') |
||||||
|
|
||||||
|
|
||||||
|
def bmp_to_bitmap_fn(im): |
||||||
|
def fn(img): |
||||||
|
x = [xc if sum(im.getpixel((xc,yc))) == 0 else None for xc in range(im.size[0]) for yc in range(im.size[1])] |
||||||
|
y = [yc if sum(im.getpixel((xc,yc))) == 0 else None for xc in range(im.size[0]) for yc in range(im.size[1])] |
||||||
|
x = list(filter(lambda x: x!=None, x)) |
||||||
|
y = list(filter(lambda y: y!=None, y)) |
||||||
|
xmin, xmax = min(x), max(x) |
||||||
|
ymin, ymax = min(y), max(y) |
||||||
|
# print(xmin, xmax, ymin, ymax) |
||||||
|
xsize = (xmax-xmin) |
||||||
|
ysize = (ymax-ymin) |
||||||
|
print(len(img)) |
||||||
|
for y in range(ysize): |
||||||
|
for x in range(0,xsize): |
||||||
|
# print(xmin+x+bit, ymin+y) |
||||||
|
if im.size[0] >= x and sum(im.getpixel((xmin+x,ymin+y))) == 0: |
||||||
|
# print( im.size[0], x,x+bit, (xmin+x+bit,ymin+y)) |
||||||
|
pos = ((y * xsize) + x) // 8 |
||||||
|
bit = ((y * xsize) + x) % 8 |
||||||
|
print(pos, 1<<bit) |
||||||
|
if pos < len(img): |
||||||
|
img[pos] |= 1 << bit |
||||||
|
return img, xsize, ysize |
||||||
|
return fn |
||||||
|
|
||||||
|
PNG_file_OFF = bmp_to_bitmap_fn(image_off) |
||||||
|
PNG_file_ON = bmp_to_bitmap_fn(image_on) |
||||||
|
|
||||||
|
|
||||||
|
with open("animation.bin", "wb") as fx: |
||||||
|
# |
||||||
|
fx.write(bytearray([0x42,0x4e,0x17,0xee])) #magic header |
||||||
|
fx.write(bytearray([2])) # objecets |
||||||
|
|
||||||
|
fx.write(bytearray([STATIC, 0,0])) |
||||||
|
fx.write(bytearray([120,60])) |
||||||
|
fx.write(bytearray([0]*((120*60+7)//8))) |
||||||
|
|
||||||
|
|
||||||
|
fx.write(bytearray([ANIMATION, startx, starty])) # we start at 0,0 for now |
||||||
|
# print(PNG_file_OFF([0]*120*60)[1],PNG_file_OFF([0]*120*60)[2]) |
||||||
|
# print(PNG_file_ON([0]*120*60)[1],PNG_file_ON([0]*120*60)[2]) |
||||||
|
ww, hh = 100, 10 |
||||||
|
uu = 3# update interval, 0 every tick, 1 every second tick |
||||||
|
animation = [ |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_OFF, |
||||||
|
PNG_file_ON, |
||||||
|
] |
||||||
|
fx.write(bytearray([ww,hh,len(animation),uu])) |
||||||
|
for bmp_fun in animation: |
||||||
|
img = bytearray([0]*((ww*hh+7)//8)) |
||||||
|
img,xsize,ysize = bmp_fun(img) |
||||||
|
|
||||||
|
print("framestart=",fx.tell()) |
||||||
|
fx.write(img) |
||||||
|
print("frameend=",fx.tell()) |
||||||
|
|
||||||
|
print("size:", fx.tell()) |
||||||
|
assert fx.tell() < 50000, "do not use too much image space" |
||||||
Loading…
Reference in new issue