forked from jomjol/neural-network-digital-counter-readout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-overview.py
69 lines (46 loc) · 1.97 KB
/
generate-overview.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
import os
import math
from PIL import Image, ImageOps
import logging
def main():
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG)
values = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
path = './ziffer_sortiert_raw'
for value in values:
generate(path, value, 15)
def concat_images(image_paths, size, shape=None):
# Open images and resize them
width, height = size
images = map(Image.open, image_paths)
images = [ImageOps.expand(image, border=10,fill='white')
for image in images]
images = [ImageOps.fit(image, size, Image.ANTIALIAS)
for image in images]
# Create canvas for the final image with total size
shape = shape if shape else (1, len(images))
image_size = (int(width * shape[1]), int(height * shape[0]))
image = Image.new('RGB', image_size, color='white')
# Paste images into final image
for row in range(shape[0]):
for col in range(shape[1]):
offset = width * col, height * row
idx = row * shape[1] + col
try:
image.paste(images[idx], offset)
except:
pass
return image
def generate(path, prefix, cols):
logging.info("Generating summary image of all '%s*.jpg' images in %s..." % (prefix, path))
# Get list of image paths
image_paths = [os.path.join(path, f)
for f in os.listdir(path) if (f.startswith(prefix) and f.endswith('.jpg'))]
image_paths.sort()
logging.debug("Found %d images." % len(image_paths))
rows = math.ceil(float(len(image_paths)) / cols)
logging.debug("Generating grid of %d x %d images" % (cols, rows))
# Create and save image grid
image = concat_images(image_paths, (int(800/cols), int(800/cols)), (rows, cols))
image.save("./html_output/digital-" + prefix + ".jpg", 'JPEG')
if __name__ == "__main__":
main()