summaryrefslogtreecommitdiff
path: root/misc/gen_image
blob: 2575d418746e538566195797cc07da8b5b10acb4 (plain)
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
#!/usr/bin/env python2

from __future__ import print_function

import argparse
import cv2
import os
import re
import sys

PROG_NAME = os.path.basename(sys.argv[0])

def die(msg):
    print('{}: error: {}'.format(PROG_NAME, msg), file=sys.stderr)
    sys.exit(1)

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('src_filename')
    p.add_argument('dest_filename')
    return p.parse_args()

def main():
    args = parse_args()

    src_filename = args.src_filename
    dest_filename = args.dest_filename

    if re.search('grayscale', dest_filename):
        imread_flags = cv2.IMREAD_GRAYSCALE
    else:
        imread_flags = cv2.IMREAD_COLOR

    img = cv2.imread(src_filename, imread_flags)

    match = re.search(r'(\d+)x(\d+)', dest_filename)
    if match is None:
        die('dest_filename {!r} contains no size'.format(dest_filename))

    width = int(match.groups(0)[0])
    height = int(match.groups(0)[1])

    res = cv2.resize(img, (width, height), interpolation = cv2.INTER_CUBIC)

    cv2.imwrite(dest_filename, res)

if __name__ == '__main__':
    main()