Page 1 of 1

How to save PNG without background tag?

Posted: 2010-12-22T06:07:14-07:00
by Oldes
Hi,

I was searching for solution but found no answer yet so I'm asking here, how to save PNG file which does not have the background chunk.
I'm looking for solution which would work with MagickWand, but I would like to see CMD like as well...

The problem can be described like:
I have PNG image with structure:
>> png/parse %klic.png

Code: Select all

IHDR    HEADER is an object of value:
   size            pair!     35x15
   bitDepth        integer!  8
   colorType       integer!  6
   compression     integer!  0
   filter          integer!  0
   interlace       logic!    false

IDAT    length: 1258
IEND
when I do:

Code: Select all

convert klic.png klic-new.png
I get:

Code: Select all

>> png/parse %klic-new.png
IHDR    HEADER is an object of value:
   size            pair!     35x15
   bitDepth        integer!  8
   colorType       integer!  6
   compression     integer!  0
   filter          integer!  0
   interlace       logic!    false

bKGD    #{00FF00FF00FF}
pHYs    PHYS is an object of value:
   x               integer!  72
   y               integer!  72
   unit            integer!  0

vpAg    VPAG is an object of value:
   x               integer!  35
   y               integer!  15
   unit            integer!  0

IDAT    length: 1244
tEXt    "date:create^@2010-12-10T23:11:42+01:00"
tEXt    "date:modify^@2010-12-10T19:56:14+01:00"
IEND
I don't care about pHYs and vpAg tags at this moment, but IM also adds bKGD tag which affects how the image looks like when I use it in soft where I need it. Is there any solution?
Btw, I consider it to be a bug, when I resave same image, I expect to have same image again.

Tested with ImageMagick 6.6.6-0 2010-11-21 Q16

Re: How to save PNG without background tag?

Posted: 2010-12-22T06:46:04-07:00
by glennrp
I've been thinking about putting in a "-define PNG:something" to avoid
writing the bKGD and other PNG ancillary chunks. In the meantime you
can postprocess your PNG files with "pngcrush -rem bkgd" or "pngcrush -rem alla"
to get rid of them.

How about "-define PNG:bkgd=no", PNG:iccp=no", and "-define PNG:ancillary=no" ?

Re: How to save PNG without background tag?

Posted: 2010-12-22T06:53:37-07:00
by Oldes
Still same result using:

Code: Select all

convert klic.png -define PNG:bkgd=no klic-new2.png
Also note that I would like to use MagickWand finally, I'm not sure how to use define with magick api. And I'm not interested in post processing, I want good IM functionality.

Re: How to save PNG without background tag?

Posted: 2010-12-22T16:58:39-07:00
by anthony
Glennrp was suggesting posible future additions to IM to allow it to remove these chunks.

His temporary solution was to use the non-IM "pngcrush" program
http://pmt.sourceforge.net/pngcrush/

Re: How to save PNG without background tag?

Posted: 2010-12-23T03:07:27-07:00
by Oldes
anthony wrote:Glennrp was suggesting posible future additions to IM to allow it to remove these chunks.
So there is no way how not to write these chunks at this moment?

Re: How to save PNG without background tag?

Posted: 2010-12-23T09:39:11-07:00
by glennrp
I've added options to take care of this request.

The -strip option now removes all PNG ancillary chunks, except for the gAMA chunk when
gamma is not 1/2.2, and the tRNS chunk which I do not consider optional.

For finer-grained control we now have, for example
  • -define PNG:exclude-chunk=bkgd,chrm (comma-separated list or =all or =none)
    -define PNG:include-chunk=gama
The new stuff is checked into the SVN repository and will show up shortly in IM-6.6.6-7 or 6.6.6-8.

Re: How to save PNG without background tag?

Posted: 2010-12-23T10:53:44-07:00
by fmw42
glennrp wrote:-define PNG:include-chunk=gama
Glenn,

Are your purposely misspelling gamma or is that a typo?

Fred

Re: How to save PNG without background tag?

Posted: 2010-12-23T13:33:21-07:00
by magick
You can set options, properties, and artifacts with the MagickWand API methods MagickSetOption(), MagickSetImageProperty(), and MagickSetImageArtifact() respectively. Depending on your context, choose one to exclude PNG chunks, for example, MagickSetOption(wand,"png:exclude-chunk","bkgd"). Excluding PNG chunks requires ImageMagick 6.6.6-7, available within a few days.

Re: How to save PNG without background tag?

Posted: 2010-12-23T14:23:03-07:00
by Oldes
Thank you very much!

Re: How to save PNG without background tag?

Posted: 2010-12-24T05:43:41-07:00
by glennrp
fmw42 wrote:
glennrp wrote:-define PNG:include-chunk=gama
Are your purposely misspelling gamma or is that a typo?
PNG chunk names have 4 letters. The gamma chunk is "gAMA".
In ImageMagick the "define" keywords and values are case-insensitive,
so I could have said PNG or png and =gama or =gAMA.

What's easier to remember and type is "-strip" which accomplishes
"-define png:include-chunk=none,gama".

Glenn

Re: How to save PNG without background tag?

Posted: 2010-12-24T10:25:30-07:00
by fmw42
Glenn,

Thanks for clarifying the PNG naming conventions. Sorry for the false alarm

Fred

Re: How to save PNG without background tag?

Posted: 2010-12-25T03:01:23-07:00
by Oldes
magick wrote:You can set options, properties, and artifacts with the MagickWand API methods MagickSetOption(), MagickSetImageProperty(), and MagickSetImageArtifact() respectively. Depending on your context, choose one to exclude PNG chunks, for example, MagickSetOption(wand,"png:exclude-chunk","bkgd"). Excluding PNG chunks requires ImageMagick 6.6.6-7, available within a few days.
I'm testing it now with the latest version, it's working but when I use:

Code: Select all

MagickSetOption(current_wand, "png:exclude-chunk", "bkgd");
MagickWriteImage(current_wand, "png32:klic2.png");
than the png-defines are not used. Is this a bug? Also is there a way how to force IM to write PNG as png32 without the name prefix used above?