- import base64
- import socket
- import numpy as np
- import cv2
- import datetime
- import os
- ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- ss.bind(("192.168.1.65",5)) #服务器绑定ip
- ss.listen(5) #开始监听
- tcp1,add=ss.accept() #阻塞进程,等待客户端连接
- print('tcp1',tcp1)
- print('客户端连接成功,客户端IP地址',add)
-
- data = tcp1.recv(10240000000) #设置这么大是为了完整接受一个图像的字节串
- print(type(data))
- data = str(data, encoding='utf-8')
- data = base64.b64decode(data.encode('utf-8'))
- data = np.frombuffer(data, np.uint8)
- image = cv2.imdecode(data, cv2.IMREAD_COLOR)
- file_name = datetime.datetime.now().strftime('%Y%m%H%M%S') + '.jpg'
- print(file_name)
- cv2.imwrite(file_name, image)
- # print('data',data)
- tcp1.send('收到了'.encode('utf-8'))
-
-
-
-
-
- import glob
-
- import socket
-
- import cv2
-
- import base64
-
- import time
-
- cs=socket.socket()
-
- cs.connect(('192.168.1.65',5))
-
- image = cv2.imread('output/202310000729.jpg')
-
- data = cv2.imencode('.bmp', image)[1] #图像压缩,变成一维数组
- hh=base64.b64encode(bytes(data))
- data = base64.b64encode(bytes(data)).decode('utf-8') #base64接受的是字节串的,decode将字节串转换位字符串
-
- cs.sendall(data.encode('utf-8')) #发送数据
-
- data1 = cs.recv(1024) #接受1024个字节的数据,超过1024个字节数据则接受不到
-
- data1 = str(data1, encoding='utf-8') #将字节数据转换为字符串
-
- print(data1)
-
参考: