Browse Source

Use imbytes() to hide the Image.tostring() / tobytes() horrorshow

jamesbowman 7 years ago
parent
commit
e286fd47a2
4 changed files with 27 additions and 18 deletions
  1. 8 14
      gameduino2/convert.py
  2. 9 0
      gameduino2/imbytes.py
  3. 4 3
      gameduino2/prep.py
  4. 6 1
      go

+ 8 - 14
gameduino2/convert.py

@@ -3,6 +3,7 @@ import array
 import random
 
 from gameduino2.registers import *
+from gameduino2.imbytes import imbytes
 
 def convert(im, dither = False, fmt = ARGB1555):
     """ Convert PIL image to GD2 format, optionally dithering"""
@@ -49,26 +50,19 @@ def convert(im, dither = False, fmt = ARGB1555):
         lut.putdata(range(256))
         palstr = lut.convert("RGBA").tobytes()
         rgba = zip(*(array.array('B', palstr[i::4]) for i in range(4)))
-        """
-        for i,(r,g,b,a) in enumerate(rgba):
-            self.memory[RAM_PAL + 4 * i + 0] = b
-            self.memory[RAM_PAL + 4 * i + 1] = g
-            self.memory[RAM_PAL + 4 * i + 2] = r
-            self.memory[RAM_PAL + 4 * i + 3] = a
-        """
-        data = array.array('B', im.tostring())
+        data = imbytes(im)
         totalsz = 8
     elif fmt == L8:
-        data = array.array('B', im.tostring())
+        data = imbytes(im)
         totalsz = 8
     elif fmt == L4:
-        b0 = im.tostring()[::2]
-        b1 = im.tostring()[1::2]
+        b0 = imbytes(im)[::2]
+        b1 = imbytes(im)[1::2]
         def to15(c):
             if dither:
-                dc = min(255, ord(c) + rnd.randrange(16))
+                dc = min(255, c + rnd.randrange(16))
             else:
-                dc = ord(c)
+                dc = c
             return int((15. * dc / 255))
                 
         data = array.array('B', [(16 * to15(l) + to15(r)) for (l,r) in zip(b0, b1)])
@@ -78,6 +72,6 @@ def convert(im, dither = False, fmt = ARGB1555):
             im = im.convert("1", dither=Image.FLOYDSTEINBERG)
         else:
             im = im.convert("1", dither=Image.NONE)
-        data = array.array('B', im.tostring())
+        data = imbytes(im)
         totalsz = 1
     return (im.size, data)

+ 9 - 0
gameduino2/imbytes.py

@@ -0,0 +1,9 @@
+import array
+
+def imbytes(im):
+    try:
+        bb = im.tobytes()
+    except AttributeError:
+        bb = im.tostring()
+    return array.array('B', bb)
+

+ 4 - 3
gameduino2/prep.py

@@ -13,9 +13,10 @@ import ImageDraw
 
 import gameduino2 as gd2
 import gameduino2.convert
+from gameduino2.imbytes import imbytes
 
 def stretch(im):
-    d = array.array('B', im.tostring())
+    d = imbytes(im)
     # print min(d), max(d)
     r = max(d) - min(d)
     return im.point(lambda x: (x - min(d)) * 255 / r)
@@ -62,8 +63,8 @@ def even(im):
 def extents(im):
     """ find pixel extents of im, as a box """
     w,h = im.size
-    cols = [set(im.crop((i, 0, i + 1, im.size[1])).tostring()) != set(chr(0)) for i in range(w)]
-    rows = [set(im.crop((0, i, im.size[0], i + 1)).tostring()) != set(chr(0)) for i in range(h)]
+    cols = [set(imbytes(im.crop((i, 0, i + 1, im.size[1])))) != set([0]) for i in range(w)]
+    rows = [set(imbytes(im.crop((0, i, im.size[0], i + 1)))) != set([0]) for i in range(h)]
     if not True in cols:
         return (0, 0, 0, 0)
     else:

+ 6 - 1
go

@@ -1,3 +1,8 @@
 sudo rm -rf /usr/local/lib/python2.7/dist-packages/gameduino2/*
 sudo python setup.py install
-gd2asset -f xxx testdata/felix.png,testdata/felix.png,format=L1 testdata/Hobby-of-night.ttf testdata/0.wav
+gd2asset -f xxx \
+  testdata/felix.png,format=RGB332 \
+  testdata/felix.png,format=L4 \
+  testdata/felix.png,format=L1 \
+  testdata/Hobby-of-night.ttf \
+  testdata/0.wav