gpt4 book ai didi

python - 从套接字客户端向套接字服务器发送PIL图像对象

转载 作者:行者123 更新时间:2023-12-03 11:55:38 25 4
gpt4 key购买 nike

我想从套接字客户端向套接字服务器发送PIL图像对象。由于无法直接通过套接字发送图像对象,因此我已使用numpy将其转换为数组,然后尝试将数组发送到套接字服务器。

这是我的服务器程序(time只是为了在每次保存文件时获得文件的新名称):

import socket
import time
import os
import sys
import Image
import ImageGrab
import numpy

sd='C:\Users\Saurabh\Desktop'
s=socket.socket()
host=socket.gethostname() #'WarMachine'
port=12300
s.bind((host,port))

s.listen(9)

while True:
a,addr=s.accept()
print "got connection from",addr
a.send("1")
imgarr=a.recvfrom(4096)
img=Image.fromarray(numpy.uint8(imgarr))
sec=time.time()
lc=time.localtime(sec)
t=time.asctime(lc)
print t
strng=""+t[8:10]+"_"+t[11:13]+"_"+t[14:16]+"_"+t[17:19]
saveas=os.path.join(sd, 'ScreenShot_'+strng+'.jpg')
img.save(saveas)
print "run successful server"
a.close()

上面提到的客户端程序错误是:
got connection from ('192.168.1.9', 50903)
Traceback (most recent call last):
File "C:/Users/Saurabh/PycharmProjects/MajorProject/srvr.py", line 26, in <module>
img=Image.fromarray(numpy.uint8(imgstr))
ValueError: invalid literal for long() with base 10: ''

这是客户端程序:
import os
import sys
import time
import Image
import ImageGrab
import subprocess
import socket
import numpy

s=socket.socket()
host="WarMachine" #use same hostname as server or else it wont work
port=12300
s.connect((host,port))

def shtDwn():
time = 10
subprocess.call(["shutdown.exe", "/s"])

def screenShot():
sd='C:\Users\Saurabh\Desktop'

img=ImageGrab.grab()
imgarr=numpy.asarray(img)
s.send(imgarr)
print "run successful screentest1"


ip=s.recv(1024)
#this receives 1 sent from server to activate snapshot function

if (ip=="1"):
for i in range(0,10):
screenShot()
time.sleep(5)
elif(ip=="2"):
shtDwn()
else:
print"Wrong Input"

我在上面的客户端程序中得到的错误是:
run successful screentest1
Traceback (most recent call last):
File "C:/Users/Saurabh/PycharmProjects/MajorProject/ScreenTest1.py", line 42, in <module>
screenShot()
File "C:/Users/Saurabh/PycharmProjects/MajorProject/ScreenTest1.py", line 27, in screenShot
s.sendto(imgarr,("WarMachine",12300))
socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host

顺便说一句,在使用下面的程序之前,我已经保存了图像,但是我不知道上面的程序出了什么问题。

没有客户端和服务器的先前程序:
import os
import sys
import Image
import time
import ImageGrab
import numpy


sd='C:\Users\Saurabh\Desktop'

img=ImageGrab.grab()
imgarr=numpy.asarray(img)
print imgarr
img2=Image.fromarray(numpy.uint8(imgarr))
sec=time.time()
lc=time.localtime(sec)
t=time.asctime(lc)
print t
strng=""+t[8:10]+"_"+t[11:13]+"_"+t[14:16]+"_"+t[17:19]
saveas=os.path.join(sd, 'ScreenShot_'+strng+'.jpg')
#img.save(saveas)
img2.save(saveas)
print "run successful"

最佳答案

好吧,我也一直在研究这个问题,并发现您可以通过将图像转换为numpy数组来通过套接字发送图像。

这是示例代码-

#Host Side script
#Host will be sending the screen to client.

import numpy as np
import socket
from PIL import ImageGrab

filename = 'host_data.npy'

print("started.\nListening for connections...")
s = socket.socket()
s.bind((socket.gethostname(), 1234))
s.listen(0)

conn, addr = s.accept()
print('connection established.')

img = np.array(ImageGrab.grab())
np.save(filename, img)
file = open(filename, 'rb')
data = file.read(1024)
conn.send(data)

while data != b'':
data = file.read(1024)
conn.send(data)

print('data has been successfully transmitted.')
#This is client side script

import socket
import numpy as np
import cv2

s = socket.socket()
s.connect((socket.gethostname(), 1234))
print("connected.")

filename = 'client_data.npy'
file = open(filename, 'wb')

data = s.recv(1024)
file.write(data)

while data != b'':
data = s.recv(1024)
file.write(data)
file.close()

o_file = np.load(filename)
o_file = cv2.cvtColor(o_file, cv2.COLOR_BGR2RGB)
cv2.imshow('transferred file', o_file)
cv2.waitKey(0)

我所做的就是,使用 npy将numpy数组保存在 np.save(filename)文件中,并以字节格式发送 npy文件的内容。然后,客户端接收到内容并制作了另一个 npy类型的文件并加载了数组。

关于python - 从套接字客户端向套接字服务器发送PIL图像对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29119417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com