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)