1
0
Fork 0

WIP: tools: Simple RLE encoder

pull/5/head
Daniel Thompson 2020-01-23 18:55:03 +00:00
parent 7ea3acc21a
commit 1ec5c11ea7
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
from PIL import Image
im = Image.open('pine64.png')
pixels = im.load()
rle = []
rl = 0
px = 0
for y in range(im.height):
for x in range(im.width):
newpx = pixels[x, y]
if newpx == px:
rl += 1
continue
assert(rl < 255)
rle.append(rl)
rl = 1
px = newpx
rle.append(rl)
sx = 240
sy = 240
image = bytes(rle)
data = bytearray(2*sx)
dp = 0
black = ord('#')
white = ord(' ')
color = black
for rl in image:
while rl:
data[dp] = color
data[dp+1] = color
dp += 2
rl -= 1
if dp >= (2*sx):
print(data.decode('utf-8'))
dp = 0
if color == black:
color = white
else:
color = black
assert(dp == 0)
print(image)