Lighten dark areas only?

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
Elaphe
Posts: 5
Joined: 2019-09-12T12:32:19-07:00
Authentication code: 1152

Lighten dark areas only?

Post by Elaphe » 2019-09-13T01:10:26-07:00

Is there any command or a series of commands that lighten the dark areas of a photo (usually happens after level correction) thus showing the lost details, and not affecting the rest of the image? I mean something like what Photoshop does with the shadows/highlights function. Please don't tell me to resort to scripts for this, because I don't use Linux and I don't want to install cgywin, apart from the fact that I have no idea how to use scripts.

snibgo
Posts: 12032
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lighten dark areas only?

Post by snibgo » 2019-09-13T05:09:05-07:00

What version of IM, on what platform? I'll assume v7 on Windows.

There are many ways of lightening shadows, depending on exactly what you want. For example:

Code: Select all

magick in.tiff +level 10,100% out.tiff
This raises black to 10%, and raises lighter values less.

Or "-gamma 0.8" will raise all values except black ad white.

Any effect can be masked so that, for example, it only applies to tones in the range 0 to 30%, with maximum effect at 15%.

I suggest you show sample images, eg an input and a desired output from that input.
snibgo's IM pages: im.snibgo.com

Elaphe
Posts: 5
Joined: 2019-09-12T12:32:19-07:00
Authentication code: 1152

Re: Lighten dark areas only?

Post by Elaphe » 2019-09-13T12:27:16-07:00

OK, here an example (Samara from The Ring).

