Use estimated position of feature within block rather than just block's centre
[stop_motion.git] / gamma.py
1 #!/usr/bin/env python3
2
3 import imageio
4 import numpy
5
6 def read_image(in_file):
7   # read double image
8   image = imageio.imread(in_file) / 255.
9   
10   # gamma decode
11   linear = image < .0031308 * 12.92
12   image[linear] /= 12.92
13   image[~linear] = ((image[~linear] + .055) / 1.055) ** 2.4
14
15   return image
16
17 def write_image(out_file, image):
18   image = numpy.copy(image)
19
20   # gamma encode
21   linear = image < .0031308
22   image[linear] *= 12.92
23   image[~linear] = 1.055 * image[~linear] ** (1. / 2.4) - .055
24
25   # write double image
26   image[image < 0.] = 0.
27   image[image > 1.] = 1.
28   imageio.imwrite(
29     out_file,
30     numpy.round(image * 255.).astype(numpy.uint8)
31   )
32
33 if __name__ == '__main__':
34   import sys
35   
36   if len(sys.argv) < 3:
37     print(f'usage: {sys.argv[0]:s} in_file out_file')
38     sys.exit(1)
39   in_file = sys.argv[1]
40   out_file = sys.argv[2]
41   
42   write_image(out_file, read_image(in_file))