将 RGB 数组转换为 PIL 图像 [英] convert RGB arrays to PIL image
本文介绍了将 RGB 数组转换为 PIL 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我正在创建一个 RGB 数组 (256 * 256 * 3),我需要显示它.我无法从 RGB 阵列创建 PIL 图像.我写了这段代码来解释:
In my code, I am creating a RGB array (256 * 256 * 3) and I need to show it. I am having trouble creating a PIL image from a RGB array. I wrote this code to explain:
import numpy as np
from PIL import Image
image = Image.open('img_test.png')
image.thumbnail((256, 256))
image = image.convert("RGB")
image = np.asarray(image, dtype=np.float32) / 255
PIL.Image.fromarray(image, "RGB").show()
我正在找回这张图片:
如果我正在使用
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()
然后我得到这张图片:
这条线我做错了什么?
PIL.Image.fromarray(image, "RGB").show()
推荐答案
您希望 PIL 处理 32 位浮点 RGB 图像,但它无法处理 - 请参阅 这里.
You are expecting PIL to handle a 32-bit floating point RGB image, which it cannot - see here.
它可以处理,其中包括:
It can handle, amongst others:
- RGB 为三个 8 位整数值 (RGB888),或
- 灰度 float32.
这篇关于将 RGB 数组转换为 PIL 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文