Page 1 of 3

[SOLVED] multi-colour gradient, dreadful banding

Posted: 2012-07-30T17:00:33-07:00
by Draoidh
I have created a gradient as per http://www.imagemagick.org/Usage/canvas ... nterpolate:

Code: Select all

( -size 1100x742 gradient: -interpolate Bicubic -rotate 180 ( +size xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append ) -clut )
It displays terrible banding. The code works great for 2-colour gradients. Can the result be improved?
The code needs to be generic, i.e. work equally well for 2 or more colours, as I use a script to generate gradients.

It looks much better when applied in CSS:

Code: Select all

-moz-linear-gradient(top, #0c0126 7%, #19022a 12%, #27032e 23%, #43013a 38%, #5f0046 44%, #7e003a 54%, #9e002f 61%, #ce1129 71%, #fe2323 77%, #f6441a 82%, #ee6611 87%, #f68617 93%, #ffa61e 98%) repeat scroll 0 0 transparent

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-30T17:46:49-07:00
by fmw42
( -size 1100x742 gradient: -interpolate Bicubic -rotate 180 ( +size xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append ) -clut )
It is hard to test if you don't show your full command line. And I am not sure your syntax is proper for IM 6. Also you do not specify what version of IM you are using (and I presume windows)


Your command seems to work fine for me on IM 6.7.8.6 Q16 Mac OSX Snow Leopard (unix) with no banding that I can see:

convert \( -size 1100x742 gradient: -interpolate Bicubic -rotate 180 \
\( +size xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a \
xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append \) -clut \) \
1tmp1.png


But I would have written it as follows, each of which produces a slightly different graduation of colors:

convert \( -size 1100x742 gradient: -rotate 180 \) \
\( -size 1x1 xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a \
xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append \) \
-interpolate Bicubic -clut \
1tmp2.png

or

convert \( -size 1100x742 gradient:"gray(255)-gray(0)" -rotate 180 \) \
\( -size 1x1 xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a \
xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append \) \
-interpolate Bicubic -clut \
1tmp3.png


Note that gradient: has undergone some changes in recent version. They can be either linear or non-linear (sRGB). That depends upon how you specify your values for the gradient colors. The default gradient: is non-linear as is gradient:"white-black" and gradient:"gray100-gray0", but gradient:"gray(255)-gray(0)" is linear as are gradients using -icc-color for grays.

see
http://www.imagemagick.org/script/color.php

Anthony can say for sure, but I think I recall that -interpolate bicubic may have changed the formula that is used for the bicubic.

P.S. see viewtopic.php?f=2&t=21118&start=45#p86006

But I do not know if he means that bicubic is now using catrom or bicubic is deprecated and replaced by catrom. In any event, the list of interplation options does not list catrom.

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-30T20:23:48-07:00
by fmw42
By the way, you don't identify what your output image format is. That may be important if you are converting to 8-bit color (e.g. gif) rather than 24-bit color or you have some kind of compression on (such as jpg).

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T02:57:45-07:00
by Draoidh
Apologies for my incomplete post well past the midnight hour.

I am not on Winblows; I run Version: ImageMagick 6.7.7-2 2012-05-30 Q16 on Debian wheezy.

The complete command:

Code: Select all

convert \( -size 400x300 gradient: -interpolate Bicubic -rotate 180 \( +size xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append \) -clut \) x.png
Moving -interpolate Bicubic as you suggested has no effect.

Image

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T10:25:00-07:00
by fmw42
I do not see the banding in IM 6.7.8.6 Q16 with either


convert \( -size 400x300 gradient: -rotate 180 \) \
\( -size 1x1 xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a \
xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append \) \
-interpolate Bicubic -clut \
1tmp2.png

convert \( -size 400x300 gradient:"gray(255)-gray(0)" -rotate 180 \) \
\( -size 1x1 xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a \
xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -append \) \
-interpolate Bicubic -clut \
1tmp3.png


I suspect it is the PNG. Try TIFF for output and see if that cures it. If so, then it is the fact that PNG has been undergoing changes as well for quite some release and you may just have one release with trouble. Also see about updating your libpng perhaps. I am using libpng 1.4.11

you can see what version of png you have from

convert -list format

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T10:50:08-07:00
by Draoidh
Good morning Fred,
I just tried TIFF output - why did that not occur to me before? The gradient is perfect. Then, I simply converted this to PNG:

Code: Select all

convert x.tiff x.png
This also produces a perfect gradient (looks identical to TIFF).

I use libpng 1.4. But if TIFF > PNG is fine, the PNG library can hardly be the cause, can it?

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T10:55:53-07:00
by fmw42
Draoidh wrote:Good morning Fred,
I just tried TIFF output - why did that not occur to me before? The gradient is perfect. Then, I simply converted this to PNG:

Code: Select all

convert x.tiff x.png
This also produces a perfect gradient (looks identical to TIFF).

I use libpng 1.4. But if TIFF > PNG is fine, the PNG library can hardly be the cause, can it?

But if it works, then the code is fine. I still think it is a PNG issue. The colorspace or other png properties way be different when creating directly rather than converting from TIFF. For example look at the verbose info from both PNGs and see if the direct one is 8-bit palette and not 24-bit truecolor. That is most likely the issue as my best guess. And would imply some upgrade of IM to get any fixes to PNG that might have occurred. You can often see relevant changes by reviewing the changelog http://www.imagemagick.org/script/changelog.php

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T11:00:29-07:00
by Draoidh
Oops. I just realized I still had the following in my command history from earlier this morning:

Code: Select all

-interpolate Bilinear 
That was just a clueless stab in the dark. The TIFF I produced was done with -interpolate Bilinear.

Using -interpolate Bicubic with TIFF produces the same banding as with PNG.

Producing a PNG with -interpolate Bilinear gives a smooth gradient.

Now, why I didn't notice this this morning is another question.

It's IM's Bicubic algorithm, I'm afraid.

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T11:08:37-07:00
by Draoidh
I see the bicubic algorithm was replaced in 6.7.7-6 (I have 6.7.7-2).

6.7.7-10 available in Debian wheezy. I'll give that a try.

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T11:45:43-07:00
by Draoidh
I installed ImageMagick 6.7.7-10 2012-06-29 Q16 and reran the command with -interpolate Bicubic.

The gradient is smooth but looks completely different:
Image

The colour distribution is the same with -interpolate Bilinear.

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T12:11:20-07:00
by Draoidh
Image

My gradient has 13 different colours. I would expect the stops to be evenly distributed. With a height of 300px, that would be every 25px.

At the half-way mark (150px), we have #F83E1B, which is between colour 9 and 10. I would have expcted colour 7.
At 100px, I get #E01726, which is between colour 8 and 9. I would have expected colour 4.

In fact, looking at the gradient produced with the earlier IM version, because of the banding you can see the stops, and they are evenly spaced.

How can I get an even distribution?

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T12:44:42-07:00
by Draoidh
To illustrate my point, I have reversed the order of the colours and then NOT rotated the image by 180 degrees.

Sorry if this sounds back to front, but if you look at my previous code, I rotated the image by 180 degrees so that the colour stops read from left to right produce a top-down vertical gradient.

The new code:

Code: Select all

convert  \( -size 400x300 gradient:   -interpolate Bicubic \( +size xc:#0c0126 xc:#19022a xc:#27032e xc:#43013a xc:#5f0046 xc:#7e003a xc:#9e002f xc:#ce1129 xc:#fe2323 xc:#f6441a xc:#ee6611 xc:#f68617 xc:#ffa61e -reverse -append \)   -clut \) x.png
I would expect this image to be identical to the other one. In fact, this was definitely the case with the old Bicubic algorithm. I had originally appended my canvases with -reverse but latterly rotated instead and the results were identical.

Now we get completely different images:
Image

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T13:54:12-07:00
by fmw42
try gradient:"gray(255)-gray(0)" that is making the gradient linear rather than sRGB. see if that works. Otherwise, you may need to hear from Anthony about this.

Try my two commands above and see if they make any difference. My results are quite different from yours.

I think the problem is that gradient was undergoing quite some changes to get it right about then. I think you may need to upgrade to or beyond IM 6.7.8.3.

Here is what I got with slight differences using linear and non-linear gradients:

Image

Image

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T15:20:55-07:00
by Draoidh
Fred, the first of the two images you posted looks linear and is the equivalent of my first picture with the banding, just without the banding.

I have no idea what your second picture is. The colours are totaly different.

Anyway, I can't upgrade to a higher version. 6.7.7.10 is the latest greatest, even in Debian unstable.

I have moved back to 6.7.7.2. With -interpolation Bilinear, that gives me a linear gradient without banding.

The only thing is that Bilinear doesn't give me a smooth result in conjunction with -distort Arc.

Re: multi-colour gradient, dreadful banding

Posted: 2012-07-31T18:10:13-07:00
by fmw42
[quote="Draoidh"]Fred, the first of the two images you posted looks linear and is the equivalent of my first picture with the banding, just without the banding.

I have no idea what your second picture is. The colours are totaly different.

[/color]


The first was using gradient: (which is non-linear). The second was using gradient:"gray(255)-gray(0)" which is linear. There are going to be differences when you apply the color clut to the linear or non-linear grayscale gradients.

My understanding is that the old bicubic interpolation was found to be using a cardinal cubic spline and was thought not to be very useful and was deprecated. I am not sure if the deprecated version is still using the cardinal cubic spline or the catmull-rom cubic spline.

Have you tried -interpolate spline? I am not sure what it does. Or as a guess try and see if this is even allowed -- -interpolate catrom and just missing from the list.


Anthony will have to answer these questions.