Extracting HSL shift from base image to new

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
patdavid
Posts: 8
Joined: 2013-09-04T08:05:59-07:00
Authentication code: 6789

Extracting HSL shift from base image to new

Post by patdavid »

Hi all,

I'll get right to it...

I've got a base image that I apply some color modifications to (namely, HSL shifts, and then RGB curve shifts).

I'd like to find a way to extract the shift values between the base/identity image, and the color modified version.

For instance, I've got a HALD CLUT identity image. I also have a the same image, but with HSL modifications applied. Is there any way to retrieve the numerical shifts easily for a given base value?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extracting HSL shift from base image to new

Post by fmw42 »

Not that I know by any kind of comparison of the HALD images. You would have had to record them in the filename or comments of the new Hald image.
patdavid
Posts: 8
Joined: 2013-09-04T08:05:59-07:00
Authentication code: 6789

Re: Extracting HSL shift from base image to new

Post by patdavid »

So, if I had an image that I know to be some pure red color ( rgb(255,0,0) ), and a second image identical in all ways, except there has been an HSL shift of some sort, there's not a way for me to determine what that shift was? (Sorry if I'm dense, just trying to understand a path forward).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting HSL shift from base image to new

Post by snibgo »

I've tried to solve a similar question: given corresponding colour patches in two images, what transformation from one makes the other?

Sorry, I can't find my code. From memory, I needed to iterate to get the "-modulate" parameters, even when the only initial transformation was "-modulate". But it did give a complete solution, possibly including curves.
snibgo's IM pages: im.snibgo.com
patdavid
Posts: 8
Joined: 2013-09-04T08:05:59-07:00
Authentication code: 6789

Re: Extracting HSL shift from base image to new

Post by patdavid »

snibgo wrote:I've tried to solve a similar question: given corresponding colour patches in two images, what transformation from one makes the other?

Sorry, I can't find my code. From memory, I needed to iterate to get the "-modulate" parameters, even when the only initial transformation was "-modulate". But it did give a complete solution, possibly including curves.
Now we're talking! :D

modulate is where I should be looking then?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting HSL shift from base image to new

Post by snibgo »

Yes, it is one way, and I found it successful. (I wish I could find my code!!)

From very vague memory:

1. Find the HSL difference between images A and B. This gives approximate parameters for "-modulate", to transform A to B.

2. "convert A -modulate {parameters} A2"

3. Find the HSL difference between images A2 and B. Use this to tweak the parameters.

4. Repeat from 2 until the difference is almost zero. It only takes 2 or 3 iterations.
snibgo's IM pages: im.snibgo.com
patdavid
Posts: 8
Joined: 2013-09-04T08:05:59-07:00
Authentication code: 6789

Re: Extracting HSL shift from base image to new

Post by patdavid »

snibgo wrote:Yes, it is one way, and I found it successful. (I wish I could find my code!!)

From very vague memory:

1. Find the HSL difference between images A and B. This gives approximate parameters for "-modulate", to transform A to B.
This is the black magic part for me. Is there a method for accomplishing this? (Reading through the docs doesn't immediately bring anything to mind...)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting HSL shift from base image to new

Post by snibgo »

Yes, it's quite simple, but I don't have the time to work out the details.

The basic idea is to convert the two colours to HSL. Dump the pixel values as text. Compare the values. The lightness will have increased (or decreased) by a certain percentage. The is the first parameter for the "-modulate" operation.

Likewise, saturation.

Hue is a bit more awkward. We need to subtract the txt hue values and fiddle around. See http://www.imagemagick.org/script/comma ... p#modulate
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting HSL shift from base image to new

Post by snibgo »

Here we go. With luck, I won't lose this one. A Windows script (that I call whatMod.bat).

It takes two images as parameters. These should have one pixel each. (If they have more, it will operate on the last pixel of each).

It returns the approximate "-modulate" parameters that will convert the first image to the second.

I say "approximate", but it seems to be exact for 16-bit images.

For non-Windows users, I'll explain the script:

1. Convert first image to HSL, setting HA, SA and LA to the channels (expressed as percentages).
2. Convert second image to HSL, setting HB, SB and LB to the channels (expressed as percentages).
3. dL = 100 * LB / LA
4. dS = 100 * SB / SA
5. dH = 100 + 2 * (HB - HA)

Code: Select all

rem  Given %1 and %2, images with one pixel each,
rem  returns dL, dS, dH,
rem  the approximate "-modulate" parameters to convert %1 to %2.

for /F "usebackq tokens=7-9 delims=:,hsl()%% " %%L in (`%I%convert ^
  %1 ^
  -colorspace HSL ^
  txt:`) DO (
  set HA=%%L
  set SA=%%M
  set LA=%%N
)

for /F "usebackq tokens=7-9 delims=:,hsl()%% " %%L in (`%I%convert ^
  %2 ^
  -colorspace HSL ^
  txt:`) DO (
  set HB=%%L
  set SB=%%M
  set LB=%%N
)

for /F "usebackq" %%L in (`%I%identify -ping -format "%%[fx:100*%LB%/%LA%]" xc:`) DO set dL=%%L

for /F "usebackq" %%L in (`%I%identify -ping -format "%%[fx:100*%SB%/%SA%]" xc:`) DO set dS=%%L

for /F "usebackq" %%L in (`%I%identify -ping -format "%%[fx:100+2*(%HB%-%HA%)]" xc:`) DO set dH=%%L

echo %dL% %dS% %dH%
The source images might be single pixels cropped from larger images, of course.
snibgo's IM pages: im.snibgo.com
patdavid
Posts: 8
Joined: 2013-09-04T08:05:59-07:00
Authentication code: 6789

Re: Extracting HSL shift from base image to new

Post by patdavid »

snibgo wrote:Here we go. With luck, I won't lose this one. A Windows script (that I call whatMod.bat).
I wanted to drop in and thank you very much for taking the time to do this!

The script crashes my IM for some reason, but I've gotten exactly what I need to do it manually out of your script, so thank you.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting HSL shift from base image to new

Post by snibgo »

Good stuff. Note that it gives a bad result if the first image is gray (ie LA==0 or SA==0). You should check for this.
snibgo's IM pages: im.snibgo.com
Post Reply