Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick preserve custom palette when joining 2 PNGs

I need to join 2 PNG images, with 16 color palette, keep colors number and order as originals.

Both PNGs use the same 16 color palette.

There is a way to create 4 bit indexed PNG with all colors, in exact order, even unused in each PNG?

Edit to be more clear:

I have 2 pictures. The first is "level-000.png":

level-000.png

It is identified as:

Image: level-000.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 144x144+0+0
  Resolution: 28.35x28.35
  Print size: 5.07937x5.07937
  Units: PixelsPerCentimeter
  Type: Palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 20736
    Red:
      min: 0 (0)
      max: 184 (0.721569)
      mean: 88.6151 (0.34751)
      standard deviation: 27.9329 (0.109541)
      kurtosis: 1.7675
      skewness: 0.143857
    Green:
      min: 0 (0)
      max: 199 (0.780392)
      mean: 68.8957 (0.270179)
      standard deviation: 22.7411 (0.0891808)
      kurtosis: 18.2722
      skewness: 2.87959
    Blue:
      min: 0 (0)
      max: 111 (0.435294)
      mean: 19.4671 (0.0763416)
      standard deviation: 22.1775 (0.0869706)
      kurtosis: 2.80475
      skewness: 1.17593
  Image statistics:
    Overall:
      min: 0 (0)
      max: 199 (0.780392)
      mean: 58.9926 (0.231344)
      standard deviation: 24.4216 (0.095771)
      kurtosis: 17.5446
      skewness: 0.948025
  Colors: 4
  Histogram:
       390: (  0,  0,  0) #000000 black
     10196: ( 67, 57,  0) #433900 srgb(67,57,0)
      9770: (111, 79, 37) #6F4F25 srgb(111,79,37)
       380: (184,199,111) #B8C76F srgb(184,199,111)
  Colormap entries: 16
  Colormap:
         0: (  0,  0,  0) #000000 black
         1: (255,255,255) #FFFFFF white
         2: (104, 55, 43) #68372B srgb(104,55,43)
         3: (112,164,178) #70A4B2 srgb(112,164,178)
         4: (111, 61,134) #6F3D86 srgb(111,61,134)
         5: ( 88,141, 67) #588D43 srgb(88,141,67)
         6: ( 53, 40,121) #352879 srgb(53,40,121)
         7: (184,199,111) #B8C76F srgb(184,199,111)
         8: (111, 79, 37) #6F4F25 srgb(111,79,37)
         9: ( 67, 57,  0) #433900 srgb(67,57,0)
        10: (154,103, 89) #9A6759 srgb(154,103,89)
        11: ( 68, 68, 68) #444444 srgb(68,68,68)
        12: (108,108,108) #6C6C6C srgb(108,108,108)
        13: (154,210,132) #9AD284 srgb(154,210,132)
        14: (108, 94,181) #6C5EB5 srgb(108,94,181)
        15: (149,149,149) #959595 srgb(149,149,149)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: black
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 144x144+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-04-10T12:24:18+02:00
    date:modify: 2015-04-09T22:33:29+02:00
    png:bKGD: chunk was found (see Background color, above)
    png:IHDR.bit-depth-orig: 4
    png:IHDR.bit_depth: 4
    png:IHDR.color-type-orig: 3
    png:IHDR.color_type: 3 (Indexed)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 144, 144
    png:pHYs: x_res=2835, y_res=2835, units=1
    png:PLTE.number_colors: 16
    png:sRGB: intent=0 (Perceptual Intent)
    signature: 00d9489d55480d588c3329ab0fa844ed2a67eea06fd4dfa793503a36f9c4b160
  Artifacts:
    filename: level-000.png
    verbose: true
  Tainted: False
  Filesize: 337B
  Number pixels: 20.7K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-11-18

The second pic is "scacchiera.png".

scacchiera.png

It is identified as:

