ImageMagick 6.8.4 darkening grayscale images on convert

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

ImageMagick 6.8.4 darkening grayscale images on convert

Post by jhuckaby »

Hello,

ImageMagick 6.8.4 seems to be significantly darkening my grayscale images, when I simply run them through 'convert'. Consider these two sample grayscale images:

https://dl.dropbox.com/u/4585115/imagem ... y_ramp.jpg
https://dl.dropbox.com/u/4585115/imagem ... urated.jpg

When I simply convert them from JPEG to JPEG using ImageMagick 6.8.4, they become much darker:

convert gray_ramp.jpg gray_ramp_output.jpg
convert rgb-desaturated.jpg rgb-desaturated-output.jpg

Outputs:

https://dl.dropbox.com/u/4585115/imagem ... output.jpg
https://dl.dropbox.com/u/4585115/imagem ... output.jpg

I am running:

Version: ImageMagick 6.8.4-5 2013-04-01 Q8 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC
Delegates: bzlib freetype jng jpeg lcms png tiff xml zlib
OS: Linux 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux (64-bit CentOS 6.2)

I have used this same set of sample images for all my testing for many years, and have never seen this problem before.

Now, here is where it gets interesting. I have two other grayscale images which strangely do NOT show the problem with a simple 'convert':

https://dl.dropbox.com/u/4585115/imagem ... e-8bit.tif
https://dl.dropbox.com/u/4585115/imagem ... le-jpg.jpg

They come through just fine, and look the same on the output side. However, they DO become darker when they are composited onto a blank canvas:

Using simple Image::Magick Perl script for this test:
https://dl.dropbox.com/u/4585115/imagem ... mposite.pl

./composite.pl grayscale-jpg.jpg 743x499 xc:white grayscale-jpg-output.jpg
./composite.pl grayscale-8bit.tif 128x96 xc:white grayscale-8bit-output.jpg

Outputs:

https://dl.dropbox.com/u/4585115/imagem ... output.jpg
https://dl.dropbox.com/u/4585115/imagem ... output.jpg

I suspect the difference in the behavior has to do with the fact that the first 2 images are actually RGB but "desaturated", while the latter two are in "true" grayscale colorspace. But either way, I believe ImageMagick should not be darkening any of them.

Here are all the ImageMagick versions I have tested this on so far:

ImageMagick 6.8.4-5 Q8 - Bug is reproducible
ImageMagick 6.8.3-10 Q8 - NOT REPRODUCIBLE

So it looks like the problem was introduced sometime after ImageMagick 6.8.3-10.

Thank you.

- Joe
Last edited by jhuckaby on 2013-04-02T15:31:48-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by fmw42 »

ImageMagick 6.8.4 seems to be significantly darkening my grayscale images, when I simply run them through 'convert'. Consider these two sample grayscale images:
That is a result of changes made some while ago. Add -set colorspace RGB as per

http://www.imagemagick.org/script/forma ... colorspace
viewtopic.php?f=4&t=21269
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by GreenKoopa »

This may be similar to my problem a few days ago. See this Bugs forum post from 2013-03-30 for possible beta fix and workaround.
viewtopic.php?f=3&t=23076
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by jhuckaby »

fmw42: Adding -colorspace RGB has no effect. The images are still very dark. I tried it on the command line, as well as in the Perl script. The forum you linked to is talking about changes made in ImageMagick 6.7.7-8. This bug I am talking about here is much more recent, introduced in IM 6.8.4. ImageMagick 6.8.3 does not have the bug.

GreenKoopa: Thanks, I believe it may be related, yes.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by snibgo »

With 6.8.4-0, this ...

Code: Select all

convert gray_ramp.jpg g.jpg
... gives no problems.

With 6.8.4-6, the same command gives a result that is darker. To correct it (almost entirely; the remaining small difference is probably the JPG compression), I can do:

Code: Select all

"%IMG6846%convert" gray_ramp.jpg -set colorspace RGB g.jpg
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by GreenKoopa »

I only thought it may be related because I had problems with grayscale images that began with a recent IM version. I added -set colorspace sRGB and -set colorspace RGB in various locations. I often understand why they work in hindsight, but don't have the understanding to foresee their need in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by fmw42 »

jhuckaby wrote:fmw42: Adding -colorspace RGB has no effect. The images are still very dark. I tried it on the command line, as well as in the Perl script. The forum you linked to is talking about changes made in ImageMagick 6.7.7-8. This bug I am talking about here is much more recent, introduced in IM 6.8.4. ImageMagick 6.8.3 does not have the bug.

GreenKoopa: Thanks, I believe it may be related, yes.
There have been some changes and bugs fixed since 6.8.4.2. Please try upgrading to the current 6.8.4.6 or the .7beta which has another grayscale bug fixed.
Last edited by fmw42 on 2013-04-02T16:48:51-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by snibgo »

The superfluous conversion seems to occur while writing JPGs. v6.8.4-6 on Windows 7:

Code: Select all

convert gray_ramp.jpg g.png
convert g.png g2.png
convert g2.png g2.jpg
g.png and g2.png are visually the same as gray_ramp.png. g2.jpg is darker. Inserting "set colorspace RGB" in the final convert prevents the darkening.

