从树莓派通过TCP发送图像到PC中的程序并在PC上显示

时间:2019-03-18 13:02:51

标签: python python-2.7 image-processing tcp raspberry-pi3

我有一个客户端-服务器程序,其中raspberry pi具有客户端,而pc包含服务器。我想将pi捕获的图像发送到服务器,并在那里进行处理。代码如下。 客户端:

import io
import socket
import struct
from PIL import Image

s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.connect(('ip', 12345))
connection = s.makefile('wb')
try:
    camera = picamera.PiCamera()
    camera.resolution = (640, 480)
    camera.start_preview()
    time.sleep(2)
    stream = io.BytesIO()
    camera.capture(stream , format='jpeg')
    connection.write(struct.pack('<L', stream.tell()))
    connection.flush()
    stream.seek(0)
    connection.write(stream.read())
    stream.seek(0)
    stream.truncate()
finally:
    camera.stop_preview()
    connection.close()
    client_socket.close()

服务器:

import io
import socket
import struct
from PIL import Image

s = socket.socket()
host = '' #ip of raspberry pi
port = 12345
s.bind((host, port))

s.listen(5)
connection = s.accept()[0].makefile('rb')
try:
        image_len = struct.unpack('<L',connection.read(struct.calcsize('<L')))[0]
        image_stream = io.BytesIO()
        image_stream.write(connection.read(image_len))
        image_stream.seek(0)
        image = Image.open(image_stream)
        image.show()
        print('Image is verified')

finally:
    connection.close()
    s.close()

执行此程序时,出现错误,指出:

  Traceback (most recent call last):
  File "server.py", line 24, in <module>
    image = Image.open(image_stream)
  File "C:\Users\USER\AppData\Local\conda\conda\envs\cvv\lib\site-packages\PIL\I
mage.py", line 2687, in open
    % (filename if filename else fp))
OSError:

 cannot identify image file <_io.BytesIO object at 0x000000D6

请帮助我。

1 个答案:

答案 0 :(得分:0)

客户端:

import io
import socket
import struct
from PIL import Image
import picamera
import time

s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
ip = '192.168.1.2'
port = 12345
print("ok")
s.connect(('192.168.1.2', 12345))
print("ok")
connection = s.makefile('wb')
print("ok")
try:
    camera = picamera.PiCamera()
    camera.resolution = (640, 480)
    camera.start_preview()
    time.sleep(2)
    stream = io.BytesIO()
    camera.capture(stream , format='jpeg')
    connection.write(struct.pack('<L', stream.tell()))
    connection.flush()
    stream.seek(0)
    connection.write(stream.read())
    stream.seek(0)
    stream.truncate()
finally:
#    camera = picamera.PiCamera()
    camera.stop_preview()
    connection.close()
    client_socket.close()

服务器:

import io
import socket
import struct
from PIL import Image

s = socket.socket()
host = '192.168.1.2' #ip of host
port = 12345
print("waiting")
s.bind((host, port))
print("waiting")
s.listen(5)
connection = s.accept()[0].makefile('rb')
print("waiting")
try:
        image_len = struct.unpack('<L',connection.read(struct.calcsize('<L')))[$
        image_stream = io.BytesIO()
        image_stream.write(connection.read(image_len))
        image_stream.seek(0)
        image = Image.open(image_stream)
        image.show()
        print('Image is verified')

finally:
    connection.close()
    s.close()


您的代码中有一些错误已将其更正,现在应该可以使用。