3

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). This image's gAMA value is 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.

This image is what I want

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 image's gAMA value is 0.4545(1/2.2)

fmw42
  • 46,825
  • 10
  • 62
  • 80
ueneid
  • 63
  • 6
  • 3
    If your image has a gamma of 4.4, you could remove that with `-gamma 0.227272` (because that is 1/4.4) and then apply a new gamma of 2.2 with `-gamma 2.2`. – Mark Setchell Mar 23 '18 at 12:29
  • Hmm, I tried your suggestion like below. `$ convert orig.png -gamma 0.227272 orig.png && convert orig.png -gamma 2.2 orig.png` But the processed image is not what I want. I wrote an additional explanation, plz take a look :) – ueneid Mar 26 '18 at 02:10
  • Try -set gamma rather than -gamma – fmw42 Jan 16 '23 at 00:33

3 Answers3

2

This seems to work for me in Imagemagick.

convert red44.png -gamma 0.5 -set gamma 0.4545 red22_new.png

enter image description here

identify -verbose red22_new.png

... Gamma: 0.4545 ...

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Ah, indeed... That's almost same as the command I wrote first. Probably the real problem I have doesn't seem gamma is the root cause Anyway, Thanks a lot! – ueneid Mar 27 '18 at 02:17
1

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)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

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

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks for your comment! I tried `$ convert image.png -gamma 2 image.png` but it doesn't work. I wrote an additional explanation, plz take a look :) – ueneid Mar 26 '18 at 02:13
  • Did you try `convert image.png -set gamma 0.4545 image.png` – fmw42 Mar 26 '18 at 03:23
  • Yes, I did. But the command just set gamma value. I never get the second image's appearance. – ueneid Mar 26 '18 at 03:47