/
img_tx.py
executable file
·171 lines (146 loc) · 4.94 KB
/
img_tx.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# img_tx.py
#
# Copyright 2014 Balint Seeber <balint256@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import sys, struct, socket, traceback, time
from optparse import OptionParser
import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
def main():
parser = OptionParser(usage="%prog: [options] <destination address> [input image]")
parser.add_option("-d", "--delay", type="int", default=0, help="delay between scanlines (ms) [default=%default]")
parser.add_option("-r", "--repeat", action="store_true", default=False, help="repeat transmission")
parser.add_option("-p", "--port", type="int", default=12345, help="port [default=%default]")
parser.add_option("-v", "--view", action="store_true", default=False, help="view image before transmission")
parser.add_option("-P", "--pipe", action="store_true", default=False, help="use a pipe instead of a socket [default=%default]")
parser.add_option("-B", "--transport-buffer-size", type="int", default=1, help="request transport buffer size [default=%default]")
(options, args) = parser.parse_args()
options.delay /= 1000.0
if len(args) < 1:
if options.pipe:
print "Supply pipe name"
else:
print "Supply destination address"
return
image_filename = None
destination_addr = args[0]
if not options.pipe:
if len(args) > 1:
image_filename = args[1]
idx = destination_addr.find(':')
if idx > -1:
options.port = int(destination_addr[idx+1:])
destination_addr = destination_addr[:idx]
if image_filename is not None:
print "Opening:", image_filename
img = Image.open(image_filename)
#img_arr = mpimg.imread(image_filename)
else:
print "Using Lena"
import scipy.misc as misc
lena = misc.lena()
print lena.shape
size = (lena.shape[0], lena.shape[1])
lena = lena.reshape((lena.shape[0]*lena.shape[1],1))
buf = "".join(map(lambda x: chr(x), lena))
img = Image.frombuffer("L", size, buf, 'raw', "L", 0, 1)
print "Size:", img.size
print "Mode:", img.mode
print "Format:", img.format
img_data = list(img.getdata())
img_arr = np.asarray(img_data)
img_arr = img_arr.reshape(img.size)
if options.view:
imgplot = plt.imshow(img_arr)
plt.show()
MAGIC = 0xFEDCBA98
img_width = img.size[0]
img_height = img.size[1]
bpp = 1
try:
it = iter(img_data[0])
bpp = len(img_data[0])
except:
pass
print "BPP:", bpp
MAGIC_packed = struct.pack("I", MAGIC)
f, s = None, None
if options.pipe:
print "Opening:", destination_addr
f = open(destination_addr, 'w')
print "Opened:", destination_addr
else:
destination = (destination_addr, options.port)
print "Connecting to:", destination
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if options.transport_buffer_size > 0:
print "SO_SNDBUF was", s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, options.transport_buffer_size)
print "SO_SNDBUF is", s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
s.connect(destination)
print "Connected"
try:
cnt = 0
continue_running = True
while continue_running and (cnt == 0 or options.repeat):
sys.stdout.write("Sending image")
sys.stdout.flush()
for y in range(img_height):
scanline_start = y * img_width
scanline = img_data[scanline_start:(scanline_start+img_width)]
if bpp > 1:
scanline = [item for sublist in scanline for item in sublist]
scanline_packed = ("".join(map(lambda x: chr(x), scanline))).replace(MAGIC_packed, MAGIC_packed + struct.pack("i", 0))
buf = struct.pack("Iiiii", MAGIC, img_height, img_width, bpp, y)
buf += scanline_packed
if s:
try:
s.send(buf)
except socket.error, (e, msg):
if e == 104 or e == 32: # Connection reset by peer, Broken pipe
continue_running = False
break
raise
if f:
f.write(buf)
sys.stdout.write(".")
sys.stdout.flush()
if options.delay > 0.0:
time.sleep(options.delay)
print "done!"
cnt += 1
except KeyboardInterrupt:
print
print "Stopped"
except Exception, e:
print "Unhandled exception:", e
print traceback.format_exc()
if s:
s.close()
if f:
f.close()
return 0
if __name__ == '__main__':
main()