/
stereoscoper-from-inkscape-pairs
executable file
·61 lines (52 loc) · 1.72 KB
/
stereoscoper-from-inkscape-pairs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
from __future__ import print_function
import os, sys
from xml.dom import minidom
import urllib
import subprocess
here = os.path.dirname(__file__)
from optparse import OptionParser
def extract_path(img):
return urllib.unquote(img[0])[len('file://'):]
def main():
p = OptionParser()
p.add_option('-l', "--list", action='store_true')
p.add_option('-d', "--dest")
p.parse_args()
opts, args = p.parse_args()
for filename in args:
images = []
with open(filename) as inf:
dom = minidom.parse(inf)
# print(dom.toprettyxml())
for img in dom.getElementsByTagName('image'):
href = img.getAttribute('xlink:href')
x = float(img.getAttribute('x'))
y = float(img.getAttribute('y'))
# print("%s @ %sx%s" % (href, x, y))
images.append((href, (x,y)))
images_by_height = sorted(images, key = lambda img: img[1][1])
for i in range(0, len(images_by_height), 2):
pair = images_by_height[i: i+2]
assert len(pair) == 2, (i, pair)
pair = sorted(pair, key = lambda img: img[1][0])
# pairs.append(list(map(extract_path, pair)))
pair = list(map(extract_path, pair))
print('\t'.join(pair))
if opts.list: continue # don't run stereoscoper
name = '-'.join([os.path.splitext(os.path.basename(path))[0] for path in pair]).replace('_','')
basedir = opts.dest or os.path.dirname(pair[0])
outpath = os.path.join(basedir, name + '.jpg')
if not os.path.exists(outpath):
print('Generating: %s' % outpath, file=sys.stderr)
subprocess.check_call([
'stereoscoper',
'--merge',
'--left', pair[0],
'--right', pair[1],
'--gui',
'--output', outpath])
else:
print('Skipping existing: %s' % outpath, file=sys.stderr)
if __name__ == '__main__':
main()