【问题标题】:Convert numpy.ndarray into imageio.core.util.Image将 numpy.ndarray 转换为 imageio.core.util.Image
【发布时间】:2020-03-27 22:58:09
【问题描述】:

我尝试使用 cv2 调整输入图像的大小,但在将调整后的 np 数组转换为原始格式时遇到问题。

image = imageio.imread(filename) #<class 'imageio.core.util.Image'>
image_re = cv2.resize(image, (256, 256)) #<class 'numpy.ndarray'>
#convert into <class 'imageio.core.util.Image'> here

提前致谢。

【问题讨论】:

    标签: python image-resizing cv2 python-imageio


    【解决方案1】:

    imageio.core.util.Image 只是具有元属性的 np.ndarray 的子类。你为什么想回到它?

    对您的目标进行一些进一步的解释可能有助于澄清问题。

    【讨论】:

    • 感谢您的回复。我希望你能帮助我。我的问题是,输入图像是 512x512,但我需要它的大小为 256x256。所以,我使用 cv2 resize 函数将图像调整为所需的大小,但是这个函数输出一个 numpy.ndarray 并且我需要将它返回到 imageio.core.util.Image 以进行下一步,因为代码需要这种类型。也许这是一个简单的问题,但我是 python 的新手,我仍然无法在 google 中找到方法
    • 下一步是什么? imageio 用于读取/写入图像、视频等,所以一旦你已经有了信息,你就可以使用 imageio.imwrite() 将其写入磁盘
    【解决方案2】:

    使用下面的代码

    width = height = 256
    dim = (width, height)
    
    file_name = "your file address here"    
    image = imageio.imread(file_name )
    resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
    image2 = imageio.core.util.Array(resized)
    

    你需要使用的是image2

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 2018-11-21
      • 2014-08-11
      • 1970-01-01
      • 2022-06-27
      • 2015-07-21
      • 2019-08-31
      • 2017-11-27
      相关资源
      最近更新 更多