Convert fuzzed color to second fuzzed color

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?".
Post Reply
f.s.e
Posts: 4
Joined: 2014-03-13T06:55:56-07:00
Authentication code: 6789

Convert fuzzed color to second fuzzed color

Post by f.s.e »

Hello,

I would like to convert a "fuzzed color" = range of colors around(similar to) a single color in an image, to another "fuzzed color" = range of colours similar to a second, single color.

So far i can only convert a "fuzzed color" to a second, fixed color using:

Code: Select all

convert in.jpg -fuzz 10% -fill "#00000" -opaque "#7d7970" out.jpg
In this example, the fuzzed range will be created for color #7d7970. But the target color is not another range, but all black.

I would like the replacing target color to be the closer to black, the closer the replaced color originally was to #7d7970. I.e. I would like it to be a mix with the background.

Here an example from Photoshop:

I replace the brown color (fuzzed) in the middle of the image with a yellow color (the closer to the yellow the closer it was to the brown):

Image

Image

Thank you for your help!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert fuzzed color to second fuzzed color

Post by snibgo »

I'll use your Photoshop example, i2.jpg.

One way of doing this: create a copy of the original, entirely yellow. Composite this over the original, with a mask (diff.png). The mask is light where i2.jpg is close to brown, and dark where i2.jpg is far from brown.

I find the distance in CIELab colorspace, as I find that best for photographs. For graphics, you might prefer to omit "-colorspace Lab".

"-evaluate pow 20" makes the effect tail off quickly. Change to suit.

Windows BAT syntax.

Code: Select all

convert ^
  i2.jpg ^
  ( +clone ^
    -fill #633619 ^
    -colorize 100 ^
  ) ^
  -colorspace Lab ^
  -compose Difference -composite ^
  -grayscale RMS ^
  -set colorspace sRGB ^
  -negate ^
  -evaluate pow 20 ^
  diff.png

convert ^
  i2.jpg ^
  ( +clone ^
    -fill #ffff00 ^
    -colorize 100 ^
  ) ^
  diff.png ^
  -composite ^
  out.png
snibgo's IM pages: im.snibgo.com
f.s.e
Posts: 4
Joined: 2014-03-13T06:55:56-07:00
Authentication code: 6789

Re: Convert fuzzed color to second fuzzed color

Post by f.s.e »

Thank you!! This works very well. I had to change the PNG format to TIFF, because it took ages to write it out.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert fuzzed color to second fuzzed color

Post by snibgo »

Good stuff. Combining the two converts makes it much faster: no need to read the input twice, or to save the mask.

Code: Select all

convert ^
  i2.jpg ^
  ( +clone ^
    -fill #ffff00 ^
    -colorize 100 ^
  ) ^
  ( -clone 0 ^
    ( +clone ^
      -fill #633619 ^
      -colorize 100 ^
    ) ^
    -colorspace Lab ^
    -compose Difference -composite ^
    -grayscale RMS ^
    -set colorspace sRGB ^
    -negate ^
    -evaluate pow 20 ^
  ) ^
  -compose Over -composite ^
  out.png
snibgo's IM pages: im.snibgo.com
Post Reply