Converting 2D Numpy array of grayscale values to a PIL image

前端 未结 2 1579
Happy的楠姐
Happy的楠姐 2020-12-05 07:53

Say I have a 2D Numpy array of values on the range 0 to 1, which represents a grayscale image. How do I then convert this into a PIL Image object? All attempts so far have

2条回答
  •  渐次进展
    2020-12-05 08:00

    If I understood you question, you want to get a grayscale image using PIL.

    If this is the case, you do not need to multiply each pixels by 255.

    The following worked for me

    import numpy as np
    from PIL import Image
    
    # Creates a random image 100*100 pixels
    mat = np.random.random((100,100))
    
    # Creates PIL image
    img = Image.fromarray(mat, 'L')
    img.show()
    

提交回复
热议问题