在我的代码中,我创建了一个RGB数组(256 * 256 * 3),我需要展示它。 但我在从RGB数组中创建PIL图像方面遇到了困难。 以下是我编写的代码以进行说明:
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()
我得到了这张图片:
![输入图像描述](https://istack.dev59.com/rylg6.webp)
这行代码有什么问题?
PIL.Image.fromarray(image, "RGB").show()
.astype(np.uint8)
解决了我的问题 - 我的图像大小为 H x W x C。 - virgesmithnp.asarray(img)
时,反转置操作是.transpose(2,0,1)
。 - Sterling