Page 1 of 1

PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-04T15:45:39-07:00
by bonzairob
Hi there, I thought this would be a fairly simple question but nothing I've tried so far has worked!

I have a load of png images, which I want to first -trim, and then convert to 16-colour indexed palette png 8s, including a transparency colour.

I got as far as trimming, and almost got transparency. The command was:

Code: Select all

convert source.png -trim -depth 8 -colors 16 result.png
That got me transparency, but all black in the image was also transparent.

I assume it's something to do with the original png being black where it's transparent, and transparency not being a colour in the original for -colors 16 to use. If I knew how, at this point I would probably do this:
Overlay the transparent image onto a (255,0,255) background
Convert to indexed colour
Change (255,0,255) to (255,0,255,0) in the colormap.

How can I do this with command-line Imagemagick?

Thanks very much for any insight!

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-04T17:11:42-07:00
by fmw42
perhaps you could post a link to your image so others can test and see what is going on. Note that PNG has been under quite some development over the last number of releases and all is not fully working correctly, yet, even with the current release. Furthermore you have not identified your IM version and platform. see the changelog at http://www.imagemagick.org/script/changelog.php

try this, it seems to be the temporary workaround I found and posted in another similar topic just recently.

convert logo: logo.png
convert logo.png -transparent white -trim -colors 16 -depth 8 -type palettematte GIF:- | convert - PNG8:logo_tmp.png

If that works, then try with your images.

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-04T17:42:06-07:00
by bonzairob
Ah sorry. It's OS X, via macports. ImageMagick 6.6.7-1.

I'm afraid your workaround didn't work, it made white and black both transparent. :/

I've uploaded my files here. (Bulbasaur!)

I tried out my magenta theory with your script, though - I've been trying with various things, and had a python/pygame script that worked fine in pygame, but exported the image as non-transparent. So I exported a magenta-backgrounded, non-transparent version, ran your script with -tramnsparent magenta and... now magenta and black are both transparent. :(

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-04T19:56:58-07:00
by fmw42
On my Mac OSX Tiger IM 6.6.7.4 Q16 my command worked just fine on your original.png image. It was trimmed to 35x33 pixels, still was transparent, had 16 colors and type palettematte.

Code: Select all

  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 35x33+0+0
  Resolution: 72x72
  Print size: 0.486111x0.458333
  Units: Undefined
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 61.89 (0.242706)
      standard deviation: 60.9231 (0.238914)
      kurtosis: -0.028327
      skewness: 0.860683
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 104.424 (0.409507)
      standard deviation: 90.2587 (0.353956)
      kurtosis: -1.49733
      skewness: 0.099617
    Blue:
      min: 0 (0)
      max: 205 (0.803922)
      mean: 81.5004 (0.31961)
      standard deviation: 72.0088 (0.282388)
      kurtosis: -1.49209
      skewness: 0.232474
    Alpha:
      min: 0 (0)
      max: 255 (1)
      mean: 183.026 (0.717749)
      standard deviation: 114.774 (0.450095)
      kurtosis: -1.06381
      skewness: 0.967569
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 79.9472 (0.313518)
      standard deviation: 86.9147 (0.340842)
      kurtosis: -0.781873
      skewness: 0.73567
  Alpha: none   #00000000
  Histogram:
       326: (  0,  0,  0,  0) #00000000 none
        91: ( 16, 16, 16,255) #101010 rgba(16,16,16,1)
        77: ( 24, 74, 74,255) #184A4A rgba(24,74,74,1)
        39: ( 49,115,115,255) #317373 rgba(49,115,115,1)
       222: ( 57,148,148,255) #399494 rgba(57,148,148,1)
        22: ( 82, 98, 41,255) #526229 rgba(82,98,41,1)
       100: ( 98,213,180,255) #62D5B4 rgba(98,213,180,1)
        76: (115,172, 49,255) #73AC31 rgba(115,172,49,1)
        81: (131,238,197,255) #83EEC5 rgba(131,238,197,1)
        34: (164,213, 65,255) #A4D541 rgba(164,213,65,1)
        23: (172,  0, 49,255) #AC0031 rgba(172,0,49,1)
        39: (189,255,115,255) #BDFF73 rgba(189,255,115,1)
        10: (205,205,205,255) #CDCDCD rgba(205,205,205,1)
         7: (238, 32, 57,255) #EE2039 rgba(238,32,57,1)
         8: (255,106, 98,255) #FF6A62 rgba(255,106,98,1)
  Rendering intent: Undefined
  Interlace: None
  Background color: rgba(16,16,16,1)
  Border color: rgba(223,223,223,1)
  Matte color: grey74
  Transparent color: none
  Compose: Over
  Page geometry: 96x96+28+30
  Origin geometry: +28+30
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2011-02-04T18:54:04-08:00
    date:modify: 2011-02-04T18:54:04-08:00
    signature: f8a5c34c72b7e0291fe6f83ef6cac05b58a7b9e4c54684a1f173eefbfbee1c03
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 644BB
  Number pixels: 1.16KB
  Pixels per second: 57.8KB
  User time: 0.000u
  Elapsed time: 0:01.020
  Version: ImageMagick 6.6.7-4 2011-01-30 Q16 http://www.imagemagick.org
