【问题标题】:How to convert a 3d RGB NumPy array to float without changing the image?如何在不更改图像的情况下将 3d RGB NumPy 数组转换为浮点数?
【发布时间】:2019-11-27 02:18:02
【问题描述】:

我正在尝试保存 RGB 像素形式的图像。不管出于什么原因,当我调用matplotlib.pyplot.imshow 时,图像显示正常。

但是,当我调用 matplotlib.pyplot.imsave 时,我收到一个错误,即数组必须位于 float 中。当我决定将所有值更改为float 时,图像已完全修改。这对我来说毫无意义。

def display_image(pixels, name=""):
    if name:
        plt.imsave(name, array)
    plt.imshow(array)

pixels = im.getdata()
pixelMap = im.load()
npImage = np.array(pixels).reshape(671, 450, 3)
display_image(npImage) ## great!

# things that I tried
display_image(npImage, "image.jpg") ## error, must be floats.

# changes the images
pixels = list(pixels) ## pixels var originally imagecore class
for index in range(len(pixels)):
    pixels[index] = (float(pixels[index][0]), float(pixels[index][1]), float(pixels[index][2]))
display_image(npImage, "monaLisa.jpg") ## works but incorrect image

【问题讨论】:

  • 如果我的问题不好,我将不胜感激。对我来说,这似乎是具体的,并显示了一种尝试。我想我还在学习如何提出一个好问题

标签: python image numpy matplotlib python-imaging-library


【解决方案1】:

在我回答您的实际问题之前,先谈谈您的评论,以及为什么您的问题可能会被否决。你的代码不是minimal, reproducible example,也不能按原样工作。

您缺少所有必要的导入:

from matplotlib import pyplot as plt
import numpy as np
from PIL import Image

你的方法参数之一是错误的:

def display_image(pixels, name=""):    #  <-- I assume, pixels should be array here.
    if name:
        plt.imsave(name, array)
    plt.imshow(array)
    plt.show()                         #  <-- Without, there's no actual output.

im 是什么?最有可能的是,这是一些 PIL 图像。此外,您硬编码了一些值,但没有提供相应的图像。

im = Image.open('someImage.jpg')                   #  <-- Need to load some image at all.
pixels = im.getdata()
pixelMap = im.load()
npImage = np.array(pixels).reshape(671, 450, 3)    #  <-- That only works for images with the specific size
display_image(npImage) ## great!

在最后一个测试用例中,您没有更新npImage

pixels = list(pixels) ## pixels var originally imagecore class
for index in range(len(pixels)):
    pixels[index] = (float(pixels[index][0]), float(pixels[index][1]), float(pixels[index][2]))

#  <-- You updated pixels, but not npImage

display_image(npImage, "monaLisa.jpg")

只有在解决所有这些问题后,才会出现实际错误,您在问题中谈论的是!


现在,你的错误:

Traceback (most recent call last):
  File "[...].py", line 18, in <module>
    display_image(npImage, "image.jpg") ## error, must be floats.
  [...]
ValueError: Image RGB array must be uint8 or floating point; found int32

您的npImageint32 类型,但plt.imsave 需要uint8(值0 ... 255)float(值0.0 ... 1.0)用于写入。

所以,让我们强制执行uint8

npImage = np.array(pixels, dtype=np.uint8).reshape(..., 3)

如上所述添加npImage的必要更新后

pixels = list(pixels) ## pixels var originally imagecore class
for index in range(len(pixels)):
    pixels[index] = (float(pixels[index][0]), float(pixels[index][1]), float(pixels[index][2]))
npImage = np.array(pixels).reshape(..., 3)                              #  <-- This line
display_image(npImage, "monaLisa.jpg") ## works but incorrect image

出现以下错误:

Traceback (most recent call last):
  File "[...].py", line 25, in <module>
    display_image(npImage, "monaLisa.jpg") ## works but incorrect image
  [...]
ValueError: Floating point image RGB values must be in the 0..1 range.

您尚未将您的 float 值缩放到 0.0 ... 1.0。所以,我们只除以255

npImage = np.array(pixels).reshape(..., 3) / 255

我无法重现您的“完全修改的图像”(由于可能缺少代码片段),但这很可能也是由于未缩放的 float 值,并且您可能看到了剪辑。 (图像几乎是白色的吗?)

希望有帮助!

【讨论】:

    猜你喜欢
    • 2011-12-07
    • 2021-05-08
    • 2018-08-22
    • 1970-01-01
    • 2018-04-19
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多