Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions sirius/coding/image_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@
THRESHOLD = 127
TRANSLATE = [(1536, 255), (1152, 254), (768, 253), (384, 252), (251, 251)]


def pixel_to_bw(p):
if p == (0, 0, 0, 0): # 0 alpha means white.
return WHITE
elif p[0] > THRESHOLD or p[1] > THRESHOLD or p[2] > THRESHOLD:
return WHITE
else:
return BLACK


def ilen(i):
"Returns length of an iterator in constant space."
return sum(1 for _ in i)
Expand Down Expand Up @@ -56,15 +46,14 @@ def rle(lengths):
yield remainder


def crop_384(im):
def resize_384(im):
w, h = im.size
return im.crop((0, 0, 384, min(h, 10000)))

resize_width = 384
resize_height = resize_width * h / w
return im.resize((resize_width, resize_height), Image.ANTIALIAS)

def threshold(im):
thresholded = ''.join(pixel_to_bw(x) for x in im.convert('RGBA').getdata())
return Image.fromstring('L', im.size, thresholded)

return im.convert('1')

def rle_from_bw(bw_image):
"""
Expand Down Expand Up @@ -133,5 +122,5 @@ def default_pipeline(html):
"""Encode HTML into an RLE image."""
data = html_to_png(html)
image = Image.open(data)
image = crop_384(image)
image = resize_384(image)
return threshold(image)