As I said there have been changes in the last number of releases to the PNG format (still a work in progress to fix all the bugs), so as this works in 6.6.7.4, perhaps you should upgrade (though I don't know what version IM MacPorts is using). I have all my delegates installed by MacPorts, but I install IM manually to /usr/local/bin. That way I can keep up with IM and not have to wait for MacPorts to catch up.

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-05T16:10:28-07:00
by bonzairob
Ok, so we have some success! I updated to the latest version, 6.6.7. Your command of setting transparency to white didn't quite work, but this command works fine:

Code: Select all

convert ../../source/1.png -trim -depth 8 -colors 16 -type palettematte GIF:- | convert - PNG8:1.png
However, here's a puzzler for you: I then tried to simply do this on the result, to compare filesizes:

Code: Select all

convert 1.png GIF:1.gif
And the result is 96x96 again. (But, 200 bytes smaller!)

Adding -trim has no effect, nor do some other things I tried, such as adding in the palette/bitdepth stuff, or not bothering with the extra piped command. The gif is always 96x96. Opened in photoshop, it has a white border outside the -trim'd transparency, but in Finder the border is transparent.

I ran identify on the generated 1.png, and got this result:

Code: Select all

1.png PNG 35x33 96x96+28+30 8-bit PseudoClass 16c 648B 0.000u 0:00.010
Does that mean it still thinks it's 96x96? How can I get around that?

Here's the border'd gif (from convert 1.png GIF:1.gif):

Code: Select all

1.gif GIF 35x33 96x96+28+30 8-bit PseudoClass 16c 473B 0.000u 0:00.000
Notice the size stuff. What's going on?

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-05T19:14:58-07:00
by fmw42
Your command of setting transparency to white didn't quite work, but this command works fine:
That was just my example that people could reproduce as logo: has no transparency. So adding -transparent white converts it first to an image where all white is now transparent.

As you found out, if your image already has transparency, then -transparent white is not useful.
convert 1.png GIF:1.gif

And the result is 96x96 again.

How can I get around that?
You have to use the pipe method as I specified to get around it (for now until glennrp can fix the issue).

convert ../../source/1.png -trim -depth 8 -colors 16 -type palettematte GIF:- | convert - PNG8:1.png

should work if your 1.png already has transparency. It should then trim the surrounding transparency (unless there is one non-transparent pixel in the corners or such). Such a command worked for me on your test image.

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-06T06:20:36-07:00
by bonzairob
Oh, the pipe was to get around that border on GIFs? That's annoying.
I've got an updated version up here, so you can see what's going on. Converting the 1.png result to a gif has the same result, is it something to do with the page geometry still being 96x96? Can you force that to something else?

Thanks very much for all your help, fmw42! =D

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-06T11:53:52-07:00
by fmw42
this still works perfectly fine on my Mac OSX Tiger IM 6.6.7.5 Q16


convert original.png -trim -colors 16 -depth 8 -type palettematte GIF:- | convert - PNG8:original_tmp.png


The resulting image is 35x33 pixels.

Yes, the page geometry will still be the original size, so to fix that add +repage after -trim


convert original.png -trim +repage -colors 16 -depth 8 -type palettematte GIF:- | convert - PNG8:original_tmp2.png

see http://www.imagemagick.org/Usage/crop/#crop_repage

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-06T12:18:06-07:00
by bonzairob
Sorry, yeah, that does work fine - but I want to do this with the ultimate output being a GIF file, because it ends up quite a bit smaller.

Re: PNG 32 - PNG 8 with indexed transparency

Posted: 2011-02-06T12:26:01-07:00
by fmw42
this works fine for me

convert original.png -trim +repage -colors 16 -depth 8 -type palettematte original.gif