0

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!

  • Take a look at Anthony's [Color Modification](https://www.imagemagick.org/Usage/color_mods/) article. The section "Histogram Redistribution Methodology" might be what your attempting. – emcconville Oct 22 '18 at 19:39
  • If you already have a grayscale image, then why are you converting to LAB and extracting the luminance? Post your original color image so we can test with that. I typically convert to percent and use percent in -evaluate set. Your 189 is equal to 100*189/255=74.12%. There is no output image specified in your command to change the image, nor -format %[mean] info:. Please always show the full command you used. Also always provide your ImageMagick version and platform, since syntax may differ. – fmw42 Oct 22 '18 at 22:02
  • I was converting to LAB just to extract the luminance as I was under the impression that it was the format it needed to be in LAB format to extract a luminance value. That was the full command I used as I am new to ImageMagick and I was trying to figure it out. I am on version Q16. I will try the percent conversion that you mentioned @fmw42 thank you – Casey Jayne Oct 23 '18 at 21:03
  • I am still confused. Are you starting from a color image or from the grayscale image you posted? If the latter, that is wrong for changing it to LAB. You should start with the color image for your command to work. My suggestion to use percent is only that. You can do the -evaluate set in raw 16-bit values as well. – fmw42 Oct 23 '18 at 22:21
  • Hi @fmw42 I am starting from the grayscale as that is the one I need to adjust the luminance within. I don't believe that changing the luminance in color and then changing the background and grayscale would make the images so that the luminance remained at the same level (it's for continuity between stimuli for neuroimaging). I modified the script I've been working on based on your suggestions, did you see my "edit" to the original post? – Casey Jayne Oct 23 '18 at 22:26
  • I do not think you can change a grayscale image to LAB, since you have already change it to rec709luminance (Y from YIQ or YUV, etc), so that the colors are already blended in that way. Proper way to convert to (grayscale) LAB Luminance is by: `convert colorimage.suffix -colorspace LAB -separate r +channel output_luminance.suffix` – fmw42 Oct 23 '18 at 22:31

1 Answers1

2

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.

GeeMack
  • 4,486
  • 1
  • 7
  • 10
  • thank you. Unfortunately I've been having issues with ImageMagick all day so I haven't been able to properly try this. I will update the answers with what worked asap – Casey Jayne Oct 24 '18 at 22:17
  • This worked! I will update my question to show what happened when I tried it! Thank you for your help @GeeMack – Casey Jayne Oct 25 '18 at 15:23