[英]How to convert a 3d RGB NumPy array to float without changing the image?
我正在尝试保存 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
在我回答您的实际问题之前,先就您的评论说几句话,以及为什么您的问题可能会被否决。 您的代码不是最小的、可重现的示例,也不能按原样运行。
您缺少所有必要的导入:
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
您的npImage
是int32
,但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
值造成的,您可能看到了裁剪。 (图像几乎是白色的吗?)
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.