Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

debugging - Writing a numpy array to single band image

I previously used fromarray to create stacked image using three numpy arrays:

  from PIL import Image
    import numpy as np
    rgbArray = np.zeros((4224,2560,3), 'uint8')
    rgbArray[..., 0] = layerA*256
    rgbArray[..., 1] = LayerB*256
    rgbArray[..., 2] = LayerC*256
    img = Image.fromarray(rgbArray)
    img.save('stack_n.jpeg')

I am trying to adopt this approach for one numpy array as below:

gsArray = np.zeros((4224,2560,1), 'uint8')
gsArray = layer4_norm*256
layer4t = Image.fromarray(gsArray)
layer4t.save('layer4t.jpeg')

but it produces the following error. Please let me know how I can resolve it.

AttributeError                            Traceback (most recent call last)
<ipython-input-72-bcfbe3849ecb> in <module>()
      1 gsArray = np.zeros((4224,2560,1), 'uint8')
      2 gsArray = layer4_norm*256
----> 3 layer4t_normt = Image.fromarray(gsArray)
      4 layer4t.save('layer4t.jpeg')

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in fromarray(obj, mode)
   2668     .. versionadded:: 1.1.6
   2669     """
-> 2670     arr = obj.__array_interface__
   2671     shape = arr["shape"]
   2672     ndim = len(shape)

AttributeError: 'list' object has no attribute '__array_interface__'
question from:https://stackoverflow.com/questions/65849260/writing-a-numpy-array-to-single-band-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...