Image: scacchiera.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 144x144+0+0
  Resolution: 28.35x28.35
  Print size: 5.07937x5.07937
  Units: PixelsPerCentimeter
  Type: Palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 20736
    Red:
      min: 67 (0.262745)
      max: 111 (0.435294)
      mean: 89 (0.34902)
      standard deviation: 22 (0.0862745)
      kurtosis: -2
      skewness: -1.18433e-11
    Green:
      min: 57 (0.223529)
      max: 79 (0.309804)
      mean: 68 (0.266667)
      standard deviation: 11 (0.0431373)
      kurtosis: -2
      skewness: 7.03683e-11
    Blue:
      min: 0 (0)
      max: 37 (0.145098)
      mean: 18.5 (0.072549)
      standard deviation: 18.5 (0.072549)
      kurtosis: -2
      skewness: 0
  Image statistics:
    Overall:
      min: 0 (0)
      max: 111 (0.435294)
      mean: 58.5 (0.229412)
      standard deviation: 17.7694 (0.0696837)
      kurtosis: 29.9398
      skewness: -1.66976
  Colors: 2
  Histogram:
     10368: ( 67, 57,  0) #433900 srgb(67,57,0)
     10368: (111, 79, 37) #6F4F25 srgb(111,79,37)
  Colormap entries: 16
  Colormap:
         0: (  0,  0,  0) #000000 black
         1: (255,255,255) #FFFFFF white
         2: (104, 55, 43) #68372B srgb(104,55,43)
         3: (112,164,178) #70A4B2 srgb(112,164,178)
         4: (111, 61,134) #6F3D86 srgb(111,61,134)
         5: ( 88,141, 67) #588D43 srgb(88,141,67)
         6: ( 53, 40,121) #352879 srgb(53,40,121)
         7: (184,199,111) #B8C76F srgb(184,199,111)
         8: (111, 79, 37) #6F4F25 srgb(111,79,37)
         9: ( 67, 57,  0) #433900 srgb(67,57,0)
        10: (154,103, 89) #9A6759 srgb(154,103,89)
        11: ( 68, 68, 68) #444444 srgb(68,68,68)
        12: (108,108,108) #6C6C6C srgb(108,108,108)
        13: (154,210,132) #9AD284 srgb(154,210,132)
        14: (108, 94,181) #6C5EB5 srgb(108,94,181)
        15: (149,149,149) #959595 srgb(149,149,149)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 144x144+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-04-10T20:46:55+02:00
    date:modify: 2015-04-10T20:46:55+02:00
    png:IHDR.bit-depth-orig: 4
    png:IHDR.bit_depth: 4
    png:IHDR.color-type-orig: 3
    png:IHDR.color_type: 3 (Indexed)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 144, 144
    png:pHYs: x_res=2835, y_res=2835, units=1
    png:PLTE.number_colors: 16
    png:sRGB: intent=0 (Perceptual Intent)
    signature: 8a0ca53e6e6f8e8fc4b141c7194b399ed1fc32473b174b6240addbb3f98864e5
  Artifacts:
    filename: scacchiera.png
    verbose: true
  Tainted: False
  Filesize: 241B
  Number pixels: 20.7K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-11-18

As you can see, both pics have same colors:

  Colormap entries: 16
  Colormap:
         0: (  0,  0,  0) #000000 black
         1: (255,255,255) #FFFFFF white
         2: (104, 55, 43) #68372B srgb(104,55,43)
         3: (112,164,178) #70A4B2 srgb(112,164,178)
         4: (111, 61,134) #6F3D86 srgb(111,61,134)
         5: ( 88,141, 67) #588D43 srgb(88,141,67)
         6: ( 53, 40,121) #352879 srgb(53,40,121)
         7: (184,199,111) #B8C76F srgb(184,199,111)
         8: (111, 79, 37) #6F4F25 srgb(111,79,37)
         9: ( 67, 57,  0) #433900 srgb(67,57,0)
        10: (154,103, 89) #9A6759 srgb(154,103,89)
        11: ( 68, 68, 68) #444444 srgb(68,68,68)
        12: (108,108,108) #6C6C6C srgb(108,108,108)
        13: (154,210,132) #9AD284 srgb(154,210,132)
        14: (108, 94,181) #6C5EB5 srgb(108,94,181)
        15: (149,149,149) #959595 srgb(149,149,149)

If i join them with

convert -page 312x144+0+0 "scacchiera.png" -page +168+0 \
   "level-000.png" -background black -layers flatten "joined.png" 

then "joined.png" picture is:

joined.png

It is identified as:

Image: joined.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 312x144+0+0
  Resolution: 28.35x28.35
  Print size: 11.0053x5.07937
  Units: PixelsPerCentimeter
  Type: Palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 44928
    Red:
      min: 0 (0)
      max: 184 (0.721569)
      mean: 81.9762 (0.321475)
      standard deviation: 33.8163 (0.132613)
      kurtosis: 0.917842
      skewness: -0.743586
    Green:
      min: 0 (0)
      max: 199 (0.780392)
      mean: 63.1826 (0.247775)
      standard deviation: 25.0478 (0.0982265)
      kurtosis: 7.85041
      skewness: 0.0804364
    Blue:
      min: 0 (0)
      max: 111 (0.435294)
      mean: 17.5233 (0.0687188)
      standard deviation: 20.2675 (0.0794802)
      kurtosis: 1.51309
      skewness: 0.876441
  Image statistics:
    Overall:
      min: 0 (0)
      max: 199 (0.780392)
      mean: 54.2274 (0.212656)
      standard deviation: 26.9673 (0.105754)
      kurtosis: 7.87712
      skewness: 0.327418
  Colors: 4
  Histogram:
      3846: (  0,  0,  0) #000000 black
     20564: ( 67, 57,  0) #433900 srgb(67,57,0)
     20138: (111, 79, 37) #6F4F25 srgb(111,79,37)
       380: (184,199,111) #B8C76F srgb(184,199,111)
  Colormap entries: 4
  Colormap:
         0: ( 67, 57,  0) #433900 srgb(67,57,0)
         1: (111, 79, 37) #6F4F25 srgb(111,79,37)
         2: (  0,  0,  0) #000000 black
         3: (184,199,111) #B8C76F srgb(184,199,111)
  Rendering intent: Perceptual
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: black
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 312x144+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-04-12T15:13:11+02:00
    date:modify: 2015-04-12T15:13:11+02:00
    png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45454544 (See Gamma, above)
    png:IHDR.bit-depth-orig: 2
    png:IHDR.bit_depth: 2
    png:IHDR.color-type-orig: 3
    png:IHDR.color_type: 3 (Indexed)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 312, 144
    png:pHYs: x_res=2835, y_res=2835, units=1
    png:PLTE.number_colors: 4
    png:sRGB: intent=0 (Perceptual Intent)
    png:text: 2 tEXt/zTXt/iTXt chunks were found
    signature: 2049a10c0a2a73f864125d0ba93ccfc604a750667e3ba1d0fe8fe3be38a1cbea
  Artifacts:
    filename: joined.png
    verbose: true
  Tainted: False
  Filesize: 487B
  Number pixels: 44.9K
  Pixels per second: 4.493MB
  User time: 0.000u
  Elapsed time: 0:01.009
  Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-11-18

