I am trying to send a few pics via socket from server to client.
I get this error on the client side, and I am not sure how to fix it.
SERVER
I get this error on the client side, and I am not sure how to fix it.
Error:Traceback (most recent call last):
File "C:/Users/PycharmProjects/client-server/client.py", line 34, in <module>
im = Image.open(fp)
File "C:\Users\PycharmProjects\client-server\venv\lib\site-packages\PIL\Image.py", line 2818, in open
raise IOError("cannot identify image file %r" % (filename if filename else fp))
OSError: cannot identify image file <_io.BufferedReader name='test.png'>
here are both of my codesSERVER
import cv2 import time import socket import glob import os host = "127.0.0.1" port = 5000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(5) print("server started...") path1 = (r"C:\Users\Desktop\opencvpics") def readFileImages(): st = os.path.join(path1, "*.png") print(st) return glob.glob(st) list1=readFileImages() print(list1, "list1......") while True: c, addr = s.accept() print(f"connection from {addr} has been established !") c.send(bytes("welcome to the server".encode())) for pics in list1: f=open(pics, 'rb') while True: veri=f.read() if not veri: break s.send(veri) f.close() data = s.recv(4096) data_arr = pickle.loads(data) newrow = numpy.asarray(data_arr) myoutput = numpy.vstack([myoutput, newrow]) s.close()
import socket from PIL import Image import pickle host = "127.0.0.1" port = 5000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) msg=s.recv(1024) print(msg.decode()) fname="test.png" fp = open(fname,'rb') # image while True: strng = s.recv(1024) if not strng: break fp.write(strng) im = Image.open(fp) T= im.size data=pickle.dumps(T) s.send(data) fp.close()In the client code, I tried to move 'fp.close()'before 'im=Image.open(fp)' but it would then complain about dealing with a closed file.