Photoshop like HUE/Saturation changing

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?".

Photoshop like HUE/Saturation changing

Postby Delphir » 2013-07-10T04:05:23+00:00

Hello, how can I reproduce operations that I do in Photoshop for changing HUE/Saturation (for green and yellow) using the IM commandline? Seems that IM and PS use different gradation of values and work in a different way... maybe somebody can help me?

For example. I need to change (values taken from PS)
"Reds" HUE to +150, Saturation +10, Lightness -50
and
"Yellows" HUE to +50, Saturation +5, Lightness +15

How to reproduce this in IM ?
Delphir
 
Posts: 2
Joined: 2013-07-10T04:00:18+00:00

Re: Photoshop like HUE/Saturation changing

Postby fmw42 » 2013-07-10T09:48:57+00:00

Assuming in PS (as per my old PS CS)

Hps=-180 to 180
Sps=-100 to 100
Lps=-100 to 100

then the conversions would be for IM

H = 100 + (Hps+180)*100/360
S = 100 + Sps
L = 100 + Lps

convert image -modulate L,S,H result

this assumes you are using a version of IM 6.4.0-10 or higher, so that the colorspace is HSL and not HSB for -modulate

EDIT:
This is for all channels. I don't know anything about changing HSL values for a given channel.
Last edited by fmw42 on 2013-07-10T10:58:43+00:00, edited 2 times in total.
User avatar
fmw42
 
Posts: 13868
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: Photoshop like HUE/Saturation changing

Postby snibgo » 2013-07-10T10:32:20+00:00

The OP implies that PS can change the HLS for "reds" or "yellows" only, and that these will give different results.

I don't have PS, and don't know what this might do.
snibgo's IM pages: im.snibgo.com
snibgo
 
Posts: 3100
Joined: 2010-01-23T23:01:33+00:00

Re: Photoshop like HUE/Saturation changing

Postby GreenKoopa » 2013-07-10T10:52:29+00:00

Snibgo, isn't this similar to GIMP's Hue / Lightness / Saturation control?
User avatar
GreenKoopa
 
Posts: 448
Joined: 2010-11-04T17:24:08+00:00

Re: Photoshop like HUE/Saturation changing

Postby fmw42 » 2013-07-10T11:06:29+00:00

My conversion above was for all channels (using -modulate). I don't know anything about changing HSL values for a given channel. The PS panel allows one to select any channel R,G,B,Y,C,M. But I am not sure what they are doing.

The GIMP coloration tool and my coloration script both convert the image to grayscale and then color appropriately according to H,S,L values. GIMP's H,S,L tool is similar to PS in that it allows one to select a channel.

Perhaps it is a blending of colorizing the one channel and the original image.
User avatar
fmw42
 
Posts: 13868
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: Photoshop like HUE/Saturation changing

Postby snibgo » 2013-07-10T11:20:42+00:00

