How do I strip out PNG metadata using PerlMagick?

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
jaslee
Posts: 11
Joined: 2010-02-25T06:03:36-07:00
Authentication code: 8675308

How do I strip out PNG metadata using PerlMagick?

Post by jaslee »

Hi,
I did a lot of research to find out how to strip out the metadata in an output PNG image - this was due to inconsistent display across browsers (Gamma correction in IE!). This is the command I got to work at the command line using ImageMagick:-

Code: Select all

convert input.png -define png:include-chunk=none -set colorspace Gray -separate -average grayscale\output.png
How would I do this in Perlmagick (sorry, I'm a not up-to-speed with Perl but have to modify an existing Perl script)? Is it even possible..

Jason
jaslee
Posts: 11
Joined: 2010-02-25T06:03:36-07:00
Authentication code: 8675308

Re: How do I strip out PNG metadata using PerlMagick?

Post by jaslee »

Searching this forum for answers I got this: viewtopic.php?f=7&t=10176&p=32368&hilit=meta#p32368

...I'm guessing this is a similar issue of wanting to change met-data (?) so I will probably use the same trick of invoking the command line ImageMagick within the existing Perl script - not ideal but if it does the trick.
mrmattnc
Posts: 22
Joined: 2014-02-04T11:23:37-07:00
Authentication code: 6789

Re: How do I strip out PNG metadata using PerlMagick?

Post by mrmattnc »

Have you tried using "strip"? It should nuke all that crap, then just include whatever new profile you want (that's the colorspace, etc you're using) and write to output.

I'm much more familiar with fixing JPG and PDF problems, but if you can post an image you're working with I'll see if I can take a look at it.
jaslee
Posts: 11
Joined: 2010-02-25T06:03:36-07:00
Authentication code: 8675308

Re: How do I strip out PNG metadata using PerlMagick?

Post by jaslee »

Glad to oblige - I have uploaded a grey image to a temp storage here (28 days expire) : http://www.tiikoni.com/tis/view/?id=05800fd

If you view the image in IE and other browsers you will see it is darker in IE - I need to strip out the Gamma metadata to normalize the display consistently with IE.

Thanks
mrmattnc
Posts: 22
Joined: 2014-02-04T11:23:37-07:00
Authentication code: 6789

Re: How do I strip out PNG metadata using PerlMagick?

Post by mrmattnc »

Try this...

My brief understanding is that gamma correction in the browser is being interpreted using the png:gAMA setting -- which you can locate using identify -verbose <image>. It's as if the image is encoded with a gamma setting, then an additional gamma correction is written in with the file that browsers are using.

What I've done is asked PerlMagick to re-encode the image with gamma set to zero. Unfortunately I'm relying on Chromes interpretation in this matter because I don't have an old version of IE to test it on. In IE 10 it looks fine.

Output Image:
Image

Verification via identify:
Image

While I'm not entirely sure whats going on, I think browsers are using the png:gAMA setting to stack on top of whatever the encoded gamma setting is. If this doesn't work for you I'll try and sort out that crazy command you've got into PerlMagick :shock:

Code: Select all

#!/usr/local/bin/perl
use Image::Magick;

$inputimage = $ARGV[0];

$outputimage = Image::Magick->new;

$outputimage->Read("$inputimage");
$outputimage->Set('gamma'=>'0');
$outputimage->Write("output_$inputimage");
jaslee
Posts: 11
Joined: 2010-02-25T06:03:36-07:00
Authentication code: 8675308

Re: How do I strip out PNG metadata using PerlMagick?

Post by jaslee »

Hey thanks for that - it worked, the gamma (or gAMMA as its seem in TweakPNG) meta has been stripped.
jaslee
Posts: 11
Joined: 2010-02-25T06:03:36-07:00
Authentication code: 8675308

Re: How do I strip out PNG metadata using PerlMagick?

Post by jaslee »

Unfortunately I am still getting a darker grayscale (greyscale) image when viewing the output in IE (IE 11 in my case). Only the image created in the command-line version generated the desired grayscale I require - I think the gamma metadata setting was a bit of a red herring - i.e. in my original command-line:-

Code: Select all

convert input.png -define png:include-chunk=none -set colorspace Gray -separate -average grayscale\output.png
This gives the same result:-

Code: Select all

convert input.png -set colorspace Gray -separate -average grayscale\output.png
So if I could re-phrase the question - it would be how to achieve the above command in PerlMagick - I have already tried the following to try and replicate what it is trying to do but still end up with a darker grayscale image:-

Code: Select all

$image->Set(colorspace=>'gray');
$image->Separate();
$image->Average();
I found a mention about darker grayscale images in the ImageMagick Color Management section (http://www.imagemagick.org/script/color-management.php) :-
The second major change treats any grayscale image as linear rather than non-linear, as it was previously. This change is appropriate, since many types of processing requires a linear colorspace. This means that the conversion of a color image to grayscale via -colorspace gray looks darker relative to previous versions of ImageMagick
...but even after 1st converting the image to "linear RGB" and then applying the grayscale I see no difference :(
jaslee
Posts: 11
Joined: 2010-02-25T06:03:36-07:00
Authentication code: 8675308

Re: How do I strip out PNG metadata using PerlMagick?

Post by jaslee »

This worked for me in the end - I just explicitly applied gamma to the image itself to lighten the resulting greyscale image BUT I still needed to strip out the gAMA setting int he header:-

Code: Select all

$image->Set(type=>'grayscale');
$image->Gamma(1.8);
$image->Set('gamma'=>'0');
mrmattnc
Posts: 22
Joined: 2014-02-04T11:23:37-07:00
Authentication code: 6789

Re: How do I strip out PNG metadata using PerlMagick?

Post by mrmattnc »

Glad you got it fixed!

If there's one thing I've discovered it's that IM and particularly PerlMagick have strange things happen on occasion that don't make much sense. Luckily there's almost always more than one way to skin a cat. :D
Phantom7
Posts: 5
Joined: 2014-02-10T15:31:48-07:00
Authentication code: 6789

Re: How do I strip out PNG metadata using PerlMagick?

Post by Phantom7 »

jaslee wrote:This worked for me in the end - I just explicitly applied gamma to the image itself to lighten the resulting greyscale image BUT I still needed to strip out the gAMA setting int he header:-

I´ve tested your image.png testfile.

Code: Select all

$:identify image.png
image.png PNG 256x256 256x256+0+0 [color=#FF0000][b]8-bit[/b][/color][color=#FF0000] [b]RGB 256c[/b][/color] 5.01KB 0.000u 0:00.000
It´s a question of creativity...

http://www.imagemagick.org/script/perl- ... -attribute
http://search.cpan.org/~exiftool/Image-ExifTool-9.46/
http://www.libpng.org/pub/png/spec/1.2/ ... endix.html
PNG Tags

Tags extracted from PNG images. See http://www.libpng.org/pub/png/spec/1.2/ for the official PNG 1.2 specification.

Tag ID Tag Name Writable
------ -------- --------
'IHDR' ImageHeader PNG ImageHeader
'PLTE' Palette N
'bKGD' BackgroundColor N
'cHRM' PrimaryChromaticities PNG PrimaryChromaticities
'dSIG' DigitalSignature N
'fRAc' FractalParameters N
'gAMA' Gamma N
'gIFg' GIFGraphicControlExtension N
'gIFt' GIFPlainTextExtension N
'gIFx' GIFApplicationExtension N
'hIST' PaletteHistogram N
'iCCP' ICC_Profile ICC_Profile
'iCCP-name' ProfileName N
'iTXt' InternationalText PNG TextualData
'oFFs' ImageOffset N
'pCAL' PixelCalibration N
'pHYs' PhysicalPixel PNG PhysicalPixel
'sBIT' SignificantBits N
'sCAL' SubjectScale PNG SubjectScale
'sPLT' SuggestedPalette N
'sRGB' SRGBRendering N
'sTER' StereoImage PNG StereoImage
'tEXt' TextualData PNG TextualData
'tIME' ModifyDate Y
'tRNS' Transparency N
'tXMP' XMP XMP
'vpAg' VirtualPage PNG VirtualPage
'zTXt' CompressedText PNG TextualData

PNG ImageHeader Tags

Index1 Tag Name Writable
------ -------- --------
0 ImageWidth N
4 ImageHeight N
8 BitDepth N
9 ColorType N
10 Compression N
11 Filter N
12 Interlace N

PNG PrimaryChromaticities Tags

Index4 Tag Name Writable
------ -------- --------
0 WhitePointX N
1 WhitePointY N
2 RedX N
3 RedY N
4 GreenX N
5 GreenY N
6 BlueX N
7 BlueY N

PNG TextualData Tags

The PNG TextualData format allows arbitrary tag names to be used. The tags listed below are the only ones that can be written (unless new user-defined tags are added via the configuration file), however ExifTool will extract any other TextualData tags that are found.

These tags may be stored as tEXt, zTXt or iTXt chunks in the PNG image. By default ExifTool writes new string-value tags as as uncompressed tEXt, or compressed zTXt if the Compress (-z) option is used and Compress::Zlib is available. Alternate language tags and values containing special characters (unless the Latin character set is used) are written as iTXt, and compressed if the Compress option is used and Compress::Zlib is available. Raw profile information is always created as compressed zTXt if Compress::Zlib is available, or tEXt otherwise. Standard XMP is written as uncompressed iTXt.

Alternate languages are accessed by suffixing the tag name with a '-', followed by an RFC 3066 language code (ie. "PNG:Comment-fr", or "Title-en-US"). See http://www.ietf.org/rfc/rfc3066.txt for the RFC 3066 specification.

Some of the tags below are not registered as part of the PNG specification, but are included here because they are generated by other software such as ImageMagick.

Tag ID Tag Name Writable
------ -------- --------
'Artist' Artist string
'Author' Author string
'Comment' Comment string
'Copyright' Copyright string
'Creation Time' CreationTime string
'Description' Description string
'Disclaimer' Disclaimer string
'Document' Document string
'Label' Label string
'Make' Make string
'Model' Model string
'Raw profile type 8bim' Photoshop_Profile Photoshop
'Raw profile type APP1' APP1_Profile EXIF
XMP
'Raw profile type exif' EXIF_Profile EXIF
'Raw profile type icc' ICC_Profile ICC_Profile
'Raw profile type icm' ICC_Profile ICC_Profile
'Raw profile type iptc' IPTC_Profile Photoshop
'Raw profile type xmp' XMP_Profile XMP
'Software' Software string
'Source' Source string
'TimeStamp' TimeStamp string
'Title' Title string
'URL' URL string
'Warning' PNGWarning string
'XML:com.adobe.xmp' XMP XMP
'create-date' CreateDate string
'modify-date' ModDate string

PNG PhysicalPixel Tags

Index1 Tag Name Writable
------ -------- --------
0 PixelsPerUnitX N
4 PixelsPerUnitY N
8 PixelUnits N

PNG SubjectScale Tags

Index1 Tag Name Writable
------ -------- --------
0 SubjectUnits N
1 SubjectPixelWidth N
2 SubjectPixelHeight N

PNG StereoImage Tags

Index1 Tag Name Writable
------ -------- --------
0 StereoMode N

PNG VirtualPage Tags

Index4 Tag Name Writable
------ -------- --------
0 VirtualImageWidth N
1 VirtualImageHeight N
2 VirtualPageUnits N
Post Reply