gpt4 book ai didi

java - 将路径图像从java发送到python

转载 作者:行者123 更新时间:2023-12-02 04:05:38 28 4
gpt4 key购买 nike

我正在尝试将图像路径从java发送到python,该路径已发送,但是当我想打开它时,它想要打开

java代码

 public static void main(String[] args) {
try {
// connect to python

ServerSocket server = new ServerSocket(8080);
Socket client = server.accept();
Process p = Runtime.getRuntime().exec("C:\\Python27\\Python C:\\Users\\acer\\Desktop\\py\\a.py");

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
out.println("C:\Users\acer\Desktop\py\6.bmp");
client.close();
// System.exit(0);
} catch (IOException ex) {

}

}

Python代码

import socket
import random
import os, sys
import numpy
from PIL import Image
HOST = "localhost"
PORT = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
data = sock.recv(1024)

d=str(data)
img = d
print "d",d
im = Image.open(data)
#print(im.size, im.format)
row, col = im.size
## some code.............

它给出了这个

d 6.bmp
Traceback (most recent call last):
File "C:\Users\acer\Desktop\py\a.py", line 18, in <module>
im = Image.open(data)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2634, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: '6.bmp\r\n'

我不知道\r\n是什么,我只是想发送图像路径以在Python中打开图像

最佳答案

当你像这样写入数据时:

   out.println("C:\Users\acer\Desktop\py\6.bmp");

您正在添加行尾分隔符。

当你读取这样的数据时:

   data = sock.recv(1024)
d=str(data)

您将包含在字符串中读取的所有数据...包括行分隔符。这就是为什么错误消息显示:

   IOError: [Errno 22] invalid mode ('rb') or filename: '6.bmp\r\n'

看到\r\n了吗?这是行分隔符。

您需要修剪文件名中的行分隔符...

   im = Image.open(data.strip())

关于java - 将路径图像从java发送到python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56714997/

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