我如何从内存中读取Pyplot图像而不是文件?



这段代码是工作的,但我想避免使用临时文件,我已经尝试了不同的方法,但不工作。有人知道怎么做吗?或者临时文件是强制性的?

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
...
data = Image.fromarray(np.array(image))
data.save('output/temp.png')
img = plt.imread('output/temp.png')
...

完整功能:

data = pickle.load(datafile)
# IMAGES
image = data['img']
# LABELS
label = data['label']
# SHOW
data = Image.fromarray(np.array(image))
data.save('output/temp.png')
img = plt.imread('output/temp.png')
# Create a figure. Equal aspect so circles look circular
fig, ax = plt.subplots(1)
ax.set_aspect('equal')
# Show the image
ax.imshow(img)
# Now, loop through coord arrays, and create a circle at each x,y pair
for xx, yy in label:
circle = plt.Circle((xx, yy), 10)
ax.add_patch(circle)
# Show the image
plt.show()

因为我想在加载了numpy的图像中画圆:使用Matplotlib和NumPy在图像上绘制圆

但是我只是想知道如何避免使用时态文件。这可能吗?

你问错了问题
我想你的意思是如何表现。因为读取它是为了将它从文件导入到内存。所以你不能从内存中读取它,因为它已经存在了。
你只需要使用plt.imshow(data, *args)

根据Matplotlib在imread上的文档,它的功能类似于Image.open

这意味着你应该已经能够传递data到你需要的东西,而不是img,因为它们都是Image对象。

同时,如果可能的话,你应该解释你想做更多的事情。当前的代码太模糊了,所以我只能给出这些。

最新更新