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
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