]> git.aero2k.de Git - sirius.git/commitdiff
Pass around pixel data instead of a PIL image object.
authorTom Hunger <tehunger@gmail.com>
Tue, 20 Jan 2015 12:49:46 +0000 (12:49 +0000)
committerTom Hunger <tehunger@gmail.com>
Tue, 20 Jan 2015 12:49:46 +0000 (12:49 +0000)
We want to store the pixel data in the database, so we pass around the
lowest common representation.

sirius/coding/image_encoding.py
sirius/coding/test_image_coding.py

index 4ce2bd111ab20266b2f6502d393700f2149dcc3e..57cdb5d534b729e59cd5c6558ea4f3fe392e94c8 100644 (file)
@@ -56,11 +56,12 @@ def rle(lengths):
                     yield remainder
 
 
-def rle_image(im):
+def rle_image(data):
     """
-    :param im: A python PIL image.
+    :param data: A python bytearray containing a PNG.
     :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()
 
@@ -108,8 +109,8 @@ def html_to_png(html):
             f.write(html)
             f.flush()
             driver.get('file://' + f.name)
-            p = driver.get_screenshot_as_png()
+            data = driver.get_screenshot_as_png()
 
-        return Image.open(io.BytesIO(p))
+        return data
     finally:
         driver.quit()
index 4033129491de70d29bbaba578cb2c45afe3b2fca..dc9b9d6c8a2029cb163e202119fee0e89dd3261f 100644 (file)
@@ -1,4 +1,6 @@
+from PIL import Image
 import unittest
+import io
 
 from sirius.coding import image_encoding
 
@@ -6,15 +8,18 @@ from sirius.coding import image_encoding
 class ImageCase(unittest.TestCase):
 
     def test_normal_text(self):
-        image = image_encoding.html_to_png('')
+        data = image_encoding.html_to_png('')
+        image = Image.open(io.BytesIO(data))
         self.assertEquals(image.size[0], 384)
 
     def test_normal_height(self):
-        image = image_encoding.html_to_png(
+        data = image_encoding.html_to_png(
             '<html><body style="margin: 0px; height: 100px;"></body></html>')
+        image = Image.open(io.BytesIO(data))
         self.assertEquals(image.size[1], 100)
 
     def test_rle(self):
-        image = image_encoding.html_to_png('')
-        n_bytes, _ = image_encoding.rle_image(image)
+        data = image_encoding.html_to_png('')
+        image = Image.open(io.BytesIO(data))
+        n_bytes, _ = image_encoding.rle_image(data)
         self.assertEquals(n_bytes, 3072)