"identify" says gray_ramp.jpg is colorspace sRGB, and g2.jpg is colorspace Gray. The intermediate files g.png and g2.png are both sRGB.
snibgo's IM pages: im.snibgo.com
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by jhuckaby »

fmw42: I'm sorry to report, the bug still exists in IM 6.8.4-6.

snibgo: Thank you, that does work around the problem on the command line (I had the syntax wrong before). However, I cannot seem to make this work using Perl Image::Magick:

use Image::Magick;
my $img = new Image::Magick();
$img->Read( 'gray_ramp.jpg' );
$img->Set( colorspace => 'RGB' );
$img->Write( 'still-dark.jpg' );

That still results in a darkened image. Also, I have an application where I cannot simply convert every image to RGB colorspace on read. I think this should work how it did in ImageMagick 6.8.3 and below. I don't understand the change in 6.8.4 where SOME grayscale images are now darker, and some are fine. This doesn't make sense to me.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by fmw42 »

fmw42: I'm sorry to report, the bug still exists in IM 6.8.4-6.
Tell me which of your images to download and the process you are trying achieve or command line commands. I will investigate.
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by jhuckaby »

fmw42: Sure thing, here is the simplest test case:

Download this image:
https://dl.dropbox.com/u/4585115/imagem ... y_ramp.jpg

Run this command:
convert gray_ramp.jpg output.jpg

Try to view output.jpg. It will be much darker than the original. In fact, you will see that most of the shades of grey below 50% are totally black.

Now, I realize I can add "-set colorspace RGB" to the command to work around this, but that was not needed in ImageMagick 6.8.3. Something has changed in ImageMagick 6.8.4 to mess with some images.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by snibgo »

Sorry, I know nothing about Perl Image::Magick.

Certainly, something has changed between 6.8.4-0 and 6.8.4-6 (to name two versions that I have available). However, I fear the IM developers wouldn't call it a bug, but a feature. Or even a bug fix. The fact that I don't like this feature is neither here nor there.

As I mentioned in my previous post, identify says that gray_ramp.jpg is colorspace sRGB, but the final output g2.jpg is colorspace Gray. IM developers say Gray colorspace images should be RGB, not sRGB. So the final image is converted to RGB. But other programs don't follow this convention, and think g2.jpg is in sRGB colorspace, and display it accordingly.

I don't like this because I don't see why IM has to convert to RGB. From some flags in gray_ramp.jpg, it knows this file is sRGB. It could set the same flags in g2.jpg.

If it insists on converting, why doesn't it do so for my first command (convert gray_ramp.jpg g.png)? It quite happily creates g.png in sRGB.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by fmw42 »

I will test your image shortly. But note there have been some major changes between IM 6.8.3.x and 6.8.4.x. Those changes caused problems with grayscale images and have had to be worked out over those releases. So it is quite likely that you found a release where things were not working properly and you may not have had to use -set colorspace RGB. I have caught a number of the issues myself and notified Magick who has fixed each one right away. You might review the changelog to see what has changed when. http://www.imagemagick.org/script/changelog.php

Adding -set colorspace RGB has been the right thing to do since about 6.7.8.3, when all grayscale images started to be treated as linear grayscale to honor standards. But as I said above some things got broken and have had to be fixed as each one was caught.

Your image is an sRGB grayscale image with gamma=0.4545. You need to use -set colorspace RGB to tell IM not to transform to linear grayscale with gamma=1. But just set the gamma to 1 keeping the nonlinear grayshades as they were.


convert gray_ramp.jpg -set colorspace RGB tmp2.jpg
compare -metric rmse gray_ramp.jpg tmp2.jpg null:
10.5182 (0.000160497)

The difference is due to jpg decompression and recompression.

I am using IM 6.8.4.6 Q16 Mac OSX Snow Leopard.

If you tell me the exact version (3 dots) you are concerned about I can test those versions.

Otherwise, everything is working now as it should.

You can now use the new -intensity function to do the convert and set the linearity or non-linearity appropriately.

This will produce the correct result also.

convert gray_ramp.jpg -intensity rec601luma tmp4.jpg

see http://www.imagemagick.org/script/comma ... #intensity

The introduction of -intensity (6.8.3.10) has not been as smooth as expected and the problems have been ones I have reported recently. I believe that most have now been worked out as of 6.8.4.7 beta.
Last edited by fmw42 on 2013-04-02T18:44:38-07:00, edited 1 time in total.
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by jhuckaby »

fmw42: Thank you very much for that explanation. It makes sense to me now. I had no idea about the gamma setting in the image. If you could, please tell me how to correctly set the colorspace back using PerlMagick. I cannot get that to work no matter what I try, using the same source image:

use Image::Magick;
my $img = new Image::Magick();
$img->Read( 'gray_ramp.jpg' );
$img->Set( colorspace => 'RGB' ); # this seems to have no effect
$img->Write( 'still-dark.jpg' );

This still results in a dark image, even though I am setting the colorspace back to RGB. Am I doing something wrong?

Thanks again for your time.

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

Re: ImageMagick 6.8.4 darkening grayscale images on convert

Post by fmw42 »

I do not know Perlmagick, but your commands seem correct.

Perhaps the changes that have occurred recently have not been ported properly to Perlmagick.

Magick would need to comment here about this.
Post Reply