Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
833 views
in Technique[技术] by (71.8m points)

image - Python: Use PIL to load png file gives strange results

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...