通过tcp将图像从树莓pi发送到pc机中的程序并在p上显示

2024-04-29 02:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个客户机-服务器程序,其中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()

服务器:

^{pr2}$

执行此程序时,会出现一个错误,说明:

  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

请帮我拿这个。在


Tags: ioimageimport服务器stream客户机pilpi