Python: Use PIL to load png file gives strange results
Asked Answered
C

2

6

I took the following png file from VOC2012 dataset. enter image description here

I used the following simple code:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline

img = Image.open(filename) # filename is the png file in question
img.show()
arr = np.array(img)
plt.imshow(arr, cmap='gray')

It produced the following image:

enter image description here

It is strange since the result is not the gray-scaled image of the original one. The above code is also used in deeplab tensorflow dataset to remove the colormap in the ground-truth images.

Anyone knows why? Many thanks!

Conceptionconceptual answered 3/8, 2018 at 16:7 Comment(4)
It's probably RGB or RGBA, so you likely want to convert it to greyscale after opening Image.open(filename).convert('L')Syrup
@MarkSetchell Thank you Mark. If it is RGBA, why comes the result that the segmentation line becomes white? (255)Conceptionconceptual
I haven't got access to a machine at the minute, but try extracting and viewing/saving the individual channels so you can see what's going on img=Image.open(filename).convert('RGBA') then extract the A channel A = img.getchannel('A') and save/view that.Syrup
I just checked - your image is palettised! Use Image.open(filename).convert('RGB')Syrup
S
6

The problem is that your image is palettised. So, rather than storing a full RGB triplet for each pixel, there is a list of 256 RGB triplets and then at each pixel location it just stores the index into the list - which saves space.

I think you need this:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
#%matplotlib inline

img = Image.open('cycle.png').convert('RGB').convert('L')
img.show()
arr = np.array(img)
plt.imshow(arr, cmap='gray')

enter image description here


In response to your further question in the comments, yes, the palette entry 255 represents E0E0C0. I checked using ImageMagick to show image details and dump the palette, using the following command in the Terminal. See the list line below:

identify -verbose cycle.png

