python - 使用 PIL 显示 RGB 图像的问题

标签 python python-3.x python-imaging-library

在我的代码中,我将图像创建为 RGB 数组 (224 * 224 * 3)。

这就是我的图像表示的样子:

[[[0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  ...,  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.84066298]],, [[0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  ...,  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.84066298]],, [[0.44861878 0.65767956 0.95801105],  [0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  ...,  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.84066298]],, ...,, [[0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138],  ...,  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138]],, [[0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138],...

现在,当我使用时:

plt.imshow(image)
plt.show()

我得到这张图片:

enter image description here

当我使用时:

  image = np.asarray(image * 255, dtype=np.int)
  PIL.Image.fromarray(image, "RGB").show()

我得到:

enter image description here

为什么我无法使用 PIL 显示图像?

最佳答案

您的问题似乎是您创建了一个 np.int 类型的 NumPy 数组(64 位有符号整数),而 PIL 需要 np.uint8 (8 位无符号整数)。将 image = np.asarray(image * 255, dtype=np.int) 更改为 image = np.asarray(image * 255, dtype=np.uint8) 应该可以解决你的问题。

关于python - 使用 PIL 显示 RGB 图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62769919/

相关文章:

python - Django、ForeignKey 关系和 Q 或

python - python 3 中带有条件的 Itertools

python - PIL - 如何为粘贴的图像添加圆角?

Python,运行终端,并在其中执行命令

python - 有序列约束排列 Python

javascript - 美丽汤/正则表达式 : Find specific value from href

python - 如何在内核终端内运行带有库的Python脚本?

Python Pandas - 处理具有嵌套字典(json)值的列

Python - 将两个 Tif 文件附加到特定文件中

python - 如何在 python 中以更快的方式迭代图像像素?