1

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?

Pythonist
  • 1,937
  • 1
  • 14
  • 25
  • What do you man by regular 4 channels? If you want RGBA in PNG output, then try `convert image.gif PNG32:image.png` – fmw42 Feb 10 '21 at 19:13
  • If you want a palette PNG, then do `convert image.gif PNG8:image.png` – fmw42 Feb 11 '21 at 01:02
  • This is exactly what I needed. Thanks! Please consider turning this comment into a proper answer and I will gladly accept it. At the time you do it, please consider expanding a bit to explain what's going on here, or at least linking a good reference. I was searching online and I could not find good references about the "different ways PNG can store colour information" and/or what does the 8 stand for? – Pythonist Feb 11 '21 at 08:19

1 Answers1

0

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

fmw42
  • 46,825
  • 10
  • 62
  • 80