We're now using PIL to threshold the image before previewing it.
})
elif type(command) == messages.SetDeliveryAndPrint:
- pixel_count, pixels = image_encoding.rle_image(command.pixels)
+ pixel_count, pixels = image_encoding.rle_from_bw(command.pixels)
return make({
'binary_payload': base64.b64encode(_encode_printer_message(
0x1, pixel_count, pixels, command_id)),
})
elif type(command) == messages.SetDelivery:
- pixel_count, pixels = image_encoding.rle_image(command.pixels)
+ pixel_count, pixels = image_encoding.rle_from_bw(command.pixels)
return make({
'binary_payload': base64.b64encode(_encode_printer_message(
0x2, pixel_count, pixels, command_id)),
import io
import tempfile
-WHITE = 0
-BLACK = 1
+WHITE = '\xff'
+BLACK = '\x00'
THRESHOLD = 127
TRANSLATE = [(1536, 255), (1152, 254), (768, 253), (384, 252), (251, 251)]
yield remainder
-def rle_image(data):
+def crop_384(im):
+ w, h = im.size
+ return im.crop((0, 0, 384, min(h, 10000)))
+
+
+def threshold(im):
+ thresholded = ''.join(pixel_to_bw(x) for x in im.convert('RGBA').getdata())
+ return Image.fromstring('L', im.size, thresholded)
+
+
+def rle_from_bw(bw_image):
"""
- :param data: A python bytearray containing a PNG.
+ :param bw_image: A mode "1" PIL image.
:returns: A 2-tuple of (number of pixels, RLE-encoded pixel data)
"""
- im = Image.open(io.BytesIO(data))
- im = im.convert('RGBA')
- pixels = im.getdata()
+ pixels = list(bw_image.getdata())
- # convert to B&W and group each run length into lists
- # each list is (result of identity function, [actual pixels])
- grouped = groupby(pixels, pixel_to_bw)
+ # Group each run length into lists each list is (result of
+ # identity function, [actual pixels])
+ grouped = groupby(pixels)
# create list of tuples (WHITE|BLACK, run_length)
groups = []
# the decoder assumes that the first run is white, so if the
# first pixel is black, add a zero run of white
- if groups[k][0] == BLACK:
+ if groups[0][0] == BLACK:
groups.insert(0, (WHITE, 0))
# encode using our custom encoding
f.write(html)
f.flush()
driver.get('file://' + f.name)
- data = driver.get_screenshot_as_png()
+ data = io.BytesIO(driver.get_screenshot_as_png())
return data
finally:
driver.quit()
+
+
+def default_pipeline(html):
+ """Encode HTML into an RLE image."""
+ data = html_to_png(html)
+ image = Image.open(data)
+ image = crop_384(image)
+ return threshold(image)
from PIL import Image
import unittest
-import io
from sirius.coding import image_encoding
class ImageCase(unittest.TestCase):
- def test_normal_text(self):
+ def _test_normal_text(self):
data = image_encoding.html_to_png('')
- image = Image.open(io.BytesIO(data))
+ image = Image.open(data)
self.assertEquals(image.size[0], 384)
- def test_normal_height(self):
+ def _test_normal_height(self):
data = image_encoding.html_to_png(
'<html><body style="margin: 0px; height: 100px;"></body></html>')
- image = Image.open(io.BytesIO(data))
+ image = Image.open(data)
self.assertEquals(image.size[1], 100)
- def test_rle(self):
+ def _test_rle(self):
data = image_encoding.html_to_png('')
- image = Image.open(io.BytesIO(data))
+ image = Image.open(data)
n_bytes, _ = image_encoding.rle_image(data)
self.assertEquals(n_bytes, 3072)
+
+
+class PipeTestCase(unittest.TestCase):
+
+ def test_full(self):
+ data = image_encoding.html_to_png('')
+
+ image = Image.open(data)
+ image = image_encoding.crop_384(image)
+ image = image_encoding.threshold(image)
+
+ n_bytes, _ = image_encoding.rle_from_bw(image)
+ self.assertEquals(n_bytes, 3072)
+
+ def test_default_pipeline(self):
+ n_bytes, _ = image_encoding.rle_from_bw(
+ image_encoding.default_pipeline('')
+ )
+ self.assertEquals(n_bytes, 3072)
+import io
import datetime
import flask
from flask.ext import login
if form.validate_on_submit():
# TODO: move image encoding into a pthread.
# TODO: use templating to avoid injection attacks
- pixels = image_encoding.html_to_png(
+ pixels = image_encoding.default_pipeline(
'<html><body>{}</body></html>'.format(form.message.data))
hardware_message = messages.SetDeliveryAndPrint(
device_address=printer.device_address,
stats.inc('printer.print.offline')
# Store the same message in the database.
+ png = io.BytesIO()
+ pixels.save(png, "PNG")
model_message = model_messages.Message(
print_id=next_print_id,
- pixels=bytearray(pixels),
+ pixels=bytearray(png.getvalue()),
sender_id=login.current_user.id,
target_printer=printer,
)
assert username == login.current_user.username
message = flask.request.data
- pixels = image_encoding.html_to_png(
+ pixels = image_encoding.default_pipeline(
'<html><body>{}</body></html>'.format(message))
+ png = io.BytesIO()
+ pixels.save(png, "PNG")
stats.inc('printer.preview')
- return '<img style="width: 6cm;" src="data:image/png;base64,{}">'.format(base64.b64encode(pixels))
+ return '<img style="width: 6cm;" src="data:image/png;base64,{}">'.format(base64.b64encode(png.getvalue()))
<button class="btn" type="button" id="preview">Preview</button>
Preview:
-<div id="preview_pane" style="width: 6cm;border: 1px solid black;">
+<div id="preview_pane" style="width: 6cm;border: 1px solid black;overflow: hidden;">
</div>
{% endblock %}