-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_matrix.py
80 lines (63 loc) · 2.39 KB
/
img_matrix.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
usage: imgmatrix.py [-h] [--margin m] f w h img [img ...]
Arrange a number of images as a matrix.
positional arguments:
f Output filename.
w Width of the matrix (number of images).
h Height of the matrix (number of images).
img Images (w x h files).
optional arguments:
-h, --help show this help message and exit
--margin m Margin between images: integers are interpreted as pixels,
floats as proportions.
"""
#! /usr/bin/env python
import argparse
import itertools
import cv2
import numpy as np
if __name__=='__main__':
parser = argparse.ArgumentParser(
description='Arrange a number of images as a matrix.')
parser.add_argument('f', help='Output filename.')
parser.add_argument('w', type=int,
help='Width of the matrix (number of images).')
parser.add_argument('h', type=int,
help='Height of the matrix (number of images).')
parser.add_argument('img', nargs='+', help='Images (w x h files).')
parser.add_argument('--margin', metavar='m', nargs=1,
help='Margin between images: integers are '
'interpreted as pixels, floats as proportions.')
args = parser.parse_args()
w = args.w
h = args.h
n = w*h
if len(args.img) != n:
raise ValueError('Number of images ({}) does not match '
'matrix size {}x{}'.format(w, h, len(args.img)))
imgs = [cv2.imread(i) for i in args.img]
if any(i.shape != imgs[0].shape for i in imgs[1:]):
raise ValueError('Not all images have the same shape.')
img_h, img_w, img_c = imgs[0].shape
m_x = 0
m_y = 0
if args.margin is not None:
margin = args.margin[0]
if '.' in margin:
m = float(margin)
m_x = int(m*img_w)
m_y = int(m*img_h)
else:
m_x = int(margin)
m_y = m_x
imgmatrix = np.zeros((img_h * h + m_y * (h - 1),
img_w * w + m_x * (w - 1),
img_c),
np.uint8)
imgmatrix.fill(255)
positions = itertools.product(range(w), range(h))
for (x_i, y_i), img in itertools.izip(positions, imgs):
x = x_i * (img_w + m_x)
y = y_i * (img_h + m_y)
imgmatrix[y:y+img_h, x:x+img_w, :] = img
cv2.imwrite(args.f, imgmatrix)