Image: cycle.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 334x500+0+0
  Units: Undefined
  Colorspace: sRGB
  Type: Palette
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
  Channel statistics:
    Pixels: 167000
    Red:
      min: 0  (0)
      max: 224 (0.878431)
      mean: 93.4808 (0.366591)
      standard deviation: 99.7578 (0.391207)
      kurtosis: -1.93947
      skewness: 0.148046
      entropy: 0.854702
    Green:
      min: 0  (0)
      max: 224 (0.878431)
      mean: 96.2489 (0.377447)
      standard deviation: 72.6758 (0.285003)
      kurtosis: -0.989098
      skewness: -0.146725
      entropy: 0.837577
    Blue:
      min: 0  (0)
      max: 192 (0.752941)
      mean: 66.7367 (0.261712)
      standard deviation: 73.2147 (0.287116)
      kurtosis: -1.56751
      skewness: 0.331185
      entropy: 0.854702
  Image statistics:
    Overall:
      min: 0  (0)
      max: 224 (0.878431)
      mean: 85.4888 (0.33525)
      standard deviation: 81.8828 (0.321109)
      kurtosis: -1.56309
      skewness: 0.182395
      entropy: 0.848994
  Colors: 4
  Histogram:
     54389: (  0,  0,  0) #000000 black
     34183: (  0,128,  0) #008000 green
     61143: (192,128,128) #C08080 srgb(192,128,128)
     17285: (224,224,192) #E0E0C0 srgb(224,224,192)
  Colormap entries: 256
  Colormap:
         0: (  0,  0,  0,255) #000000FF black
         1: (128,  0,  0,255) #800000FF maroon
         2: (  0,128,  0,255) #008000FF green
         3: (128,128,  0,255) #808000FF olive
         4: (  0,  0,128,255) #000080FF navy
         5: (128,  0,128,255) #800080FF purple
         6: (  0,128,128,255) #008080FF teal
         7: (128,128,128,255) #808080FF fractal
         8: ( 64,  0,  0,255) #400000FF srgba(64,0,0,1)
         9: (192,  0,  0,255) #C00000FF srgba(192,0,0,1)
        10: ( 64,128,  0,255) #408000FF srgba(64,128,0,1)
        11: (192,128,  0,255) #C08000FF srgba(192,128,0,1)
        12: ( 64,  0,128,255) #400080FF srgba(64,0,128,1)
        13: (192,  0,128,255) #C00080FF srgba(192,0,128,1)
        14: ( 64,128,128,255) #408080FF srgba(64,128,128,1)
        15: (192,128,128,255) #C08080FF srgba(192,128,128,1)
        16: (  0, 64,  0,255) #004000FF srgba(0,64,0,1)
        17: (128, 64,  0,255) #804000FF srgba(128,64,0,1)
        18: (  0,192,  0,255) #00C000FF srgba(0,192,0,1)
        19: (128,192,  0,255) #80C000FF srgba(128,192,0,1)
        20: (  0, 64,128,255) #004080FF srgba(0,64,128,1)
        21: (128, 64,128,255) #804080FF srgba(128,64,128,1)
        22: (  0,192,128,255) #00C080FF srgba(0,192,128,1)
        23: (128,192,128,255) #80C080FF srgba(128,192,128,1)
        24: ( 64, 64,  0,255) #404000FF srgba(64,64,0,1)
        25: (192, 64,  0,255) #C04000FF srgba(192,64,0,1)
        26: ( 64,192,  0,255) #40C000FF srgba(64,192,0,1)
        27: (192,192,  0,255) #C0C000FF srgba(192,192,0,1)
        28: ( 64, 64,128,255) #404080FF srgba(64,64,128,1)
        29: (192, 64,128,255) #C04080FF srgba(192,64,128,1)
        30: ( 64,192,128,255) #40C080FF srgba(64,192,128,1)
        31: (192,192,128,255) #C0C080FF srgba(192,192,128,1)
        32: (  0,  0, 64,255) #000040FF srgba(0,0,64,1)
        33: (128,  0, 64,255) #800040FF srgba(128,0,64,1)
        34: (  0,128, 64,255) #008040FF srgba(0,128,64,1)
        35: (128,128, 64,255) #808040FF srgba(128,128,64,1)
        36: (  0,  0,192,255) #0000C0FF srgba(0,0,192,1)
        37: (128,  0,192,255) #8000C0FF srgba(128,0,192,1)
        38: (  0,128,192,255) #0080C0FF srgba(0,128,192,1)
        39: (128,128,192,255) #8080C0FF srgba(128,128,192,1)
        40: ( 64,  0, 64,255) #400040FF srgba(64,0,64,1)
        41: (192,  0, 64,255) #C00040FF srgba(192,0,64,1)
        42: ( 64,128, 64,255) #408040FF srgba(64,128,64,1)
        43: (192,128, 64,255) #C08040FF srgba(192,128,64,1)
        44: ( 64,  0,192,255) #4000C0FF srgba(64,0,192,1)
        45: (192,  0,192,255) #C000C0FF srgba(192,0,192,1)
        46: ( 64,128,192,255) #4080C0FF srgba(64,128,192,1)
        47: (192,128,192,255) #C080C0FF srgba(192,128,192,1)
        48: (  0, 64, 64,255) #004040FF srgba(0,64,64,1)
        49: (128, 64, 64,255) #804040FF srgba(128,64,64,1)
        50: (  0,192, 64,255) #00C040FF srgba(0,192,64,1)
        51: (128,192, 64,255) #80C040FF srgba(128,192,64,1)
        52: (  0, 64,192,255) #0040C0FF srgba(0,64,192,1)
        53: (128, 64,192,255) #8040C0FF srgba(128,64,192,1)
        54: (  0,192,192,255) #00C0C0FF srgba(0,192,192,1)
        55: (128,192,192,255) #80C0C0FF srgba(128,192,192,1)
        56: ( 64, 64, 64,255) #404040FF grey25
        57: (192, 64, 64,255) #C04040FF srgba(192,64,64,1)
        58: ( 64,192, 64,255) #40C040FF srgba(64,192,64,1)
        59: (192,192, 64,255) #C0C040FF srgba(192,192,64,1)
        60: ( 64, 64,192,255) #4040C0FF srgba(64,64,192,1)
        61: (192, 64,192,255) #C040C0FF srgba(192,64,192,1)
        62: ( 64,192,192,255) #40C0C0FF srgba(64,192,192,1)
        63: (192,192,192,255) #C0C0C0FF silver
        64: ( 32,  0,  0,255) #200000FF srgba(32,0,0,1)
        65: (160,  0,  0,255) #A00000FF srgba(160,0,0,1)
        66: ( 32,128,  0,255) #208000FF srgba(32,128,0,1)
        67: (160,128,  0,255) #A08000FF srgba(160,128,0,1)
        68: ( 32,  0,128,255) #200080FF srgba(32,0,128,1)
        69: (160,  0,128,255) #A00080FF srgba(160,0,128,1)
        70: ( 32,128,128,255) #208080FF srgba(32,128,128,1)
        71: (160,128,128,255) #A08080FF srgba(160,128,128,1)
        72: ( 96,  0,  0,255) #600000FF srgba(96,0,0,1)
        73: (224,  0,  0,255) #E00000FF srgba(224,0,0,1)
        74: ( 96,128,  0,255) #608000FF srgba(96,128,0,1)
        75: (224,128,  0,255) #E08000FF srgba(224,128,0,1)
        76: ( 96,  0,128,255) #600080FF srgba(96,0,128,1)
        77: (224,  0,128,255) #E00080FF srgba(224,0,128,1)
        78: ( 96,128,128,255) #608080FF srgba(96,128,128,1)
        79: (224,128,128,255) #E08080FF srgba(224,128,128,1)
        80: ( 32, 64,  0,255) #204000FF srgba(32,64,0,1)
        81: (160, 64,  0,255) #A04000FF srgba(160,64,0,1)
        82: ( 32,192,  0,255) #20C000FF srgba(32,192,0,1)
        83: (160,192,  0,255) #A0C000FF srgba(160,192,0,1)
        84: ( 32, 64,128,255) #204080FF srgba(32,64,128,1)
        85: (160, 64,128,255) #A04080FF srgba(160,64,128,1)
        86: ( 32,192,128,255) #20C080FF srgba(32,192,128,1)
        87: (160,192,128,255) #A0C080FF srgba(160,192,128,1)
        88: ( 96, 64,  0,255) #604000FF srgba(96,64,0,1)
        89: (224, 64,  0,255) #E04000FF srgba(224,64,0,1)
        90: ( 96,192,  0,255) #60C000FF srgba(96,192,0,1)
        91: (224,192,  0,255) #E0C000FF srgba(224,192,0,1)
        92: ( 96, 64,128,255) #604080FF srgba(96,64,128,1)
        93: (224, 64,128,255) #E04080FF srgba(224,64,128,1)
        94: ( 96,192,128,255) #60C080FF srgba(96,192,128,1)
        95: (224,192,128,255) #E0C080FF srgba(224,192,128,1)
        96: ( 32,  0, 64,255) #200040FF srgba(32,0,64,1)
        97: (160,  0, 64,255) #A00040FF srgba(160,0,64,1)
        98: ( 32,128, 64,255) #208040FF srgba(32,128,64,1)
        99: (160,128, 64,255) #A08040FF srgba(160,128,64,1)
       100: ( 32,  0,192,255) #2000C0FF srgba(32,0,192,1)
       101: (160,  0,192,255) #A000C0FF srgba(160,0,192,1)
       102: ( 32,128,192,255) #2080C0FF srgba(32,128,192,1)
       103: (160,128,192,255) #A080C0FF srgba(160,128,192,1)
       104: ( 96,  0, 64,255) #600040FF srgba(96,0,64,1)
       105: (224,  0, 64,255) #E00040FF srgba(224,0,64,1)
       106: ( 96,128, 64,255) #608040FF srgba(96,128,64,1)
       107: (224,128, 64,255) #E08040FF srgba(224,128,64,1)
       108: ( 96,  0,192,255) #6000C0FF srgba(96,0,192,1)
       109: (224,  0,192,255) #E000C0FF srgba(224,0,192,1)
       110: ( 96,128,192,255) #6080C0FF srgba(96,128,192,1)
       111: (224,128,192,255) #E080C0FF srgba(224,128,192,1)
       112: ( 32, 64, 64,255) #204040FF srgba(32,64,64,1)
       113: (160, 64, 64,255) #A04040FF srgba(160,64,64,1)
       114: ( 32,192, 64,255) #20C040FF srgba(32,192,64,1)
       115: (160,192, 64,255) #A0C040FF srgba(160,192,64,1)
       116: ( 32, 64,192,255) #2040C0FF srgba(32,64,192,1)
       117: (160, 64,192,255) #A040C0FF srgba(160,64,192,1)
       118: ( 32,192,192,255) #20C0C0FF srgba(32,192,192,1)
       119: (160,192,192,255) #A0C0C0FF srgba(160,192,192,1)
       120: ( 96, 64, 64,255) #604040FF srgba(96,64,64,1)
       121: (224, 64, 64,255) #E04040FF srgba(224,64,64,1)
       122: ( 96,192, 64,255) #60C040FF srgba(96,192,64,1)
       123: (224,192, 64,255) #E0C040FF srgba(224,192,64,1)
       124: ( 96, 64,192,255) #6040C0FF srgba(96,64,192,1)
       125: (224, 64,192,255) #E040C0FF srgba(224,64,192,1)
       126: ( 96,192,192,255) #60C0C0FF srgba(96,192,192,1)
       127: (224,192,192,255) #E0C0C0FF srgba(224,192,192,1)
       128: (  0, 32,  0,255) #002000FF srgba(0,32,0,1)
       129: (128, 32,  0,255) #802000FF srgba(128,32,0,1)
       130: (  0,160,  0,255) #00A000FF srgba(0,160,0,1)
       131: (128,160,  0,255) #80A000FF srgba(128,160,0,1)
       132: (  0, 32,128,255) #002080FF srgba(0,32,128,1)
       133: (128, 32,128,255) #802080FF srgba(128,32,128,1)
       134: (  0,160,128,255) #00A080FF srgba(0,160,128,1)
       135: (128,160,128,255) #80A080FF srgba(128,160,128,1)
       136: ( 64, 32,  0,255) #402000FF srgba(64,32,0,1)
       137: (192, 32,  0,255) #C02000FF srgba(192,32,0,1)
       138: ( 64,160,  0,255) #40A000FF srgba(64,160,0,1)
       139: (192,160,  0,255) #C0A000FF srgba(192,160,0,1)
       140: ( 64, 32,128,255) #402080FF srgba(64,32,128,1)
       141: (192, 32,128,255) #C02080FF srgba(192,32,128,1)
       142: ( 64,160,128,255) #40A080FF srgba(64,160,128,1)
       143: (192,160,128,255) #C0A080FF srgba(192,160,128,1)
       144: (  0, 96,  0,255) #006000FF srgba(0,96,0,1)
       145: (128, 96,  0,255) #806000FF srgba(128,96,0,1)
       146: (  0,224,  0,255) #00E000FF srgba(0,224,0,1)
       147: (128,224,  0,255) #80E000FF srgba(128,224,0,1)
       148: (  0, 96,128,255) #006080FF srgba(0,96,128,1)
       149: (128, 96,128,255) #806080FF srgba(128,96,128,1)
       150: (  0,224,128,255) #00E080FF srgba(0,224,128,1)
       151: (128,224,128,255) #80E080FF srgba(128,224,128,1)
       152: ( 64, 96,  0,255) #406000FF srgba(64,96,0,1)
       153: (192, 96,  0,255) #C06000FF srgba(192,96,0,1)
       154: ( 64,224,  0,255) #40E000FF srgba(64,224,0,1)
       155: (192,224,  0,255) #C0E000FF srgba(192,224,0,1)
       156: ( 64, 96,128,255) #406080FF srgba(64,96,128,1)
       157: (192, 96,128,255) #C06080FF srgba(192,96,128,1)
       158: ( 64,224,128,255) #40E080FF srgba(64,224,128,1)
       159: (192,224,128,255) #C0E080FF srgba(192,224,128,1)
       160: (  0, 32, 64,255) #002040FF srgba(0,32,64,1)
       161: (128, 32, 64,255) #802040FF srgba(128,32,64,1)
       162: (  0,160, 64,255) #00A040FF srgba(0,160,64,1)
       163: (128,160, 64,255) #80A040FF srgba(128,160,64,1)
       164: (  0, 32,192,255) #0020C0FF srgba(0,32,192,1)
       165: (128, 32,192,255) #8020C0FF srgba(128,32,192,1)
       166: (  0,160,192,255) #00A0C0FF srgba(0,160,192,1)
       167: (128,160,192,255) #80A0C0FF srgba(128,160,192,1)
       168: ( 64, 32, 64,255) #402040FF srgba(64,32,64,1)
       169: (192, 32, 64,255) #C02040FF srgba(192,32,64,1)
       170: ( 64,160, 64,255) #40A040FF srgba(64,160,64,1)
       171: (192,160, 64,255) #C0A040FF srgba(192,160,64,1)
       172: ( 64, 32,192,255) #4020C0FF srgba(64,32,192,1)
       173: (192, 32,192,255) #C020C0FF srgba(192,32,192,1)
       174: ( 64,160,192,255) #40A0C0FF srgba(64,160,192,1)
       175: (192,160,192,255) #C0A0C0FF srgba(192,160,192,1)
       176: (  0, 96, 64,255) #006040FF srgba(0,96,64,1)
       177: (128, 96, 64,255) #806040FF srgba(128,96,64,1)
       178: (  0,224, 64,255) #00E040FF srgba(0,224,64,1)
       179: (128,224, 64,255) #80E040FF srgba(128,224,64,1)
       180: (  0, 96,192,255) #0060C0FF srgba(0,96,192,1)
       181: (128, 96,192,255) #8060C0FF srgba(128,96,192,1)
       182: (  0,224,192,255) #00E0C0FF srgba(0,224,192,1)
       183: (128,224,192,255) #80E0C0FF srgba(128,224,192,1)
       184: ( 64, 96, 64,255) #406040FF srgba(64,96,64,1)
       185: (192, 96, 64,255) #C06040FF srgba(192,96,64,1)
       186: ( 64,224, 64,255) #40E040FF srgba(64,224,64,1)
       187: (192,224, 64,255) #C0E040FF srgba(192,224,64,1)
       188: ( 64, 96,192,255) #4060C0FF srgba(64,96,192,1)
       189: (192, 96,192,255) #C060C0FF srgba(192,96,192,1)
       190: ( 64,224,192,255) #40E0C0FF srgba(64,224,192,1)
       191: (192,224,192,255) #C0E0C0FF srgba(192,224,192,1)
       192: ( 32, 32,  0,255) #202000FF srgba(32,32,0,1)
       193: (160, 32,  0,255) #A02000FF srgba(160,32,0,1)
       194: ( 32,160,  0,255) #20A000FF srgba(32,160,0,1)
       195: (160,160,  0,255) #A0A000FF srgba(160,160,0,1)
       196: ( 32, 32,128,255) #202080FF srgba(32,32,128,1)
       197: (160, 32,128,255) #A02080FF srgba(160,32,128,1)
       198: ( 32,160,128,255) #20A080FF srgba(32,160,128,1)
       199: (160,160,128,255) #A0A080FF srgba(160,160,128,1)
       200: ( 96, 32,  0,255) #602000FF srgba(96,32,0,1)
       201: (224, 32,  0,255) #E02000FF srgba(224,32,0,1)
       202: ( 96,160,  0,255) #60A000FF srgba(96,160,0,1)
       203: (224,160,  0,255) #E0A000FF srgba(224,160,0,1)
       204: ( 96, 32,128,255) #602080FF srgba(96,32,128,1)
       205: (224, 32,128,255) #E02080FF srgba(224,32,128,1)
       206: ( 96,160,128,255) #60A080FF srgba(96,160,128,1)
       207: (224,160,128,255) #E0A080FF srgba(224,160,128,1)
       208: ( 32, 96,  0,255) #206000FF srgba(32,96,0,1)
       209: (160, 96,  0,255) #A06000FF srgba(160,96,0,1)
       210: ( 32,224,  0,255) #20E000FF srgba(32,224,0,1)
       211: (160,224,  0,255) #A0E000FF srgba(160,224,0,1)
       212: ( 32, 96,128,255) #206080FF srgba(32,96,128,1)
       213: (160, 96,128,255) #A06080FF srgba(160,96,128,1)
       214: ( 32,224,128,255) #20E080FF srgba(32,224,128,1)
       215: (160,224,128,255) #A0E080FF srgba(160,224,128,1)
       216: ( 96, 96,  0,255) #606000FF srgba(96,96,0,1)
       217: (224, 96,  0,255) #E06000FF srgba(224,96,0,1)
       218: ( 96,224,  0,255) #60E000FF srgba(96,224,0,1)
       219: (224,224,  0,255) #E0E000FF srgba(224,224,0,1)
       220: ( 96, 96,128,255) #606080FF srgba(96,96,128,1)
       221: (224, 96,128,255) #E06080FF srgba(224,96,128,1)
       222: ( 96,224,128,255) #60E080FF srgba(96,224,128,1)
       223: (224,224,128,255) #E0E080FF srgba(224,224,128,1)
       224: ( 32, 32, 64,255) #202040FF srgba(32,32,64,1)
       225: (160, 32, 64,255) #A02040FF srgba(160,32,64,1)
       226: ( 32,160, 64,255) #20A040FF srgba(32,160,64,1)
       227: (160,160, 64,255) #A0A040FF srgba(160,160,64,1)
       228: ( 32, 32,192,255) #2020C0FF srgba(32,32,192,1)
       229: (160, 32,192,255) #A020C0FF srgba(160,32,192,1)
       230: ( 32,160,192,255) #20A0C0FF srgba(32,160,192,1)
       231: (160,160,192,255) #A0A0C0FF srgba(160,160,192,1)
       232: ( 96, 32, 64,255) #602040FF srgba(96,32,64,1)
       233: (224, 32, 64,255) #E02040FF srgba(224,32,64,1)
       234: ( 96,160, 64,255) #60A040FF srgba(96,160,64,1)
       235: (224,160, 64,255) #E0A040FF srgba(224,160,64,1)
       236: ( 96, 32,192,255) #6020C0FF srgba(96,32,192,1)
       237: (224, 32,192,255) #E020C0FF srgba(224,32,192,1)
       238: ( 96,160,192,255) #60A0C0FF srgba(96,160,192,1)
       239: (224,160,192,255) #E0A0C0FF srgba(224,160,192,1)
       240: ( 32, 96, 64,255) #206040FF srgba(32,96,64,1)
       241: (160, 96, 64,255) #A06040FF srgba(160,96,64,1)
       242: ( 32,224, 64,255) #20E040FF srgba(32,224,64,1)
       243: (160,224, 64,255) #A0E040FF srgba(160,224,64,1)
       244: ( 32, 96,192,255) #2060C0FF srgba(32,96,192,1)
       245: (160, 96,192,255) #A060C0FF srgba(160,96,192,1)
       246: ( 32,224,192,255) #20E0C0FF srgba(32,224,192,1)
       247: (160,224,192,255) #A0E0C0FF srgba(160,224,192,1)
       248: ( 96, 96, 64,255) #606040FF srgba(96,96,64,1)
       249: (224, 96, 64,255) #E06040FF srgba(224,96,64,1)
       250: ( 96,224, 64,255) #60E040FF srgba(96,224,64,1)
       251: (224,224, 64,255) #E0E040FF srgba(224,224,64,1)
       252: ( 96, 96,192,255) #6060C0FF srgba(96,96,192,1)
       253: (224, 96,192,255) #E060C0FF srgba(224,96,192,1)
       254: ( 96,224,192,255) #60E0C0FF srgba(96,224,192,1)
       255: (224,224,192,255) #E0E0C0FF srgba(224,224,192,1)   <--- THIS LINE
Syrup answered 3/8, 2018 at 17:52 Comment(3)
Thank you again. My purpose is actually not getting a gray-scaled result. But rather, understanding why this line "img = Image.open(filename)" could produce that qualsi black-and-white result. If I understood correctly, in palletised png, each pixel only stores the index of the color value. So, the segmentation border becomes "white" because the pixels of original border has an index of 255, which points to an RGB value of "E0E0C0" (original segmentation border color)?Conceptionconceptual
Yes, I have added some further info to my answer to help you understand. Good luck with your project.Syrup
Awsome! Thank you again !Conceptionconceptual
M
2

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

enter image description here

Manchukuo answered 3/8, 2018 at 18:30 Comment(1)
Hi. I wanted to ask, why do you multiply idx by 3 when getting the R,G, B values? Also, why did you multiply the R channel by 0.2989, G by 0.5870 and B by 0.1140? And, how did you come to know that the image is Palettised? Sorry for so many questions. I am a beginner at image processing, and just curious about this. :) Thanks.Rowlandson

© 2022 - 2024 — McMap. All rights reserved.