saving output as png makes image dark
-
- Posts: 5
- Joined: 2013-02-24T14:15:16-07:00
- Authentication code: 6789
saving output as png makes image dark
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
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
- 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
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
http://www.imagemagick.org/script/forma ... colorspace
-
- Posts: 5
- Joined: 2013-02-24T14:15:16-07:00
- Authentication code: 6789
Re: saving output as png makes image dark
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 );) ?
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 );) ?
Re: saving output as png makes image dark
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";
?>
- 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
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
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
-
- Posts: 5
- Joined: 2013-02-24T14:15:16-07:00
- Authentication code: 6789
Re: saving output as png makes image dark
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...
$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...
-
- Posts: 5
- Joined: 2013-02-24T14:15:16-07:00
- Authentication code: 6789
Re: saving output as png makes image dark
Ok cracked it, setting the colorspace to rgb rather than srgb seems to work for everything...
- 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
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.
-
- Posts: 5
- Joined: 2013-02-24T14:15:16-07:00
- Authentication code: 6789
Re: saving output as png makes image dark
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...
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...
- 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
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.
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.