【问题标题】:Combine 3 separate numpy arrays to an RGB image in Python在 Python 中将 3 个单独的 numpy 数组组合为 RGB 图像
【发布时间】:2012-05-13 16:06:52
【问题描述】:

所以我有一组数据,我可以将其转换为 R、G、B 波段的单独 numpy 数组。现在我需要将它们组合起来形成一个 RGB 图像。

我尝试使用“图像”来完成这项工作,但它需要归因于“模式”。

我试着做一个把戏。我会使用 Image.fromarray() 将数组转换为图像,但是当 Image.merge 需要“L”模式图像进行合并时,默认情况下它会达到“F”模式。如果我首先将 fromarray() 中的数组属性声明为“L”,那么所有的 R G B 图像都会失真。

但是,如果我保存图像然后打开它们然后合并,它工作正常。 Image 以“L”模式读取图像。

现在我有两个问题。

首先,我不认为这是一种优雅的工作方式。所以如果有人知道更好的方法,请告诉

其次,Image.SAVE 无法正常工作。以下是我面临的错误:

In [7]: Image.SAVE(imagefile, 'JPEG')
----------------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/media/New Volume/Documents/My own works/ISAC/SAMPLES/<ipython console> in <module>()

TypeError: 'dict' object is not callable

请提出解决方案。

请注意,图像大约是 4000x4000 大小的数组。

【问题讨论】:

    标签: python image image-processing numpy


    【解决方案1】:
    rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3
    

    如果您传递 3 个通道,此代码不会创建 3d 数组。剩余 2 个频道。

    【讨论】:

      【解决方案2】:

      我认为您的失真是由于您将原始图像拆分为各个条带,然后在将其合并之前再次调整大小造成的;

      `
      image=Image.open("your image")
      
      print(image.size) #size is inverted i.e columns first rows second eg: 500,250
      
      #convert to array
      li_r=list(image.getdata(band=0))
      arr_r=np.array(li_r,dtype="uint8")
      li_g=list(image.getdata(band=1))
      arr_g=np.array(li_g,dtype="uint8")
      li_b=list(image.getdata(band=2))
      arr_b=np.array(li_b,dtype="uint8")
      
      # reshape 
      reshaper=arr_r.reshape(250,500) #size flipped so it reshapes correctly
      reshapeb=arr_b.reshape(250,500)
      reshapeg=arr_g.reshape(250,500)
      
      imr=Image.fromarray(reshaper,mode=None) # mode I
      imb=Image.fromarray(reshapeb,mode=None)
      img=Image.fromarray(reshapeg,mode=None)
      
      #merge
      merged=Image.merge("RGB",(imr,img,imb))
      merged.show()
      `
      

      这很好用!

      【讨论】:

        【解决方案3】:
        rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3
        

        还要将浮点数 0 .. 1 转换为 uint8 s,

        rgb_uint8 = (np.dstack((r,g,b)) * 255.999) .astype(np.uint8)  # right, Janna, not 256
        

        【讨论】:

          【解决方案4】:

          将 numpy 数组转换为 uint8,然后再将它们传递给 Image.fromarray

          例如。如果您有 [0..1] 范围内的浮点数:

          r = Image.fromarray(numpy.uint8(r_array*255.999))
          

          【讨论】:

          【解决方案5】:

          我不太明白你的问题,但这里有一个我最近做过的类似事情的例子,似乎它可能会有所帮助:

          # r, g, and b are 512x512 float arrays with values >= 0 and < 1.
          from PIL import Image
          import numpy as np
          rgbArray = np.zeros((512,512,3), 'uint8')
          rgbArray[..., 0] = r*256
          rgbArray[..., 1] = g*256
          rgbArray[..., 2] = b*256
          img = Image.fromarray(rgbArray)
          img.save('myimg.jpeg')
          

          希望对你有帮助

          【讨论】:

          • @IshanTomar - 如果有帮助,您可能希望接受该答案。
          • 如果要将数组保存为图像,则应为“toimage”
          • @IshanTomar 你真的应该接受最有帮助的答案,这是保持 StackOverflow 运行的重要部分
          • 三个点(...)是什么意思?
          • @LeiYang 表示对之前的所有维度进行切片。看到这个:python-reference.readthedocs.io/en/latest/docs/brackets/…
          猜你喜欢
          • 2015-01-11
          • 2018-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-07
          • 2020-12-21
          相关资源
          最近更新 更多