Honza Javorek
Honza Javorek

Reputation: 8816

Why my ImageMagick performs colortoning with different results than expected?

I found Instagraph PHP library (GitHub, NetTuts) for photo filters. However, I found out on my box it does not output the same images as presented on NetTuts. Because I have no further knowledge of photo filters or ImageMagick's magic, I need help from you, ImageMagick magicians :-)

I localized the issue and it has to be within colortone function, because otherwise filters work well. Let's take Toaster as a showcase, because it performs only one colortone transformation.

Code generated by the colortone PHP function (bash-escaped):

convert test.jpg \( -clone 0 -fill '#330000' -colorize 100% \) \( -clone 0 -colorspace gray -negate \) -compose blend -define compose:args=100,0 -composite test.jpg

The rest of sequence of command to complete the filter (those should be all right):

convert test.jpg -modulate 150,80,100 -gamma 1.2 -contrast -contrast test.jpg
convert test.jpg \( -size 960.0x960.0 radial-gradient:none-LavenderBlush3 -gravity center -crop 640x640+0+0 +repage \) -compose multiply -flatten test.jpg
convert test.jpg \( -size 960.0x960.0 radial-gradient:#ff9966-none -gravity center -crop 640x640+0+0 +repage \) -compose multiply -flatten test.jpg

And now, let's look at the expected result of the Toaster filter:

enter image description here

However, my script produces following:

enter image description here

It is a lot darker, but I have no idea why. There are probably only two options to explain such behavior:

  1. Author of the library has a bug in his library. (I do not think so, but it is still possible...)
  2. My ImageMagick is different than author's and so it produces different results. (How that can happen?)

Has anyone a clue why my ImageMagick produces different results? Could it be some initial settings? My ImageMagick is installed purely from standard Ubuntu package.

$ convert --version
Version: ImageMagick 6.7.7-10 2012-08-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

I compiled my own latest upstream version of ImageMagick and it produced the same, wrong output.

$ convert --version
Version: ImageMagick 6.8.3-9 2013-03-15 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: bzlib djvu mpeg fontconfig freetype jbig jng jp2 jpeg lcms lqr lzma openexr png ps tiff x xml zlib

Upvotes: 3

Views: 977

Answers (1)

Honza Javorek
Honza Javorek

Reputation: 8816

Solution:

convert -set colorspace RGB test.jpg \( -clone 0 -fill '#330000' -colorize 100% \) \( -clone 0 -colorspace gray -negate \) -compose blend -define compose:args=100,0 -composite test.jpg

In English: Add -set colorspace RGB to force the image to be in linear RGB colorspace.


Thanks to guys from ImageMagick forum and Dejan Marjanovic, author of Instagraph for help in my investigation. The issue appeared because ImageMagick changed behavior in version 6.7.7-8 and started to treat images as sRGB by default.

Dejan developed and tested Instagraph with version 6.7.5, which was upstream those days. Filters are written assuming RGB as the default colorspace (previous ImageMagick's default behavior). I tried the same code with ImageMagick 6.7.7-10 and 6.8.3-9 and so I got different output.

If -set colorspace RGB is added, image is converted to linear RGB and filters work properly as they were designed. Another solution (probably better) would be to tweak colortone subprocedure so it works also in sRGB colorspace, but that is far beyond the scope of my current knowledge, so I am pretty okay with this, let's say hotfix.

Upvotes: 3

Related Questions