File: side_by_side_images.py

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (60 lines) | stat: -rwxr-xr-x 1,790 bytes parent folder | download
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
#!/usr/bin/env python
# quick script for comparing SVGs in 2 different directories.
import argparse
import glob
from pathlib import Path

parser = argparse.ArgumentParser(description='Make an HTML table for comparing images.')
parser.add_argument('--dir1', required=True, help='Name of first directory.')
parser.add_argument('--dir2', required=True, help='Name of second directory.')
parser.add_argument('--file-glob', required=True, help='glob for files in first directory')
parser.add_argument('--outfile', default='side_by_side.html', help='Name of HTML file')

args = parser.parse_args()

d1 = args.dir1
d2 = args.dir2

if not Path(d1).exists():
    print(f'Directory {d1} missing.')
    exit(1)
if not Path(d2).exists():
    print(f'Directory {d2} missing.')
    exit(1)

with open(args.outfile, 'w') as f:
    f.write(f'''<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>{d1} vs {d2}</title>
    <link href="index.css" rel="stylesheet" />
  </head>
  <body>
    <table border="1">
      <tr>
        <td></td>
        <td>{d1}</td>
        <td>{d2}</td>
      </tr>\n''')

    inglob = Path(d1)
    fns = [Path(fn) for fn in inglob.glob(args.file_glob)]
    fns.sort(key = lambda f: f.stat().st_mtime, reverse=True)

    for fp in fns:
        fn = fp.name
        if not fn.endswith('.svg') and not fn.endswith('.png'):
            continue
        fns = fn.replace('.svg', '')
        f.write(f'''      <tr>
        <td>{fns}</td>
        <td><img src="{d1}/{fn}" alt="{fns}"/></td>
        <td><img src="{d2}/{fn}" alt="{fns}"/></td>
        </tr>\n''')
    
    f.write('''    </table>
  </body>
</html>\n''')