[Image-SIG] PIL and 8-bit palette alpha values

Roger André randre at gmail.com
Fri Jan 15 22:53:45 CET 2010


Hi All,

I'm trying to figure out how to use PIL to crop an existing image, and to
preserve all of the characteristics of the input image in the output.  My
input image is an 8-bit paletted PNG with alpha. It "looks" like this:
(NOTE that it only has 85 colors, and that alpha value in entries 1 - 43 is
< 255)

Metadata:
  Software=UMN Mapserver
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  256.0)
Upper Right (  256.0,    0.0)
Lower Right (  256.0,  256.0)
Center      (  128.0,  128.0)
Band 1 Block=256x1 Type=Byte, ColorInterp=Palette
  NoData Value=0
  Color Table (RGB with 85 entries)
    0: 0,0,0,0
    1: 251,0,0,64
    2: 250,0,0,54
    3: 253,0,0,114
    4: 254,0,0,176
    5: 254,0,0,224
    6: 254,0,0,228
    7: 254,0,0,252
    8: 253,0,0,168
    9: 254,0,0,170
   10: 254,0,0,234
   11: 254,0,0,246
   12: 254,0,0,230
   13: 252,0,0,194
   14: 253,0,0,232
   15: 254,0,0,248
   16: 254,0,0,226
   17: 253,0,0,252
   18: 254,0,0,244
   19: 254,0,0,240
   20: 253,0,0,248
   21: 254,0,0,192
   22: 250,0,0,98
   23: 252,0,0,98
   24: 253,0,0,236
   25: 251,0,0,144
   26: 253,0,0,208
   27: 252,0,0,252
   28: 253,0,0,250
   29: 253,0,0,226
   30: 254,0,0,212
   31: 253,0,0,242
   32: 250,0,0,112
   33: 253,0,0,238
   34: 253,0,0,222
   35: 254,0,0,208
   36: 253,0,0,234
   37: 254,0,0,236
   38: 254,0,0,232
   39: 252,0,0,150
   40: 253,0,0,240
   41: 251,0,0,114
   42: 253,0,0,244
   43: 254,0,0,222
   44: 231,0,0,255
   45: 202,0,0,255
   46: 249,0,0,255
   47: 96,0,0,255
   48: 63,0,0,255
   49: 229,0,0,255
   50: 89,0,0,255
   51: 248,0,0,255
   52: 192,0,0,255
   53: 230,0,0,255
   54: 88,0,0,255
   55: 176,0,0,255
   56: 225,0,0,255
   57: 206,0,0,255
   58: 64,0,0,255
   59: 53,0,0,255
   60: 113,0,0,255
   61: 169,0,0,255
   62: 183,0,0,255
   63: 252,0,0,255
   64: 184,0,0,255
   65: 251,0,0,255
   66: 247,0,0,255
   67: 189,0,0,255
   68: 224,0,0,255
   69: 250,0,0,255
   70: 244,0,0,255
   71: 243,0,0,255
   72: 227,0,0,255
   73: 241,0,0,255
   74: 253,0,0,255
   75: 246,0,0,255
   76: 167,0,0,255
   77: 168,0,0,255
   78: 254,0,0,255
   79: 242,0,0,255
   80: 245,0,0,255
   81: 44,0,0,255
   82: 161,0,0,255
   83: 0,0,0,255
   84: 255,0,0,255

I tried a simple experiment with PIL to create a new paletted image that was
identical to the input:

>>> import Image
>>> image = Image.open('quant_on_mapserv.png')
>>> image.info
{'transparency': 0, 'Software': 'UMN Mapserver'}
>>> assert image.mode == 'P'
>>> transparency = image.info['transparency']
>>> image.save('pil_test.png', transparency=transparency)

