-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_send.py
63 lines (52 loc) · 1.4 KB
/
image_send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from PIL import Image
from PIL import ImageColor
import serial
import sys
#current data format sends 2 bytes per pixel
#1152 pixels for 2304 bytes of data sent
#default baud_rate is 9600
#color components are converted from 8bit to 4bit
#so use wide range in color values
#defaults
img_name = 'image.bmp'
ser_port = '/dev/ttyUSB0'
#parse command line
if (len(sys.argv) > 1):
if (sys.argv[1] == 'help'):
print "image file name then serial port" # Option "help"
exit()
elif (len(sys.argv) == 3):
img_name = sys.argv[1]
ser_port = sys.argv[2]
else:
print "Error: bad args"
exit()
#tests before sending
try:
img = Image.open(img_name)
except IOError:
print "Error: Image fine not found"
exit()
try:
ser = serial.Serial(ser_port)
except serial.SerialException:
print "Error: Serial port not found"
exit()
if (img.height != 24 or img.width != 48):
print "Error: Wrong image size"
exit()
#wait for start character before sending
print "waiting"
while ser.read() <> 'g':
pass
print "start"
for x in range(48):
for y in range(24):
r, g, b = img.getpixel((x,y))
#RGBA uses 8b per, G35 only uses 4b
temp_MSB = int(b / 16) #the upper 4 bits are not used
temp_LSB = int(g / 16) * 16 + int(r / 16)
ser.write(chr(temp_MSB))
ser.write(chr(temp_LSB))
print "done"
#there is a "done" byte sent by the sign, ignoring for now