简体   繁体   中英

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). To do that, I use the library 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. 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 .

So, I print the shape of the initial png image which worked fine, to see the difference(s):

>>> 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:

[[[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 ! 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. 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) ).

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. Not taking the scatterplot into account you want to plot as well.

What happens if you try to plot only a part of the image? Lets say the upper left quarter?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM