[SOLVED] multi-colour gradient, dreadful banding

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?".
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

[SOLVED] multi-colour gradient, dreadful banding

Post 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
Last edited by Draoidh on 2012-09-29T14:58:11-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: multi-colour gradient, dreadful banding

Post 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.
Last edited by fmw42 on 2012-07-30T20:56:47-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: multi-colour gradient, dreadful banding

Post 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).
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: multi-colour gradient, dreadful banding

Post 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
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: multi-colour gradient, dreadful banding

Post 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
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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.
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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.
Last edited by Draoidh on 2012-07-31T12:55:08-07:00, edited 1 time in total.
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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.
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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?
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: multi-colour gradient, dreadful banding

Post 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
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: multi-colour gradient, dreadful banding

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: multi-colour gradient, dreadful banding

Post 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.
Post Reply