Rename men_at_work to water_scene, crop and zoom IMG_2412.jpg for continuity
[stop_motion.git] / post.py
1 #!/usr/bin/env python3
2
3 import gamma
4 import numpy
5 import perspective
6 import scipy
7 import scipy.ndimage
8 import sys
9
10 sys.stderr.write(
11   '''stdin: crop and file list
12 x0,y0 : top left (pixels)
13 xs,ys : size (pixels)
14 in.png out.png
15 ...
16 '''
17 )
18
19 x0, y0 = (int(i) for i in sys.stdin.readline().split(','))
20 xs, ys = (int(i) for i in sys.stdin.readline().split(','))
21
22 images = []
23 out_pngs = []
24 line = sys.stdin.readline()
25 while len(line):
26   fields = line.split()
27   in_png = fields[0]
28   out_png = fields[1]
29   sys.stderr.write(f'read {in_png:s}\n')
30   images.append(gamma.read_image(in_png)[y0:y0 + ys, x0:x0 + xs, :3])
31   out_pngs.append(out_png)
32   line = sys.stdin.readline()
33 images = numpy.stack(images, 0)
34
35 # get standardization parameters and remove from images
36 mean = numpy.mean(numpy.mean(images, 1), 1)
37 #images -= mean[:, numpy.newaxis, numpy.newaxis, :]
38 #stddev = numpy.sqrt(numpy.mean(numpy.mean(numpy.square(images), 1), 1))
39 #images /= stddev[:, numpy.newaxis, numpy.newaxis, :]
40 images /= mean[:, numpy.newaxis, numpy.newaxis, :]
41
42 # find a middle ground of standardization parameters and apply to images
43 #stddev = numpy.mean(stddev, 0)
44 #images *= stddev[numpy.newaxis, numpy.newaxis, numpy.newaxis, :]
45 mean = numpy.mean(mean, 0)
46 #images += mean[numpy.newaxis, numpy.newaxis, numpy.newaxis, :]
47 images *= mean[numpy.newaxis, numpy.newaxis, numpy.newaxis, :]
48
49 for i in range(len(out_pngs)):
50   sys.stderr.write(f'write {out_pngs[i]:s}\n')
51   gamma.write_image(out_pngs[i], images[i, :, :, :])