[英]When showing an image with plt.show() -> MemoryError : Unable to allocate array with shape (3600, 7200, 4) and data type float32
I am trying to plot an image with some things plotted on it (like points, axes, etc).我正在尝试 plot 一个图像,上面绘制了一些东西(如点、轴等)。 To do that, I use the library matplotlib.pyplot
.为此,我使用库matplotlib.pyplot
。 Here is the (reduced) code I use:这是我使用的(简化的)代码:
fig = plt.figure(figsize=(18, 18))
ax = fig.add_subplot(111)
img = plt.imread(stim_dir + image_name)
ax.imshow(img, extent=[-np.pi, np.pi, -np.pi / 2, np.pi / 2])
ax.scatter(*zip(*points), marker='^', s=1, c='red', alpha=0.4)
plt.xlim((-np.pi, np.pi))
plt.title(file_name)
plt.show()
Now, the code works fine when I use a png image, everything is perfect and all.现在,当我使用 png 图像时,代码可以正常工作,一切都很完美。 But I also have bmp ones to plot and there it doesn't work, cannot read a bitmap, but no worries I can convert it to a png one, However: these new png images doesn't work either and I get the error : MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32
.但是我也有到 plot 的 bmp 文件,但它不起作用,无法读取 bitmap,但不用担心我可以将其转换为 png 文件,但是:这些新的 png 图像也不起作用,我得到错误: MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32
。
So, I print the shape of the initial png image which worked fine, to see the difference(s):因此,我打印了工作正常的初始 png 图像的形状,以查看差异:
>>> print(img.shape)
(2160, 4320, 3)
My guess is that the third dimension should be of 3 (for RGB) instead of 4 (which may be ARGB), so I print the img array with the "new" not working image, which gives me something like:我的猜测是第三维应该是 3(对于 RGB)而不是 4(可能是 ARGB),所以我用“新”不工作的图像打印 img 数组,这给了我类似的东西:
[[[0.46666667 0.46666667 0.46666667 1. ]
[0.46666667 0.46666667 0.46666667 1. ]
[0.46666667 0.46666667 0.46666667 1. ]
...
I see after checking, that every 4th number is equal to 1, so that is neat, I just have to delete it, it doesn't carry important information !查了一下,发现每4个数就等于1,好整齐,我只需要删掉它,它没有携带重要信息! I use img = np.delete(img, 3, 2)
and it works well, I get the new shape of (3600, 7200, 3)
with the values of each pixel like I want.我使用img = np.delete(img, 3, 2)
并且效果很好,我得到了(3600, 7200, 3)
的新形状以及我想要的每个像素的值。 However, it STILL doesn't work and STILL gives me the same error as before: MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32
, it seems like it doesn't take the new img array... (Obviously I do the delete just after the instruction img = plt.imread(stim_dir + image_name)
).但是,它仍然不起作用,并且仍然给我与以前相同的错误: MemoryError: Unable to allocate array with shape (3600, 7200, 4) and data type float32
,似乎它不采用新的 img 数组...(显然我在指令img = plt.imread(stim_dir + image_name)
之后执行删除)。
So, do I miss something obvious?那么,我错过了一些明显的东西吗? What could I do to make it work?我该怎么做才能让它发挥作用?
Sorry, not really a solution:对不起,不是真正的解决方案:
Apparently matplotlib is unable to allocate enough memory to plot all your datapoints, in the end you have 3600*7200*4 = 103.680.000 of them which is quite a number.显然 matplotlib 无法将足够的 memory 分配给 plot 你所有的数据点,最后你有 3600*78000*4 中的 3600*7800*4。 Not taking the scatterplot into account you want to plot as well.不考虑散点图,你也想 plot 。
What happens if you try to plot only a part of the image?如果您尝试仅对图像的一部分进行 plot 会发生什么? Lets say the upper left quarter?让我们说左上角?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.