implement reading image data
Stefan Schuermans

Stefan Schuermans commited on 2017-05-26 18:05:03
Showing 3 changed files, with 24 additions and 6 deletions.

... ...
@@ -1,6 +1,8 @@
1 1
 import socket
2 2
 import struct
3 3
 
4
+from PIL import Image
5
+
4 6
 from mapping import Mapping
5 7
 
6 8
 
... ...
@@ -53,17 +55,26 @@ class Distributor(object):
53 55
 
54 56
     def data_image(self, image):
55 57
         """set image data"""
58
+        # get image size
59
+        (width, height) = image.size
56 60
         # collect pixels from image and assemble message in buffer
57 61
         clr = [0] * Mapping.CHANNELS
58 62
         data = []
59 63
         for output_pixel_coords in self._pixel_coords:
60
-            for pixel_coord in output_pixel_coords:
61
-                if pixel_coord is None:
62
-                    pix = clr
64
+            for x_y in output_pixel_coords:
65
+                if x_y is None:
66
+                    pix = clr # pixel coordinates not known -> cleared
67
+                else:
68
+                    (x, y) = x_y
69
+                    if x < 0 or x >= width or y < 0 or y >= height:
70
+                        pix = clr # outside of image -> cleared
63 71
                     else:
64
-                    pix = (128, 128, 0) # TODO
72
+                        # get pixel from image
73
+                        pix = image.getpixel(x_y)
74
+                # add pixel to pixel data
65 75
                 data += [self._mappings[channel].table[pix[channel]]
66 76
                                         for channel in range(Mapping.CHANNELS)]
77
+        # build UDP message from pixel data
67 78
         self._buffer = self._udp_hdr + "".join(data)
68 79
 
69 80
     def send(self, socket):
... ...
@@ -3,6 +3,8 @@
3 3
 import sys
4 4
 import time
5 5
 
6
+from PIL import Image
7
+
6 8
 import pyetherpix
7 9
 
8 10
 
... ...
@@ -11,13 +13,17 @@ def main(argv):
11 13
         print >>sys.stderr, "usage: %s <config.etp>" % argv[0]
12 14
         return 2
13 15
     config_file = argv[1]
16
+    # create display
14 17
     display = pyetherpix.Display(config_file)
15 18
     (width, height) = display.get_size()
16 19
     print "width %u, height %u" % (width, height)
20
+    # prepare "on" image (all white)
21
+    on = Image.new("RGB", (width, height), "white")
22
+    # blink
17 23
     print "blink"
18 24
     for i in range(5):
19 25
         print "on"
20
-        display.data_image("ON") # TODO
26
+        display.data_image(on)
21 27
         display.send()
22 28
         time.sleep(0.5)
23 29
         print "off"
... ...
@@ -25,6 +31,7 @@ def main(argv):
25 31
         display.send()
26 32
         time.sleep(0.5)
27 33
     print "done"
34
+    # close display
28 35
     display.close()
29 36
     return 0
30 37
 
... ...
@@ -8,6 +8,6 @@ setup(name='pyetherpix',
8 8
       author_email='stefan@schuermans.info',
9 9
       license='LGPLv3',
10 10
       packages=['pyetherpix'],
11
-      install_requires=[],
11
+      install_requires=['pillow'],
12 12
       zip_safe=False)
13 13
 
14 14