saving output as png makes image dark

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
davidosullivan
Posts: 5
Joined: 2013-02-24T14:15:16-07:00
Authentication code: 6789

saving output as png makes image dark

Post by davidosullivan »

Hi there,

I am using imagemagick in a plugin for wordpress (so I am not using the command line but doing things through PHP).

Everything seems to be working quite well except that with some images when I save then as png they end up very dark, if I save the image as jpg it is fine. The reason I need to save as png is because the settings the user makes on the image may result in there being empty space in the image that needs to be transparent (they may rotate the image inside its frame by 45degrees for example).

Does anyone here know what may be happening and what I might be able to do to solve it? I have tried $image->setColorspace(Imagick::COLORSPACE_SRGB); but that is not resolving the issue....

Examples the first one is correct the second one the result of saving as png

Image
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: saving output as png makes image dark

Post by fmw42 »

grayscale images are treated now since about IM 6.8.0.3 as linear (gamma=1) rather than non-linear (gamma 0.4545). See

http://www.imagemagick.org/script/forma ... colorspace
davidosullivan
Posts: 5
Joined: 2013-02-24T14:15:16-07:00
Authentication code: 6789

Re: saving output as png makes image dark

Post by davidosullivan »

Thanks so much for getting back to me ;)

I had look at that page but it does not seem very clear what I should actually do to fix the issue...

I am guessing I need to run some test for grayscale and if so set some parameter or other...

Any tips on how I might achieve this using the php commands (i.e. by running commands on the image object provided by $image = new Imagick( $img_path );) ?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: saving output as png makes image dark

Post by Bonzo »

This worked for me using exec( ) but I do not know how to do it with Imagick.

Code: Select all

<?php

$colorspace = exec("identify -format %[colorspace] input.jpg ");

echo "colorspace = $colorspace";

?>
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: saving output as png makes image dark

Post by fmw42 »

Your image is sRGB jpg with all 3 channels the same. I had no trouble doing:

convert IMG_9121-HS-3-@iCE@1722101722100006081.jpg test2.png

or

convert IMG_9121-HS-3-@iCE@1722101722100006081.jpg -colorspace sRGB test1.png

and the result looks just like the input.

What version of IM are you using and what platform? You may have a transition version of IM that did not have all the colorspace changes working correctly.

I am running IM 6.8.3.4 Q16 Mac OSX Snow Leopard
davidosullivan
Posts: 5
Joined: 2013-02-24T14:15:16-07:00
Authentication code: 6789

Re: saving output as png makes image dark

Post by davidosullivan »

ok well setting $image->setColorspace(Imagick::COLORSPACE_GRAY);
$image->setImageColorspace(Imagick::COLORSPACE_GRAY); makes the output correct, only problem is that $image->getColorspace(); returns COLORSPACE_UNDEFINED so I cannot tell if the image is greyscale or not...
davidosullivan
Posts: 5
Joined: 2013-02-24T14:15:16-07:00
Authentication code: 6789

Re: saving output as png makes image dark

Post by davidosullivan »

Ok cracked it, setting the colorspace to rgb rather than srgb seems to work for everything...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: saving output as png makes image dark

Post by fmw42 »

That is the standard solution now for converting to grayscale. First convert to linear RGB, then convert to grayscale. But for color PNG or JPG, you will get an RGB listing in the meta file rather than sRGB. So some viewers may not like the RGB rather than sRGB.
davidosullivan
Posts: 5
Joined: 2013-02-24T14:15:16-07:00
Authentication code: 6789

Re: saving output as png makes image dark

Post by davidosullivan »

Yeah so actually now I have the opposite problem, color images or grayscale images that have been rotated and then cropped are coming out too light...

So are we saying color images need to be sRGB and greyscale RGB? If so how can I test if an image is greyscale? get colorspace just returned undefined...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: saving output as png makes image dark

Post by fmw42 »

What command are you using that makes color images too light.

The issue for grayscale images, is that IM tries to convert them to linear gray (for standards consistency) when most viewers do not want that. So setting the colorspace to RGB before -colorspace gray is just telling IM that it is to be treated as already linear and not do the gamma conversion from 0.4545 to 1. What this does is trick IM into keeping the sRGB non-linearity.

But for color images, you do not want to do that.

But your image was an sRGB 3 channel image that had R=G=B. But IM handled it correctly for me. That is why I asked what version of IM you were using. I had no trouble like you did with that image converting correctly without any special commands.

If you really need to distinguish color and gray, you can test for colorspace and type to see if sRGB colorspace or if colorspace or type is grayscale (gray). Then you may have to check if sRGB has R=G=B from the mean and standard deviation values. All that can be extracted from the IM verbose information.

identify -verbose yourimage.

But I am puzzled why you had trouble in the first place, since I did not.
Post Reply