Python: Use PIL to load png file gives strange results

后端 未结 2 1467
误落风尘
误落风尘 2020-12-19 11:58

I took the following png file from VOC2012 dataset.

I used the following simple code:

import numpy as np
from PIL import Image
import matplotlib.pyp         


        
2条回答
  •  别那么骄傲
    2020-12-19 12:03

    The image is in 'P' mode, it has a palette that we can access (with index) and get the RGB values of the colors in the palette and convert them to gray values, for each pixel, the code shown below.

    import numpy as np
    from PIL import Image
    import matplotlib.pyplot as plt
    
    def rgb2gray(R, G, B):
        return 0.2989 * R + 0.5870 * G + 0.1140 * B 
    
    img = Image.open(filename) # filename is the png file in question
    pal = img.getpalette()     # get the palette
    
    arr = np.zeros((img.height, img.width))
    for i in range(arr.shape[0]):
        for j in range(arr.shape[1]):
            idx = img.getpixel((j,i)) # get the index of the pixel in the palette
            R, G, B = pal[3*idx], pal[3*idx+1], pal[3*idx+2] # get the R,G,B values of the pixel
            arr[i,j] = rgb2gray(R, G, B) # convert to grayscale
    
    plt.imshow(arr, cmap='gray')
    

    with the following output

提交回复
热议问题