im1.jpg is the original file, which has some dark areas with details not visible such as the hair.
im2.jpg is lightening the shadows with Photoshop (shadows and highlights function) and not affecting the rest of the image (what I want).
im3.jpg is what happens if you just adjust midtones/gamma (you lighten dark areas but the bright areas too, and I don't want that).

im1.jpg
Image

im2.jpg
Image

im3.jpg
Image

User avatar
fmw42
Posts: 25425
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lighten dark areas only?

Post by fmw42 » 2019-09-13T13:47:02-07:00

ImageMagick does not have a direct equivalent command to Photoshop shadows and highlights. So what user snibgo gave was one approach from what you asked originally. Did you try that? It seems to bring out the details in the hair as you asked.

Also you said TIFF files, but the file you provided is JPG and will have lost information due to lossy compression.

Another option is using sigmoidal-contrast primarily in the dark tones:

Code: Select all

convert im1.jpg +sigmoidal-contrast 2.5x100% result.png

snibgo
Posts: 12032
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lighten dark areas only?

Post by snibgo » 2019-09-13T14:42:13-07:00

The input JPEG has virtually no detail in the dark hair.

We read the input file, samara.jpg. The first parentheses is the main transformation, "-evaluate Pow 0.6", which leaves black and white unchanged, and brightens mid-tones.

The next parentheses define a mask. Where do we want the effect? We want the full effect to apply where the grayscale input is 0 to 5%, and fade down to 10%. Windows BAT syntax:

Code: Select all

magick ^
  samara.jpg ^
  ( -clone 0 ^
    -evaluate Pow 0.6 ^
  ) ^
  ( -clone 0 ^
    -colorspace Gray ^
    -range-threshold 0,0,5,10%% ^
  ) ^
  -alpha off ^
  -compose Over -composite ^
  samara_out.png
Image
snibgo's IM pages: im.snibgo.com

Elaphe
Posts: 5
Joined: 2019-09-12T12:32:19-07:00
Authentication code: 1152

Re: Lighten dark areas only?

Post by Elaphe » 2019-09-14T00:33:17-07:00

OK, the provided image maybe is not the best, but it's just an example.

First I tried the level command and the result is that all dark areas go grey.

Then I tried the gamma command and it's better but again, the light areas are affected too.

I tried your set of commands. I wrote a .bat file and executed it. I get a series of errors:


D:\ImageMagick>magick samara.jpg ( -clone 0 -evaluate Pow 0.6 ) ( -c
lone 0 -colorspace Gray -range-threshold 0,0,5,10% ) -alpha off -c
ompose Over -composite samara_out.png

magick: no decode delegate for this image format `JPEG' @ error/constitute.c/Rea
dImage/505.
magick: NoImageForProperty "%w" @ warning/property.c/GetMagickPropertyLetter/248
6.
magick: unknown image property "%w" @ warning/property.c/InterpretImagePropertie
s/3399.
magick: NoImageForProperty "%h" @ warning/property.c/GetMagickPropertyLetter/237
8.
magick: unknown image property "%h" @ warning/property.c/InterpretImagePropertie
s/3399.
magick: NoImageForProperty "%m" @ warning/property.c/GetMagickPropertyLetter/240
9.
magick: unknown image property "%m" @ warning/property.c/InterpretImagePropertie
s/3399.

snibgo
Posts: 12032
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lighten dark areas only?

Post by snibgo » 2019-09-14T05:32:55-07:00

I don't know what the problem is.

You say you tried the level and gamma methods, and IM worked. Did you use the same filename? The same "magick" command?

I suggest you show the complete BAT file, with all three commands.

What version of IM do you use?
snibgo's IM pages: im.snibgo.com

Elaphe
Posts: 5
Joined: 2019-09-12T12:32:19-07:00
Authentication code: 1152

Re: Lighten dark areas only?

Post by Elaphe » 2019-09-14T06:23:41-07:00

Sorry, I was still using version 6. I've updated to 7 and now it's working fine. Thanx.

Elaphe
Posts: 5
Joined: 2019-09-12T12:32:19-07:00
Authentication code: 1152

Re: Lighten dark areas only?

Post by Elaphe » 2019-09-14T07:02:16-07:00

I've been trying different values of the code you provided, but still Photoshop gets more constrast adding more light only to the details on the dark areas.

User avatar
fmw42
Posts: 25425
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lighten dark areas only?

Post by fmw42 » 2019-09-14T08:34:15-07:00

Create a HALD image (in PNG output). Take it to your Photoshop and apply the same shadow highlight processing to it. Save as PNG. Bring it back to ImageMagick and apply that to your image using -hald-clut. See https://imagemagick.org/Usage/color_mods/#hald-clut

snibgo
Posts: 12032
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lighten dark areas only?

Post by snibgo » 2019-09-14T11:42:31-07:00

As Fred says, you can apply Photoshop operations to a clut or hald-clut, then use that in IM.

In IM alone, you can get as much contrast as you want. For example, change "-evaluate Pow 0.6" to "-evaluate Multiply 20". Then what was 5% becomes 100%.
Image
I think that looks horrible, but it shows what can be done.

Incidentally, if we use a grayscale gradient instead of samara.jpg as the input, we can graph the result to see the transfer curve:
Image
From the graph (x-axis is input, y-axis is output), we see massive contrast in the bottom 5% of input. From 10% upwards, the input is unchanged (ie output=input). But we have to pay a price: a massive negative contrast between 5% and 10%.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25425
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lighten dark areas only?

Post by fmw42 » 2019-09-14T15:40:14-07:00

If you are able to work in either Windows 10 Unix or Windows w/Cygwin, I have a bash unix shell script for which I tried to emulate Photoshop function of the same name. The results are similar but I do not claim it works perfectly to replicate Photoshop.

Here I just adjust the shadow amount.

Input:
Image

Code: Select all

shadowhighlight -sa 100 -ha 0 -ma 0 -bc 0 -wc 0 im1.jpg im1_shadowhightlight_sa100.jpg
Image


The idea is to apply a half sigmoidal-contrast (as I mentioned in an earlier post) in LAB to the L channel, but mask it with a blurred version of the L channel. Here is part of my script code for shadow processing only. Unix syntax.

Code: Select all

samt=50
srad=30
swid=50
srad=`convert xc: -format "%[fx:$srad/3]" info:`
samt=`convert xc: -format "%[fx:$samt/20]" info:`
proc="+sigmoidal-contrast $samt,100%"	
convert im1.jpg -write mpr:img -colorspace LAB -channel R +sigmoidal-contrast $samt,100% +channel -colorspace sRGB \
\( mpr:img -colorspace LAB -channel R -separate +channel -blur 0x${srad} -black-threshold $swid% -level ${swid}x100% -negate \) \
-alpha off -compose copy_opacity -composite im1_sa100_result.jpg
Image

Adjust the samt value as desired.

Post Reply