【问题标题】:Import image as RGB pixels of "imageio.core.util.Image" type in Python在 Python 中将图像导入为“imageio.core.util.Image”类型的 RGB 像素
【发布时间】:2019-02-22 08:42:13
【问题描述】:

我想导入一些具有拆分 RGB 值的图像,对于某些图像它可以工作,而对于其他一些图像,输出只给出一个像素的 RGB 值。

这是代码适用的图像:

if os.path.isfile(location1):  
    image = imageio.imread(location1)
print("Type : ", type(image[0][0]))
## Type : imageio.core.util.Image
input : image
output: Image([[[167, 126,  94],
        [210, 184, 147],
        [245, 234, 188],
        ...,

这是代码不起作用的图像。

if os.path.isfile(location2):  
    image = imageio.imread(location2)
print("TYpe : ", type(image[0][0]))
## TYpe : <class 'numpy.uint8'>
input: image
output: Image([[81, 78, 74, ..., 72, 71, 69],
      [74, 71, 67, ..., 70, 70, 68],
      [61, 58, 55, ..., 65, 65, 64],
   ...,

(我将不胜感激)

【问题讨论】:

    标签: python image imread python-imageio


    【解决方案1】:

    您加载的第二张图像似乎只是一张灰度图像(即不是带有颜色的图像,而只是带有灰度级别的图像)。要将其转换为 RGB,请尝试以下操作:

    from skimage import color
    img = color.gray2rgb(your_image)
    

    另外,由于转换为RGB只是将每个灰度值重复3次,您可以使用this snippet

    import numpy as np
    rgb = np.stack((your_image, your_image, your_image), axis=-1)
    

    【讨论】:

    • 感谢您的宝贵时间,我运行了代码,但仍未解决并给出相同的输出。
    • 对不起,这应该是gray2rgb
    • 非常感谢,我不知道会因为图片的灰色而出现这种情况。
    猜你喜欢
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2020-01-05
    • 2012-08-25
    • 2020-08-13
    • 2015-07-11
    相关资源
    最近更新 更多