report.py 1.5 KB
Newer Older
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
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
#!/usr/bin/env python
# 
# Copyright 2021 Sean Fitzgibbon, University of Oxford
# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# 
import jinja2
import os.path as op
import os
import base64

def report_reg(dirs, out, embed=False):

    images = [f'{d}/alignment.png' for d in dirs]
    images = [d for d in images if op.exists(d)]

    images = [(op.dirname(f), to_base64(f) if embed else f) for f in images]
    
    env = jinja2.Environment(
        loader=jinja2.PackageLoader('slider', 'resources'),
        autoescape=jinja2.select_autoescape(['html', 'xml']),
        extensions=['jinja2.ext.do']
    )

    template = env.get_template('registration_report_template.html')
    html = template.render(
        images=images,
    )

    with open(out, 'w') as outfile:
        outfile.write(html)


def to_base64(fname):

    with open(fname, 'rb') as tmp:
        tmp.seek(0)
        s = base64.b64encode(tmp.read()).decode("utf-8")

    return 'data:image/{};base64,'.format(op.splitext(fname)[-1]) + s