and you can see that palette is totally different from the initial one.

Images are here:

  • www.dropbox.com/s/jdftk03rizeqcub/scacchiera.png
  • www.dropbox.com/s/9c1fmav5qni8u2m/level-000.png
  • www.dropbox.com/s/8hhd1zyzb31c4me/joined.png

The question is: is there a way to have ALWAYS this palette:

  Colormap entries: 16
  Colormap:
         0: (  0,  0,  0) #000000 black
         1: (255,255,255) #FFFFFF white
         2: (104, 55, 43) #68372B srgb(104,55,43)
         3: (112,164,178) #70A4B2 srgb(112,164,178)
         4: (111, 61,134) #6F3D86 srgb(111,61,134)
         5: ( 88,141, 67) #588D43 srgb(88,141,67)
         6: ( 53, 40,121) #352879 srgb(53,40,121)
         7: (184,199,111) #B8C76F srgb(184,199,111)
         8: (111, 79, 37) #6F4F25 srgb(111,79,37)
         9: ( 67, 57,  0) #433900 srgb(67,57,0)
        10: (154,103, 89) #9A6759 srgb(154,103,89)
        11: ( 68, 68, 68) #444444 srgb(68,68,68)
        12: (108,108,108) #6C6C6C srgb(108,108,108)
        13: (154,210,132) #9AD284 srgb(154,210,132)
        14: (108, 94,181) #6C5EB5 srgb(108,94,181)
        15: (149,149,149) #959595 srgb(149,149,149)

exactly with these 16 colors and in this order for each resluting PNG?

like image 831
Flavio Pasqualin Avatar asked Apr 12 '15 08:04

Flavio Pasqualin


1 Answers

I have tried some more and I can't see a way to do this with ImageMagick. You can extract the unique colours from scacchiera.png like this:

convert scacchiera.png -unique-colors -scale 1000 swatch.png

but there are only 2 colours in there, so that is no use:

enter image description here

You can also extract the colormap and make a CLUT like this:

convert xc:"#000000" xc:"#FFFFFF" xc:"#68372B" xc:"#70A4B2" \
        xc:"#6F3D86" xc:"#588D43" xc:"#352879" xc:"#B8C76F" \
        xc:"#6F4F25" xc:"#433900" xc:"#9A6759" xc:"#444444" \
        xc:"#6C6C6C" xc:"#9AD284" xc:"#6C5EB5" xc:"#959595" +append -scale 500 clut.png

which gives you this

enter image description here

You can then use that CLUT like this, but it doesn't help

convert -page 312x144+0+0 "scacchiera.png" -page +168+0      \
"level-000.png" -background black -layers flatten             \
-remap clut.png "joined.png"  

and you can also use it like this but that doesn't work either

convert -page 312x144+0+0 "scacchiera.png" -page +168+0   \
        "level-000.png" -background black -layers flatten \
        clut.png -clut "joined.png" 

You can also try to tell ImageMagick to preserve the colormap like this, but I'll leave you to guess whether that works :-(

convert -define png:preserve-colormap scacchiera.png ...
convert -define png:preserve-colormap=1 scacchiera.png png00:out.png
convert -define png:preserve-colormap=true scacchiera.png png00:out.png

So, I am stuck too :-)

Original Answer

You don't say how you want to join your images, nor provide any images, nor show what you have tried, so you may not get a good answer. This may be close though...

convert 1.png 2.png -append -remap 1.png result.png
like image 117
Mark Setchell Avatar answered Nov 12 '22 21:11

Mark Setchell