我正在尝试从Matplotlib图中获取一个numpy数组图像,我现在正在通过保存到文件,然后重新读取文件来实现它,但我觉得必须有更好的方法.这就是我现在正在做的事情:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.gca()
ax.text(0.0,0.0,"Test", fontsize=45)
ax.axis('off')
canvas.print_figure("output.png")
image = plt.imread("output.png")
Run Code Online (Sandbox Code Playgroud)
我试过这个:
image = np.fromstring( canvas.tostring_rgb(), dtype='uint8' )
Run Code Online (Sandbox Code Playgroud)
从我发现的一个例子,但它给我一个错误,说'FigureCanvasAgg'对象没有属性'渲染器'.
我正在尝试在 Rust 中实现 VXI11 协议(与示波器、电源等仪器进行通信),并且我成功发送了广播 UDP 消息,但没有收到响应。这是我用来设置 UDP 套接字的代码:
let socket:UdpSocket = UdpSocket::bind("0.0.0.0:0")?;
socket.set_read_timeout(Some(Duration::new(5, 0)))?;
socket.set_broadcast(true)?;
socket.connect(("255.255.255.255", port))?;
println!("Connected on port {}", port);
println!("Broadcast: {:?}", socket.broadcast());
println!("Timeout: {:?}", socket.read_timeout());
Run Code Online (Sandbox Code Playgroud)
我还在绑定调用中尝试过“255.255.255.255:0”和“192.168.2.255:0”,但没有成功。这是我用来接收响应的代码。
let call:Vec<u8> = self.packer.get_buf()?;
println!("Sending call, {} bytes", call.len());
match self.socket.send(&call) {
Ok(n) => {
if n != call.len() {
return Err(Error::new(ErrorKind::Other, "Sent the wrong number of bytes"))
}
else {
// Do nothing because we sent the number of bytes we expected to send
}
},
Err(e) => return …Run Code Online (Sandbox Code Playgroud)