Search code examples
image-processingautomationimagemagickimage-manipulationimagemagick-convert

Imagemagick: How to improve the brightness/contrast of an image with a narrow histogram?


I have a dark image which histogram lies in a tight group.

A source image

The image histogram

In image editor I can convert image int HSL colorspace, separate L channels and make histogram adjustment.

Histogram adjustment dialog

Is this the right way? Which next steps are necessary to do?

Is there a universal solution how to work with such histogram and improve image brightness/contrast?

How can I do the same thing with ImageMagick?


Solution

  • One simple method in Imagemagick is simply -auto-level.

    Input:

    enter image description here

    convert img.png -auto-level result.png
    

    enter image description here

    If you want to replicate what you did, then in Unix syntax, convert your level parameters to percents: low=100*87/255=34.1% and high=100*141/255=55.3% and gamma=1.83

    convert img.png -colorspace LAB -separate -write mpr:LAB \
    \( mpr:LAB[0] -level 34.1%,55.3%,1.83 \) \
    -swap 0,3 +delete \
    -set colorspace LAB -combine -colorspace sRGB \
    result.png
    

    enter image description here

    For Windows, remove the \ from the parentheses and change the end of line \ to ^. Double the % if in .bat file.