How can I fix PNG's gamma using ImageMagick? - imagemagick

I'd like to fix a PNG's gamma value to 1/2.2. For example, let's say there is a png image whose gAMA value is 1/4.4. For some reasons, I have to fix the value to 1/2.2. So I try convert command like below.
# 0.5 is led by (1/4.4)/(1/2.2)
# 0.45455 is led by 1/2.2
$ convert orig.png -gamma 0.5 +gamma 0.45455 new.png
Then, I compare two images, orig.png and new.png, but they appear to be different.
I guess the formula, (1/4.4)/(1/2.2), is wrong. Does anyone know the correct way to do it?
Thanks.
EDITED:
Sorry, but maybe I could not tell you what I really want to do. So, I added more information and sample images.
First of all, I have the below image. This image's gAMA is about 0.227(1/4.4).
And then, I need this image. The image's gAMA is 0.4545(1/2.2) but the appearance is same as the above one.
So, I don't need this. This image's gAMA is also 0.4545(1/2.2), but the appearance is different from the first one.

This seems to work for me in Imagemagick.
convert red44.png -gamma 0.5 -set gamma 0.4545 red22_new.png
identify -verbose red22_new.png
...
Gamma: 0.4545
...

I have been experimenting some more and I am not sure this answer is correct, but I'll leave it a while so that Fred and Eric and Glenn (#GlennRandersPehrson) can see it and maybe correct it or comment on it. I also don't understand why this:
convert start.png -set png:gAMA 2 -verbose info: | grep -i -C5 gam
differs from this:
convert start.png -set png:gAMA 2 tmp.png
identify -verbose tmp.png | grep -i -C5 gam
I think I see what is happening - in a nutshell, I don't think the PNG encoder is picking up the gamma correctly.
Let's make a single pixel image with value 100 so we can see what happens to it when transformed:
convert xc:"gray(100)" start.png
Let's inspect it:
convert start.png txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (25700) #646464 gray(100)
Yes, it's definitely 100. Let's check the gamma:
identify -verbose start.png | grep -i gam
Gamma: 0.45455
png:gAMA: gamma=0.45455 (See Gamma, above)
Now let's change the gamma parameter, using either my way, or Fred's (#fmw42) way:
# Change gamma parameter my way
convert start.png +gamma 2 -verbose info: | grep -i gam
Gamma: 2
png:gAMA: gamma=0.45455 (See Gamma, above)
convert start.png +gamma 2 txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (25700) #646464 gray(100)
# Change gamma parameter Fred's way
convert start.png -set gamma 2 -verbose info: | grep -i gam
Gamma: 2
png:gAMA: gamma=0.45455 (See Gamma, above)
convert start.png -set gamma 2 txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (25700) #646464 gray(100)
As you can see, neither way has changed the pixel values themselves, both ways have changed the ImageMagick internal gamma, but crucially neither has done what you need, which is to change the PNG encoder's gamma. A little experimentation shows you need:
convert start.png -set png:gAMA 2 -verbose info: | grep -i gam
Gamma: 0.45455
png:gAMA: 2
So, I think that is the answer - namely use:
convert input.png -set png:gAMA XYZ result.png
Just for future readers, if you want to change the pixel values themselves rather than just the gamma parameter, do this:
# Set gamma of 2, initial pixel value 100 becomes 160
convert start.png -gamma 2 txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (41039.6) #A0A0A0 gray(160)
# Set gamma of 0.5, initial pixel value 100 becomes 39
convert start.png -gamma 0.5 txt:
# ImageMagick pixel enumeration: 1,1,65535,gray
0,0: (10078.4) #272727 gray(39)

In Imagemagick, just use -set gamma:
convert image.png -set gamma 0.4545 image.png
That will simply change the gamma value, but not change the pixel values. If you need to do that, then you should use (as Mark Setchell suggested). But if you want 1/2.2 and have 1/4.4, then you need a value of (1/2.2)/(1/4.4)=2
convert image.png -gamma 2 image.png

Related

Magick equivalent of GIMP Hue-Chroma transformation

Using Gimp, given an input image, I can improve its contrast using Colors > Hue Chroma... by setting Chroma=50 (in a scale between -100 and 100) and leaving Hue=0 and Lightness=0. So it appears I'm doing an HCL transformation.
Is there an equivalent command for Magick?
The following image shows the GIMP effect:
Image
Updated Answer
Not sure about this at all. I think you can get pretty close with -modulate if you go into an LCH colourspace, but I have no idea if it will work consistently. I got:
magick cXDv3.jpg -define modulate:colorspace=LCH -modulate 100,150 result.jpg
If that doesn't work, or is not to your liking, read on...
Generic Method for any GIMP filters
The method below should allow you to replicate any GIMP filter with ImageMagick - as long as it is a pure "point process", I mean one where each pixel's output value is purely derived from its input value and not an "area process" where surrounding pixels contribute - such as blurring or median filtering, for example.
It's called a HALD-CLUT. You would create a HALD-CLUT something like this:
magick hald:16 clut.png
Then take that file (clut.png) into GIMP and apply your GIMP processing on it and save the result as GIMP-H0-C50-L0.png so we know how GIMP affects each colour. You do that just once.
Then you go back to ImageMagick and apply that CLUT to your image:
magick input.png GIMP-H0-C50-L0.png -hald-clut result.png
That gives me this:
and I think you'll agree the left side looks pretty similar to the right side of your input image.
Original Answer
I don't know what that command does in GIMP, but you can convert to HCL colourspace in ImageMagick and select the Chroma channel for modification like this:
magick INPUT.PNG -colorspace HCL -channel G ...
You then want to do something ? to affect the Chroma channel, so try -auto-level for now, and then return to sRGB colourspace and save:
magick INPUT.PNG -colorspace HCL -channel G -auto-level +channel -colorspace sRGB RESULT.PNG
Then you need to provide more clues or experiment more with what that command does in GIMP - or provide examples.

Imagemagick - how to get largest layer and compress?

Please help. Is there an easy way to take the largest layer of a tiff and zip compress it back as a single layer tiff again with imagemagick or similar?
Just a slightly easier version of Fred's answer. You can generate a list of the area (in pixels) of each layer in a TIF followed by the layer/scene number like this:
magick identify -format "%[fx:w*h] %s\n" image.tif
Sample Output
240000 0
560000 1
200000 2
So, if we do that again, sort it reverse numerically and take the second field of the first result, we will get the number of the layer with the largest area:
layer=$(magick identify -format "%[fx:w*h] %s\n" image.tif | sort -rn | awk 'NR==1{print $2}')
So, the complete solution would look like:
#!/bin/bash
# Get layer number of layer with largest area
layer=$(magick identify -format "%[fx:w*h] %s\n" image.tif | sort -rn | awk 'NR==1{print $2}')
# Extract that layer and recompress as single layer
magick image.tif[$layer] -compress lzw result.tif
If you are using ImageMagick v6 or older:
magick identify ... becomes identify ...
magick image.tif ... becomes convert image.tif ...
In concept, using ImageMagick this can be done in a single command. Here's an example...
magick input.tif -background none -virtual-pixel none ^
( -clone 0--1 +repage -layers merge ) ^
-distort affine "0,0 0,%[fx:s.w==u[-1].w&&s.h==u[-1].h?0:h]" ^
-delete -1 -layers merge output.tif
That starts by reading in the original TIF and setting the background and virtual-pixel settings to "none".
Then inside the parentheses it clones all the layers of the TIF, repages them, and merges them into a single image with the dimensions of the largest layer. That will become a gauge to measure with.
Next it uses "-distort affine" to slide each image out of the viewport and leave it transparent unless the image matches the width and height of that gauge. So after that distort, the largest image will remain unchanged, and all the others will be transparent.
Finish by deleting that gauge image and merging the rest. All the layers are transparent except the largest one, so merging them leaves just that visible one as a single layer.
The command is in Windows syntax using IM7. If you're using ImageMagick v6, use "convert" instead of "magick". To make it work in *nix, change the continued line carets "^" to backslashes "\" and escape the parentheses with backslashes "\(...\)". There may be other issues I've overlooked.
Obviously if there are two or more layers matching the largest dimensions, the output result will only be the first one from the original TIF.
Edited to add: This method will only work if both the greatest width and greatest height are on the same image.
How do you define largest? Width, Height, File size? If the largest dimension from width and height is used, then in Unix, you can do the following on a 3 layer tif file. Get the max dimension of each layer. Then find which layer is the largest. Then use just that layer when reading and writing the file.
Arr=(`identify -format "%[fx:max(w,h)]\n" img.tif`)
echo "${Arr[*]}"
500 1024 770
num=${#Arr[*]}
dim=0
for ((i=0; i<num; i++)); do
if [ ${Arr[$i]} > $dim ]; then
dim=${Arr[$i]}
index=$i
fi
done
echo "$index"
2
convert img.tif[$index] -compress zip newimg.tif
identify newimg.tif
newimg.tif[2] TIFF 770x768 770x768+0+0 8-bit sRGB 3662B 0.000u 0:00.000
I cannot think of any direct and simple method to find the largest layer and extract it in the same command line.

How do I convert EXR to PNG and adjust brightness at the same time

I was able to convert my EXR image to a PNG using the techniques outlined in Image conversion from IFF and EXR formats to JPEG format .
convert 0007.exr /tmp/0007.png
Unfortunately the PNG looks quite dim.
What should I add to the imagemagick convert command line to increase the brightness?
Starting with this:
You could try -auto-gamma:
convert start.jpg -auto-gamma result.jpg
If the -auto-gamma overcooks the image for your liking, you could apply a percentage of it. So, here I clone the original image and apply auto-gamma to the clone but then only blend 80% back into the original because I feel auto-gamma overdoes it:
convert start.jpg \( +clone -auto-gamma \) \
-define compose:args=80 -compose blend -composite result.jpg
Or, another option, you could experiment with your particular images and maybe try using -modulate for the brightness, where 100% means "do nothing", so numbers over 100 increase the brightness:
convert start.jpg -define modulate:colorspace=LCHuv -modulate 160 result.jpg
You can try -auto-level, which will take the minimal value and the maximal value of your picture and then stretches the values to the full range of values:
convert input.exr -auto-level output.jpg
Note that if you picture was too bright and this does not help, then it might be that your image is stored with 32 Bit, while ImageMagick is working with 16 Bit and no HDRI support. 32 Bit input is supported if convert --version
either show Q32 as part of the version string or lists HDRI under features.
Depending on your operating system you might be able to install another variant of ImageMagick. For example, for Debian Buster we can use sudo apt list imagemagick* to see that the package imagemagick-6.q16hdri is available. Installing this package provides convert-im6.q16hdri, which allows reading 32 Bit EXR images.
EXR is in linear RGB colorspace. You want to convert it to non-linear sRGB colorspace in Imagemagick as:
Input:
convert image.exr -set colorspace RGB -colorspace sRGB output.png

Changing exposure of jpeg

Given a jpeg, what is the formula to change the exposure of that jpeg by +/-1 stop or as known as 1 EV? I want to simulate this exposure change. Is there a formula/ method to do so?
I can demonstrate that using ImageMagick, which is included in most Linux distros and available for OSX and Windows from here.
First, at the Terminal command line create an image:
convert -size 512x512 gradient:black-yellow gradient.png
Now, the way to effect +1 stop exposure increase is to composite the image with itself using the Screen blending mode - it is available in Photoshop and ImageMagick and is described here.
So, the formula to composite image A with image B is:
1-stop brighter image = 1-(1-A)(1-B)
but as we are compositing the image with itself, A and B are the same, so we effectively have
1-(1-A)(1-A)
ImageMagick refers to the pixels of an image using p rather than A, so we can do a 1-stop increase like this:
convert gradient.png -colorspace RGB -fx "(1-(1-p)(1-p))" result.png
Note that the Wikipedia article, and ImageMagick's -fx both assume your pixel intensities vary between 0 and 1.0. If you are using 8-bit images, you should calculate with 255 in place of 1, namely
+1 stop brighter image = 255-(255-A)(255-A)
or if using 16-bit values
+1 stop brighter image = 65535-(65535-A)(65535-A)
The above fx-based method is however, very slow because the -fx is interpreted rather than compiled, so a faster way to do it is:
convert gradient.png gradient.png -colorspace RGB -compose screen -composite screen.png
Just for fun, another way of looking at that is that we take the inverse of A, that is 1-A, and square it, and then take the inverse, so it can be done like this:
convert gradient.png -colorspace RGB -negate -evaluate pow 2 -negate result.png
The equivalent of -1 stop exposure decrease is to composite the image with itself using the Multiply blend mode, the formula being
1-stop darker image = A x B
which you would do faster with
convert gradient.png gradient.png -colorspace RGB -compose multiply -composite result.png
or even faster, by using memory-to-memory cloning rather than reading from disk twice, with
convert gradient.png -colorspace RGB +clone -compose multiply -composite result.png
but could do equally with
convert gradient.png -colorspace RGB -evaluate pow 2 result.png

In ImageMagick, is it possible to -auto-gamma based on a region?

I'm using ImageMagick to prepare a set of ~20,000 photos for a timelapse video. A common problem in timelapse videos is flickering, due to changing lighting conditions, passing clouds, hue changes, etc.
I've used IM to convert each image to greyscale and -auto-gamma, which is a drastic improvement in lighting "stability". Very good, but not yet perfect.
I would now like to do the following, but can't figure out how.
1. determine ideal auto gamma based only on the lower 30% of the image
2. apply that ideal gamma to the entire image
Each of my images has sky above and buildings below. The sky changes dramatically as clouds pass by, but the buildings' lighting is fairly stable.
I tried -region, but as expected, it only applies the gamma to the region specified. Is it possible to do what I'm hoping for? Thanks for any advice!
Yes, I think so.
You can crop the bottom 30% of the image like this:
convert image.jpg -gravity south -crop x30%+0+0 bottom.jpg
so that means you can get the mean of the bottom 30% of the image like this:
convert image.jpg -gravity south -crop x30%+0+0 -format "%[mean]" info:
and you can also get the quantum range as well all in one go if you add that in:
convert image.jpg -gravity south -crop x30%+0+0 -format "%[mean] %[fx:quantumrange]" info:
Now, the gamma is defined as the logarithm of the mean divided by the logarithm of the midpoint of the dynamic range, but we can normalize both these numbers to the range [0-1] as follows:
log(mean/quantumrange) / log(0.5)
so we'll let bc work that out for us like this:
echo "scale=4; l($mean30/$qr)/l(0.5)" | bc -l
and we can use the result of that to apply a gamma correction to the entire image. So, I have put all that together in a single script, which I call b30gamma. You save it under that name and then type:
chmod +x b30gamma
to make it executable. Then you can run it on an image like this and the result will be saved as out.jpg so as not to destroy the input image:
./b30gamma input.jpg
Here is the script:
#!/bin/bash
# Pick up image name as parameter
image=$1
# Get mean of bottom 30% of image, and quantum range (65535 for Q16, or 255 for Q8)
read mean30 qr < <(convert "$image" -gravity south -crop x30%+0+0 -format "%[mean] %[fx:quantumrange]" info:)
# Gamma = log(mean)/log(dynamic range centre point)
gcorr=$(echo "scale=4;l($mean30/$qr)/l(0.5)" | bc -l)
# Debug
echo Image: $image, Mean: $mean30, quantum range: $qr, Gamma: $gcorr
# Now apply this gamma to entire image
convert "$image" -gamma $gcorr out.jpg

Resources