imagemagickluminance

adjust image luminance imagemagick


I am trying to adjust the mean luminance of an image using imagemagick. I have converted the size and color of the images so they are now in grayscale as below:

body_heavy_female_gray_resize

next I need to adjust the luminance of each image so they match (for research). The target luminance mean is 189.

I used this code to get the luminance value:

$ convert image -colorspace LAB -channel r -separate +channel -format "%[mean]\n" info:  

Which gives the value out of 65535 (from this post)

I used the equation x/65535 = 189/255 to understand what my target is for high quality images: 48,573.

The above image is currently 29319.5

Is there a way to adjust this value and set it to 48573 within command line?

I tried:

convert image -colorspace LAB -channel r -evaluate set 48573

AND

convert image -colorspace LAB -channel r -evaluate set "48573"

AND

I tried changing the final number to 189, 89, and .89 (in case I was in the wrong dimensions) each time the error was in the number listed.

> convert:  `.89' @ error/convert.c/ConvertImageCommand/3272

I've continued working on this problem and adjusted based on comments left below so now I am here:

target image:

I ran the following script:

target_percent_luminance=74.12
hundred=100
echo "working on ${target_pic}"
gray_mean_val=$(magick identify -verbose      ${target_pic} | grep mean | awk '{print $2}' | sed -n '1p')
percent_gray_mean_val=$(echo     $hundred\*$gray_mean_val/255 | bc)
echo $percent_gray_mean_val 
difference=$(echo 74.12-$percent_gray_mean_val | bc)
echo $difference
magick convert ${target_pic} -modulate ${difference}% ${target_pic}_luminance.jpg

Each line worked-- output:

casey$ target_percent_luminance=74.12
casey$ hundred=100
casey$ echo "working on ${target_pic}"
working on F201_background_gray_resized.jpg
casey$ gray_mean_val=$(magick identify -verbose  ${target_pic} | grep mean | awk '{print $2}' | sed -n '1p')
casey$ percent_gray_mean_val=$(echo $hundred\*$gray_mean_val/255 | bc)
casey$ echo $percent_gray_mean_val 
40
casey$ difference=$(echo 74.12-$percent_gray_mean_val | bc)
casey$ echo $difference 
34.12
casey$ magick convert ${target_pic} -modulate ${difference}% ${target_pic}_luminance.jpg

But here is the output image, which seems much too dark. Can anyone see the error?

Using GeeMac's answer below I wrote:

casey$ input=F201_background_gray_resized.jpg
casey$ magick $input -brightness-contrast "%[fx:${lumin}-(mean*100)]" ${input}_lumintwo.jpg

and got this image which looks better!


Solution

  • If you're using IM7 you can do a lot of calculations directly within the "magick ..." command. This command, for example, reads an input image, and adjusts the brightness so the output image has a mean of 74.12%...

    lumin=74.12
    
    magick input.jpg -brightness-contrast "%[fx:${lumin}-(mean*100)]" output.jpg
    

    I don't know how that compares to making adjustments with "-modulate N", but when I check the output with this...

    magick output.jpg -format "%[fx:mean*100]\n" info:
    

    ... the result is "74.1219", or whatever the value of ${lumin} is. It might give you another approach to consider.