Search code examples
image-processingimagemagickpngrasterio

Convert GIF image to PNG with imagemagick, but getting a RGBA image instead of a paletissed version


This question is similar to this one. The difference is that the solution proposed there does not seem to work when you want a PNG as output.

In a nutshell, I have a GIF image that I convert to PNG for further processing (with Python rasterio). I would like to get a regular PNG image with the expected four channels.

I have tried this:

convert -type TrueColorAlpha Image.gif Image.png

However, I get a very optimised version that seems to be paletissed and just one channel. This is the metadata I get using gdalinfo:

Driver: GIF/Graphics Interchange Format (.gif)
Files: ESZA_20210204_105147_0_source.gif
Size is 480, 530
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  530.0)
Upper Right (  480.0,    0.0)
Lower Right (  480.0,  530.0)
Center      (  240.0,  265.0)
Band 1 Block=480x1 Type=Byte, ColorInterp=Palette
  Metadata:
    GIF_BACKGROUND=0
  Color Table (RGB with 64 entries)
    0: 0,0,0,255
    1: 89,111,115,255
    2: 127,127,127,255
    3: 188,50,31,255
    4: 255,0,0,255
    5: 255,127,0,255
    6: 200,0,90,255
    7: 208,112,99,255
    8: 67,131,35,255
    9: 0,192,0,255
   10: 0,255,0,255
   11: 255,187,0,255
   12: 248,210,21,255
   13: 255,255,0,255
   14: 155,155,117,255
   15: 250,222,83,255
   16: 3,49,147,255
   17: 37,85,163,255
   18: 69,110,178,255
   19: 0,0,252,255
   20: 98,133,190,255
   21: 0,148,252,255
   22: 123,152,201,255
   23: 0,252,252,255
   24: 222,156,145,255
   25: 250,234,152,255
   26: 233,194,186,255
   27: 149,172,211,255
   28: 172,191,221,255
   29: 243,222,218,255
   30: 253,245,203,255
   31: 192,205,228,255
   32: 208,218,235,255
   33: 222,229,242,255
   34: 250,239,237,255
   35: 255,251,232,255
   36: 231,236,245,255
   37: 239,243,248,255
   38: 243,246,250,255
   39: 247,249,252,255
   40: 253,250,249,255
   41: 250,251,253,255
   42: 254,254,252,255
   43: 252,253,254,255
   44: 255,255,255,255
   45: 0,0,0,255
   46: 0,0,0,255
   47: 0,0,0,255
   48: 0,0,0,255
   49: 0,0,0,255
   50: 0,0,0,255
   51: 0,0,0,255
   52: 0,0,0,255
   53: 0,0,0,255
   54: 0,0,0,255
   55: 0,0,0,255
   56: 0,0,0,255
   57: 0,0,0,255
   58: 0,0,0,255
   59: 0,0,0,255
   60: 0,0,0,255
   61: 0,0,0,255
   62: 0,0,0,255
   63: 0,0,0,255

Any idea how to force imagemagick to produce a regular four-channel PNG image?


Solution

  • To convert GIF to PNG, if you want to set the color depth use the PNG prefix. This is special to PNG.

    For 32-bit color (RGBA):

    convert image.gif PNG32:image.png
    

    For 24-bit color (RGB):

    convert image.gif PNG24:image.png
    

    For 8-bit Palette color:

    convert image.gif PNG8:image.png
    

    See https://legacy.imagemagick.org/Usage/formats/#png_formats and https://legacy.imagemagick.org/Usage/formats/#png_write