How to convert a PIL Image into a numpy array?

后端 未结 8 2229
不知归路
不知归路 2020-11-22 17:12

Alright, I\'m toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL\'s Pixel

相关标签:
8条回答
  • 2020-11-22 17:45

    You need to convert your image to a numpy array this way:

    import numpy
    import PIL
    
    img = PIL.Image.open("foo.jpg").convert("L")
    imgarr = numpy.array(img) 
    
    0 讨论(0)
  • 2020-11-22 17:53
    def imshow(img):
        img = img / 2 + 0.5     # unnormalize
        npimg = img.numpy()
        plt.imshow(np.transpose(npimg, (1, 2, 0)))
        plt.show()
    

    You can transform the image into numpy by parsing the image into numpy() function after squishing out the features( unnormalization)

    0 讨论(0)
提交回复
热议问题