Yes, it could well be. (For those without Gimp, an explanation is at http://docs.gimp.org/2.8/en/gimp-tool-h ... ation.html .)

Replicating this in IM might be:

(1) Modulate the source according to the desired H, L and S parameters.

(2) For each pixel in the source, determine how far the pixel is from Red, Yellow or whatever colour has been selected. This distance is modified according to the "Overlap" option.

(3) Blend the source with the output from (1) in proportion given by the result of (2).
snibgo's IM pages: im.snibgo.com
snibgo
 
Posts: 3100
Joined: 2010-01-23T23:01:33+00:00

Re: Photoshop like HUE/Saturation changing

Postby Delphir » 2013-07-10T12:27:25+00:00

so there is no a regular command-style solution for that?
Delphir
 
Posts: 2
Joined: 2013-07-10T04:00:18+00:00

Re: Photoshop like HUE/Saturation changing

Postby snibgo » 2013-07-10T12:30:09+00:00

There is no one-word IM command for the effect. It can be done, but needs figuring out how.

Using as source image:
Code: Select all
%IM%convert -size 500x500 gradient:red-blue rb.png

Gimp, Colors, Hue-Saturation.

Selecting "Master", the HLS sliders have the expected effect. The Overlap slider does nothing.

Selecting colours Yellow, Green or Cyan: no sliders do anything.

Selecting colours Red, Magenta or Blue: the appropriate third of my source image is effected. When Overlap==0, there is a sharp transition between the effect and no-effect; the transition is at the selected colour plus or minus 20 degrees. When Overlap==100, the transition is gradual.

So my step (2) above isn't what Gimp does. A closer version is:

2a. Suppose the selected colour, as an angle in degrees, is S. Suppose the image pixel has colour P. The absolute difference D = abs(P-S).

2b. When Overlap==0, transition T = D < 20 ? 1 : 0
When Overlap==100, transition T = 1 - (D/60), clipped to values zero to one.

So the transition function seems to have a slope discontinuity at D==20.
snibgo's IM pages: im.snibgo.com
snibgo
 
Posts: 3100
Joined: 2010-01-23T23:01:33+00:00

Re: Photoshop like HUE/Saturation changing

Postby fmw42 » 2013-07-10T13:43:07+00:00

Looks to me like it might do the following:

1) takes a color (R,G,B,M,Y,C) and modulates that color
2) creates a mask (possibly from some threshold on the difference between the image colors at a pixel and this modulated color). Something like what snibgo suggested. The overlap my a be some kind of blurring or ramping of the mask
3) uses the mask to -compose over (or possibly blend) -composite the modified color with the original image.

Next color selected works on the result of the previous changed image.
User avatar
fmw42
 
Posts: 13868
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: Photoshop like HUE/Saturation changing

Postby snibgo » 2013-07-10T14:58:20+00:00

If we take the central transition to be at D==30 degrees, so there is no slope discontinuity in the transition and we can do the job with one "-level" instead of two, this Windows script gives results very close to those from Gimp:
Code: Select all
set SRC=rb.png

set SELCOL=Red
set OVERLAY=100

set modH=130
set modS=100
set modL=75

FOR /F %%L IN ('%IM%identify -format "L1=%%[fx:100/12-%OVERLAY%/12]\nL2=%%[fx:100/12+%OVERLAY%/12]" xc:') DO set %%L

%IM%convert ^
  %SRC% ^
  ( -clone 0 -modulate %modL%,%modS%,%modH% ) ^
  ( -clone 0 ^
    ( -clone 0 ^
      -fill %SELCOL% -colorize 100 ^
    ) ^
    -colorspace HSL ^
    -compose Difference -composite ^
    -separate -delete -2--1 ^
    -solarize 50%% ^
    -level %L1%,%L2%%% ^
    -negate ^
  ) ^
  -compose Over -composite ^
  out.png
snibgo's IM pages: im.snibgo.com
snibgo
 
Posts: 3100
Joined: 2010-01-23T23:01:33+00:00

Re: Photoshop like HUE/Saturation changing

Postby snibgo » 2013-07-10T18:54:28+00:00

I'm not keen on the way Gimp does this job, so I probably won't refine the emulation further.

But I like the concept so here is my variation, more in line with what I initially thought Gimp did. I find the distance in any colorspace between a pixel and the chosen colour. (Gimp apparently compares only the hue; I compare all three channels.) I then transition the effect down from maximum at pixels that match exactly, to zero effect at a distance MAXDIST from the exact match, where typically 0 <= MAXDIST <= 100, but higher values gives results closer to conventional "-modulate". The effect never goes negative. Distance measurement is in the specified colourspace.

Windows 7 script, tested under IM v6.8.6-0.
Code: Select all
rem Modulate by colour.
rem Effect is maximum for pixels of given colour SELCOL,
rem declining to zero at colour distance MAXDIST from SELCOL, in colorspace COLSPACE.


setlocal

set EXT=%2
if "%EXT%"=="" set EXT=tiff

set modL=%3
if "%modL%"=="" set modL=100

