diff options
author | Jason Ekstrand <jason.ekstrand@intel.com> | 2015-12-08 14:07:25 -0800 |
---|---|---|
committer | Jason Ekstrand <jason.ekstrand@intel.com> | 2015-12-08 14:34:48 -0800 |
commit | 8db88894655efa8223cbd7e9a059af0afc832446 (patch) | |
tree | ae80d786eb3cf8ed8d51e59d1c7b7220c4bf3459 | |
parent | aec6c4f820a694f0ea5f7c26da525ff21f00d19e (diff) |
gen_image: Use python OpenCV instead of ImageMagick
Python with opencv provides us a lot more control of what's going on and
its interface shouldn't change arbitrarily.
-rw-r--r-- | Makefile.am | 8 | ||||
-rwxr-xr-x | misc/gen_image | 36 |
2 files changed, 15 insertions, 29 deletions
diff --git a/Makefile.am b/Makefile.am index de720c7..5b75bfd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -212,9 +212,7 @@ data/grass-2048x1024.jpg: $(srcdir)/data/grass-2014x1536.jpg data/grass-%.png: \ $(srcdir)/data/grass-2048x1024.jpg \ $(srcdir)/misc/gen_image - $(AM_V_GEN) $(PYTHON3) $(srcdir)/misc/gen_image \ - --convert-tool="$(IMAGEMAGICK_CONVERT)" \ - $< $@ + $(AM_V_GEN) $(PYTHON2) $(srcdir)/misc/gen_image $< $@ data/pink-leaves-2048x1024.jpg: $(srcdir)/data/pink-leaves-3264x2448.jpg $(AM_V_GEN) @@ -224,9 +222,7 @@ data/pink-leaves-2048x1024.jpg: $(srcdir)/data/pink-leaves-3264x2448.jpg data/pink-leaves-%.png: \ $(srcdir)/data/pink-leaves-2048x1024.jpg \ $(srcdir)/misc/gen_image - $(AM_V_GEN) $(PYTHON3) $(srcdir)/misc/gen_image \ - --convert-tool="$(IMAGEMAGICK_CONVERT)" \ - $< $@ + $(AM_V_GEN) $(PYTHON2) $(srcdir)/misc/gen_image $< $@ TESTS = \ src/tests/self/bad-test-names.bash \ diff --git a/misc/gen_image b/misc/gen_image index f9d6f5e..2575d41 100755 --- a/misc/gen_image +++ b/misc/gen_image @@ -1,12 +1,13 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python2 + +from __future__ import print_function import argparse +import cv2 import os import re import sys -from subprocess import check_call - PROG_NAME = os.path.basename(sys.argv[0]) def die(msg): @@ -15,8 +16,6 @@ def die(msg): def parse_args(): p = argparse.ArgumentParser() - p.add_argument('--convert-tool', default='convert') - p.add_argument('-d', '--debug', action='store_true') p.add_argument('src_filename') p.add_argument('dest_filename') return p.parse_args() @@ -27,32 +26,23 @@ def main(): src_filename = args.src_filename dest_filename = args.dest_filename - # The badly named "convert" tool is an ImageMagick tool. - convert_args = [args.convert_tool, src_filename] - if re.search('grayscale', dest_filename): - convert_args += [ - '-set', 'colorspace', 'Gray', - '-separate', - '-average', - ] + 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 = match.groups(0)[0] - height = match.groups(0)[1] - - convert_args += [ - '-adaptive-resize', '{}x{}'.format(width, height), - dest_filename, - ] + width = int(match.groups(0)[0]) + height = int(match.groups(0)[1]) - if args.debug: - print('run: ' + ' '.join(map(repr, convert_args))) + res = cv2.resize(img, (width, height), interpolation = cv2.INTER_CUBIC) - check_call(convert_args) + cv2.imwrite(dest_filename, res) if __name__ == '__main__': main() |