Created
October 31, 2016 15:58
-
-
Save yig/ea6945e6187661827a2453b86b5bb3b6 to your computer and use it in GitHub Desktop.
What happens when you create a PNG with and without sRGB or linear RGB colorspace headers?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Make a 5 pixel gradient PNG with no header: 0, 64, 128, 191, 255. | |
## Save the values with PIL (Python Image Library). PIL does not store a header. | |
python make_png.py | |
''' | |
from numpy import * | |
from PIL import Image | |
N = 5 | |
arr = zeros( (1,N,3) ) | |
arr[:] = linspace(0,1,N)[newaxis,:,newaxis] | |
Image.fromarray( (arr*255).round().clip(0,255).astype(uint8) ).save( "test.png" ) | |
''' | |
## Use ImageMagick to assign an sRGB header | |
convert test.png -set colorspace sRGB -define png:color-type=2 test_sRGB.png | |
## Use ImageMagick to assign a linear RGB header | |
convert test.png -set colorspace RGB -define png:color-type=2 test_RGB.png | |
## Print the values after loading with PIL (Python Image Library). PIL ignores the header | |
## and gives us the same raw values: | |
python print_png.py test.png | |
python print_png.py test_sRGB.png | |
python print_png.py test_RGB.png | |
''' | |
from numpy import * | |
from PIL import Image | |
import sys | |
arr = asarray( Image.open( sys.argv[1] ) ) | |
print( arr ) | |
''' | |
## Open the images with Preview. Preview on OS X 10.11.6 ignores the colorspace when showing images. Using a color picker confirms this. | |
open test.png | |
open test_sRGB.png | |
open test_RGB.png | |
## With Safari, all three look the same in http://yig.github.io/image-rgb-in-3D/ | |
## Safari is ignoring headers. | |
## With Chrome, the RGB one looks different http://yig.github.io/image-rgb-in-3D/ | |
## Chrome does not ignore headers and converts values to sRGB for pixel | |
## values in JavaScript. | |
## Check the metadata. | |
## The original has nothing: | |
exiftool test.png | |
## sRGB is full of information (gamma, white point, red, green, and blue X Y): | |
exiftool test_sRGB.png | |
## Linear RGB has gamma = 1: | |
exiftool test_RGB.png | |
## Use ImageMagick to convert to sRGB. This does nothing because ImageMagick assumes sRGB | |
## for a PNG with no header. | |
convert test.png -colorspace sRGB -define png:color-type=2 test_colorspace_sRGB.png | |
python print_png.py test_colorspace_sRGB.png | |
## Use ImageMagick to convert to RGB. This changes all the values stored in the image by | |
## applying inverse gamma correction, because ImageMagick assumes sRGB. | |
convert test.png -colorspace RGB -define png:color-type=2 test_colorspace_RGB.png | |
python print_png.py test_colorspace_RGB.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment