How to save PNG without background tag?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Oldes
Posts: 6
Joined: 2010-12-22T05:54:19-07:00
Authentication code: 8675308

How to save PNG without background tag?

Post 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
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to save PNG without background tag?

Post 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" ?
Oldes
Posts: 6
Joined: 2010-12-22T05:54:19-07:00
Authentication code: 8675308

Re: How to save PNG without background tag?

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to save PNG without background tag?

Post 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/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Oldes
Posts: 6
Joined: 2010-12-22T05:54:19-07:00
Authentication code: 8675308

Re: How to save PNG without background tag?

Post 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?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to save PNG without background tag?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save PNG without background tag?

Post by fmw42 »

glennrp wrote:-define PNG:include-chunk=gama
Glenn,

Are your purposely misspelling gamma or is that a typo?

Fred
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: How to save PNG without background tag?

Post 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.
Oldes
Posts: 6
Joined: 2010-12-22T05:54:19-07:00
Authentication code: 8675308

Re: How to save PNG without background tag?

Post by Oldes »

Thank you very much!
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to save PNG without background tag?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save PNG without background tag?

Post by fmw42 »

Glenn,

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

Fred
Oldes
Posts: 6
Joined: 2010-12-22T05:54:19-07:00
Authentication code: 8675308

Re: How to save PNG without background tag?

Post 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?
Post Reply