Reading bmp files in Python

前端 未结 7 1305
北荒
北荒 2021-02-19 01:41

Is there a way to read in a bmp file in Python that does not involve using PIL? PIL doesn\'t work with version 3, which is the one I have. I tried to use the Image object from g

7条回答
  •  感情败类
    2021-02-19 01:52

    Use pillow for this. After you installed it simply import it

    from PIL import Image
    

    Then you can load the BMP file

    img = Image.open('path_to_file\file.bmp')
    

    If you need the image to be a numpy array, use np.array

    img = np.array(Image.open('path_to_file\file.bmp'))
    

    The numpy array will only be 1D. Use reshape() to bring it into the right shape in case your image is RGB. For example:

    np.array(Image.open('path_to_file\file.bmp')).reshape(512,512,3)
    

提交回复
热议问题