When I inspected the resulting image, I see that the palette is completely
different from what the input had (more entries, and all alpha entries from
1 - 255 set to 255).

Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  256.0)
Upper Right (  256.0,    0.0)
Lower Right (  256.0,  256.0)
Center      (  128.0,  128.0)
Band 1 Block=256x1 Type=Byte, ColorInterp=Palette
  NoData Value=0
  Color Table (RGB with 256 entries)
    0: 0,0,0,0
    1: 251,0,0,255
    2: 250,0,0,255
    3: 253,0,0,255
    4: 254,0,0,255
    5: 254,0,0,255
    6: 254,0,0,255
    7: 254,0,0,255
    8: 253,0,0,255
    9: 254,0,0,255
   10: 254,0,0,255
   11: 254,0,0,255
   12: 254,0,0,255
   13: 252,0,0,255
   14: 253,0,0,255
   15: 254,0,0,255
   16: 254,0,0,255
   17: 253,0,0,255
   18: 254,0,0,255
   19: 254,0,0,255
   20: 253,0,0,255
   21: 254,0,0,255
   22: 250,0,0,255
   23: 252,0,0,255
   24: 253,0,0,255
   25: 251,0,0,255
   26: 253,0,0,255
   27: 252,0,0,255
   28: 253,0,0,255
   29: 253,0,0,255
   30: 254,0,0,255
   31: 253,0,0,255
   32: 250,0,0,255
   33: 253,0,0,255
   34: 253,0,0,255
   35: 254,0,0,255
   36: 253,0,0,255
   37: 254,0,0,255
   38: 254,0,0,255
   39: 252,0,0,255
   40: 253,0,0,255
   41: 251,0,0,255
   42: 253,0,0,255
   43: 254,0,0,255
   44: 231,0,0,255
   45: 202,0,0,255
   46: 249,0,0,255
   47: 96,0,0,255
   48: 63,0,0,255
   49: 229,0,0,255
   50: 89,0,0,255
   51: 248,0,0,255
   52: 192,0,0,255
   53: 230,0,0,255
   54: 88,0,0,255
   55: 176,0,0,255
   56: 225,0,0,255
   57: 206,0,0,255
   58: 64,0,0,255
   59: 53,0,0,255
   60: 113,0,0,255
   61: 169,0,0,255
   62: 183,0,0,255
   63: 252,0,0,255
   64: 184,0,0,255
   65: 251,0,0,255
   66: 247,0,0,255
   67: 189,0,0,255
   68: 224,0,0,255
   69: 250,0,0,255
   70: 244,0,0,255
   71: 243,0,0,255
   72: 227,0,0,255
   73: 241,0,0,255
   74: 253,0,0,255
   75: 246,0,0,255
   76: 167,0,0,255
   77: 168,0,0,255
   78: 254,0,0,255
   79: 242,0,0,255
   80: 245,0,0,255
   81: 44,0,0,255
   82: 161,0,0,255
   83: 0,0,0,255
   84: 255,0,0,255
   85: 85,85,85,255
   86: 86,86,86,255
   87: 87,87,87,255
   88: 88,88,88,255
   89: 89,89,89,255
   90: 90,90,90,255
   91: 91,91,91,255
   92: 92,92,92,255
   93: 93,93,93,255
   94: 94,94,94,255
   95: 95,95,95,255
   96: 96,96,96,255
   97: 97,97,97,255
   98: 98,98,98,255
   99: 99,99,99,255
  100: 100,100,100,255
  101: 101,101,101,255
  102: 102,102,102,255
  103: 103,103,103,255
  104: 104,104,104,255
  105: 105,105,105,255
  106: 106,106,106,255
  107: 107,107,107,255
  108: 108,108,108,255
  109: 109,109,109,255
  110: 110,110,110,255
  111: 111,111,111,255
  112: 112,112,112,255
  113: 113,113,113,255
  114: 114,114,114,255
  115: 115,115,115,255
  116: 116,116,116,255
  117: 117,117,117,255
  118: 118,118,118,255
  119: 119,119,119,255
  120: 120,120,120,255
  121: 121,121,121,255
  122: 122,122,122,255
  123: 123,123,123,255
  124: 124,124,124,255
  125: 125,125,125,255
  126: 126,126,126,255
  127: 127,127,127,255
  128: 128,128,128,255
  129: 129,129,129,255
  130: 130,130,130,255
  131: 131,131,131,255
  132: 132,132,132,255
  133: 133,133,133,255
  134: 134,134,134,255
  135: 135,135,135,255
  136: 136,136,136,255
  137: 137,137,137,255
  138: 138,138,138,255
  139: 139,139,139,255
  140: 140,140,140,255
  141: 141,141,141,255
  142: 142,142,142,255
  143: 143,143,143,255
  144: 144,144,144,255
  145: 145,145,145,255
  146: 146,146,146,255
  147: 147,147,147,255
  148: 148,148,148,255
  149: 149,149,149,255
  150: 150,150,150,255
  151: 151,151,151,255
  152: 152,152,152,255
  153: 153,153,153,255
  154: 154,154,154,255
  155: 155,155,155,255
  156: 156,156,156,255
  157: 157,157,157,255
  158: 158,158,158,255
  159: 159,159,159,255
  160: 160,160,160,255
  161: 161,161,161,255
  162: 162,162,162,255
  163: 163,163,163,255
  164: 164,164,164,255
  165: 165,165,165,255
  166: 166,166,166,255
  167: 167,167,167,255
  168: 168,168,168,255
  169: 169,169,169,255
  170: 170,170,170,255
  171: 171,171,171,255
  172: 172,172,172,255
  173: 173,173,173,255
  174: 174,174,174,255
  175: 175,175,175,255
  176: 176,176,176,255
  177: 177,177,177,255
  178: 178,178,178,255
  179: 179,179,179,255
  180: 180,180,180,255
  181: 181,181,181,255
  182: 182,182,182,255
  183: 183,183,183,255
  184: 184,184,184,255
  185: 185,185,185,255
  186: 186,186,186,255
  187: 187,187,187,255
  188: 188,188,188,255
  189: 189,189,189,255
  190: 190,190,190,255
  191: 191,191,191,255
  192: 192,192,192,255
  193: 193,193,193,255
  194: 194,194,194,255
  195: 195,195,195,255
  196: 196,196,196,255
  197: 197,197,197,255
  198: 198,198,198,255
  199: 199,199,199,255
  200: 200,200,200,255
  201: 201,201,201,255
  202: 202,202,202,255
  203: 203,203,203,255
  204: 204,204,204,255
  205: 205,205,205,255
  206: 206,206,206,255
  207: 207,207,207,255
  208: 208,208,208,255
  209: 209,209,209,255
  210: 210,210,210,255
  211: 211,211,211,255
  212: 212,212,212,255
  213: 213,213,213,255
  214: 214,214,214,255
  215: 215,215,215,255
  216: 216,216,216,255
  217: 217,217,217,255
  218: 218,218,218,255
  219: 219,219,219,255
  220: 220,220,220,255
  221: 221,221,221,255
  222: 222,222,222,255
  223: 223,223,223,255
  224: 224,224,224,255
  225: 225,225,225,255
  226: 226,226,226,255
  227: 227,227,227,255
  228: 228,228,228,255
  229: 229,229,229,255
  230: 230,230,230,255
  231: 231,231,231,255
  232: 232,232,232,255
  233: 233,233,233,255
  234: 234,234,234,255
  235: 235,235,235,255
  236: 236,236,236,255
  237: 237,237,237,255
  238: 238,238,238,255
  239: 239,239,239,255
  240: 240,240,240,255
  241: 241,241,241,255
  242: 242,242,242,255
  243: 243,243,243,255
  244: 244,244,244,255
  245: 245,245,245,255
  246: 246,246,246,255
  247: 247,247,247,255
  248: 248,248,248,255
  249: 249,249,249,255
  250: 250,250,250,255
  251: 251,251,251,255
  252: 252,252,252,255
  253: 253,253,253,255
  254: 254,254,254,255
  255: 255,255,255,255

While the resulting image looks pretty good, the file size is far larger
than that of the input image.

$ ll quant_on_mapserv.png pil_test.png
-rw-r--r-- 1 randre randre 4540 2010-01-15 11:15 pil_test.png
-rw-r--r-- 1 randre randre 1951 2010-01-15 10:50 quant_on_mapserv.png

I would very much like to keep the size down, and I imagine that in order to
do so, I need to be able to match the palette better.  Can someone explain
to me A) what the difference is between these 2 palette ptypes, and B)
whether it is possible for me to use PIL to create a copy, or cropped
version, of the original file that matches its format?

Thanks,

Roger
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20100115/5c9cc889/attachment-0001.htm>


More information about the Image-SIG mailing list