Send image over tcp from raspberry pi to program in pc and display it on pc

0

I am having a client - server program in which raspberry pi has the client and pc contains the server. I want to send image captured by pi to server and want to process it there. The code is given below. Client side :

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()

Server:

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()

When this program is executed, an error occurs stating :

  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

Please help me with this.

python
python-2.7
image-processing
tcp
raspberry-pi3
asked on Stack Overflow Mar 18, 2019 by afnaS

1 Answer

0

client:

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()

server:

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()


There were some mistakes in your code have corrected it this should work now.

answered on Stack Overflow Mar 28, 2020 by Madhuraank B

User contributions licensed under CC BY-SA 3.0