【问题标题】:Numpy array of tuples to PIL imageNumpy 元组数组到 PIL 图像
【发布时间】:2017-12-22 15:46:19
【问题描述】:

我目前有一个 numpy 数组或 RBG 元组,我想将其转换为 PIL 图像并保存。目前我正在做以下事情:final = Image.fromarray(im_arr, mode='RGB')。这里im_arr(R, G, B) 形式的元组的numpy 数组。它似乎最终在创建图像时保持元组分开,如下所示。

【问题讨论】:

    标签: python arrays image numpy python-imaging-library


    【解决方案1】:

    尝试使用这些功能:

    import numpy
    import Image
    
    def PIL2array(img):
        return numpy.array(img.getdata(),
                        numpy.uint8).reshape(img.size[1], img.size[0], 3)
    
    def array2PIL(arr, size):
        mode = 'RGBA'
        arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2])
        if len(arr[0]) == 3:
            arr = numpy.c_[arr, 255*numpy.ones((len(arr),1), numpy.uint8)]
        return Image.frombuffer(mode, size, arr.tostring(), 'raw', mode, 0, 1)
    
    def main():
        img = loadImage('foo.jpg')
        arr = PIL2array(img)
        img2 = array2PIL(arr, img.size)
        img2.save('out.jpg')
    
    if __name__ == '__main__':
        main()
    

    信用:http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/

    其他信息:http://www.imagexd.org/tutorial/lessons/0_images_are_arrays.html

    如果它不起作用,也许你的数组没有合适的形状。

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 2018-11-22
      • 2017-06-25
      • 1970-01-01
      • 2019-02-26
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多