set modS=%4
if "%modS%"=="" set modS=100

set modH=%5
if "%modH%"=="" set modH=100

set MAXDIST=%6
if "%MAXDIST%"=="" set MAXDIST=100

set COLSPACE=%7
if "%COLSPACE%"=="" set COLSPACE=sRGB

rem External parameter:
if "%SELCOL%"=="" set SELCOL=rgb(50%%,50%%,50%%)

if /I "%COLSPACE%"=="sRGB" (
  set INCOLSP=
  set OUTCOLSP=
) else (
  set INCOLSP=-colorspace %COLSPACE%
  set OUTCOLSP=-colorspace sRGB
)


%IM%convert ^
  %1.%EXT% ^
  ( -clone 0 -modulate %modL%,%modS%,%modH% ) ^
  ( -clone 0 ^
    ( -clone 0 ^
      -fill %SELCOL% -colorize 100 ^
    ) ^
    %INCOLSP% ^
    -compose Difference -composite ^
    -grayscale RMS ^
    -level 0,%MAXDIST%%% ^
    -negate ^
    %OUTCOLSP% ^
    -write mask_mbc.png ^
  ) ^
  -compose Over -composite ^
  %1_mbc.%EXT%
snibgo's IM pages: im.snibgo.com
snibgo
 
Posts: 3100
Joined: 2010-01-23T23:01:33+00:00

Re: Photoshop like HUE/Saturation changing

Postby fmw42 » 2013-07-10T19:47:54+00:00

snibgo,

I don't read your windows scripts very well, but from what I can tell you are modulating the image. When I look at what is going on in the GIMP panel when one color is selected, I see that the controls change the color patch. This would suggest to me that the modulation perhaps should be on the selected color and that compared with the image for the distance test and mask. But this is only a guess on my part and you may be on the more correct path. I did notice also that in GIMP that it appeared that they only compared hue.

I have not tried to experiment with any bash scripting. I did not like that effect too well (especially at the extremes) and did not think it was worth the effort to try to figure it out.

Fred
User avatar
fmw42
 
Posts: 13868
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: Photoshop like HUE/Saturation changing

Postby snibgo » 2013-07-10T20:44:46+00:00

The Windows-specific parts of my scripts do nothing interesting, and can easily be replicated in another language.

The Gimp (v2.8 ) patches show what would happen to any pure red, magenta etc pixels with the current settings.

There are many ways to skin a cat. My method modulates the image then composes that over the original, with a mask. The complexity is in the mask. Both scripts use gray masks. The first uses the difference in hues, and the level uses the "overlap" parameter (which my script mis-spells as "overlay") to restrict the transition. The second script uses the diagonal distance in the 3-D colour cube between the selected colour and each pixel. It also uses level to restrict the transition, but in a different way.

I tried to see if there was a way to modulate an image of a constant colour and use that to modify the image, along the lines of your previous post. I couldn't see a way of doing that, but it might exist, of course, and be better than my method.

I should mention that Gimp can apply seven transformations, whereas my scripts can do only one -- but could be run multiple times.

OOPS: in my second script, I left in a debugging line: "-write mask_mbc.png ^". This can be removed.
snibgo's IM pages: im.snibgo.com
snibgo
 
Posts: 3100
Joined: 2010-01-23T23:01:33+00:00

Re: Photoshop like HUE/Saturation changing

Postby GreenKoopa » 2013-07-10T22:55:00+00:00

Delphir wrote:so there is no a regular command-style solution for that?

I don't know of one. The simple solution is to use a hald color look-up. This would let you apply your color modification to one or many images. The down side is that a color look-up file must be created by hand using PS for each set of parameters you use. Alternatively, snibgo's clever script could be simplified if your needs are narrower that supporting the entire PS dialog.
User avatar
GreenKoopa
 
Posts: 448
Joined: 2010-11-04T17:24:08+00:00


Return to Users

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Google Adsense [Bot] and 15 guests