使用Imageio -typeError:Imaplotlib:图像数据无效的形状(3,408,612)

发布于 2025-01-31 04:54:36 字数 1059 浏览 3 评论 0原文

我想使用matplotlib进行图像处理。 代码

import matplotlib.pyplot as plt

img = io.imread('/content/drive/MyDrive/fishsampleimages/fish1.jpg')

# io.imwrite("readfish1.jpg", img)
plt.imshow(img)

这是我收到错误和输出的

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-4abe12c2ad85> in <module>()
      5 
      6 # io.imwrite("readfish1.jpg", img)
----> 7 plt.imshow(img)

5 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in set_data(self, A)
    697                 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
    698             raise TypeError("Invalid shape {} for image data"
--> 699                             .format(self._A.shape))
    700 
    701         if self._A.ndim == 3:

TypeError: Invalid shape (3, 408, 612) for image data

,如

I want to do image processing using matplotlib.
Here is the code

import matplotlib.pyplot as plt

img = io.imread('/content/drive/MyDrive/fishsampleimages/fish1.jpg')

# io.imwrite("readfish1.jpg", img)
plt.imshow(img)

I got the error and output as below

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-4abe12c2ad85> in <module>()
      5 
      6 # io.imwrite("readfish1.jpg", img)
----> 7 plt.imshow(img)

5 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in set_data(self, A)
    697                 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
    698             raise TypeError("Invalid shape {} for image data"
--> 699                             .format(self._A.shape))
    700 
    701         if self._A.ndim == 3:

TypeError: Invalid shape (3, 408, 612) for image data

enter image description here

How should I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2025-02-07 04:54:36

问题似乎在您的源文件中。标准图像将带有(高度,宽度,num_colors)的形状,因此,当您的情况下,

img.shape

您应该得到形状的结果

(612,408,3)

,形状似乎是向后的。

TypeError: Invalid shape (3, 408, 612) for image data

要么尝试另一个图像,也可以在显示图像之前重塑图像

img_reshaped = img.reshape(612,408,3)
plt.imshow(img_reshaped)

可以正确显示您的图像,否则您必须转到图像源并确保正确编写的图像。

The problem seems to be in your source file. A standard image would load with a shape of (height, width, num_colors) so when you do

img.shape

you should get a result of

(612,408,3)

in your case, the shape seems to be backwards.

TypeError: Invalid shape (3, 408, 612) for image data

either try with another image or you can reshape the image before showing it

img_reshaped = img.reshape(612,408,3)
plt.imshow(img_reshaped)

This may show your image correctly, else you would have to go to the source of the image and make sure it is written correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文