This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collage.py
154 lines (125 loc) · 5.4 KB
/
collage.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from operator import itemgetter
from optparse import OptionParser
import logging
from PIL import Image
from math import ceil
import sys
"""
Create a collage of images, filling all space without cropping.
Original source: https://gist.github.com/JesseCrocker/cfd05006335c2c828a2b
Last modification: https://gist.github.com/baloo1379/d6c4e3995cc17df79ec2ffe17de259dd
Author: https://gist.github.com/JesseCrocker
Copyright 2014 Trailbehind inc
Implements algorithm presented in
https://www.crispymtn.com/stories/the-algorithm-for-a-perfectly-balanced-photo-gallery
Linear partition algorithm from
http://stackoverflow.com/questions/7938809/dynamic-programming-linear-partitioning-please-help-grok/7942946#7942946
2015-01-05 - JesseCrocker - initial release
2015-01-12 - JesseCrocker - small changes
2019-04-14 - baloo1379 - make support for Python 3
"""
def linear_partition(seq: list, k: int):
if k <= 0:
return []
n = len(seq) - 1
if k > n:
return map(lambda x: [x], seq)
values = [i[0] for i in seq]
table, solution = linear_partition_table(values, k)
k, ans = k - 2, []
while k >= 0:
ans = [[seq[i] for i in range(solution[n - 1][k] + 1, n + 1)]] + ans
n, k = solution[n - 1][k], k - 1
ans = [[seq[i] for i in range(0, n + 1)]] + ans
return ans
def linear_partition_table(seq: list, k: int):
n = len(seq)
table = [[0] * k for x in range(n)]
solution = [[0] * (k - 1) for x in range(n - 1)]
for i in range(n):
table[i][0] = seq[i] + (table[i - 1][0] if i else 0)
for j in range(k):
table[0][j] = seq[0]
for i in range(1, n):
for j in range(1, k):
table[i][j], solution[i - 1][j - 1] = min(
((max(table[x][j - 1], table[i][0] - table[x][0]), x) for x in range(i)),
key=itemgetter(0))
return table, solution
def create_collage(image_files: list, result_width: int, rows: int):
"""
Create a collage of images, with <rows> rows, filling all space without cropping
"""
logging.debug("Creating collage of %d images with %d rows" % (len(image_files), rows))
if len(image_files) < rows:
rows = len(image_files)
images = []
for filename_or_image in image_files:
if isinstance(filename_or_image, Image.Image):
im = filename_or_image
else:
im = Image.open(filename_or_image)
aspect = float(im.size[0]) / float(im.size[1])
images.append((aspect, im))
partioned_images = linear_partition(images, int(rows))
result_height = 0
for row in partioned_images:
row_width = float(sum([i[0] for i in row]))
aspect, image = row[0]
width_percent = aspect / row_width
image_width = width_percent * result_width
scale_ratio = image_width / float(image.size[0])
result_height += image.size[1] * scale_ratio
result_size = (result_width, int(result_height))
logging.debug("Creating result image size: %f*%f" % result_size)
result = Image.new('RGB', result_size)
y_position = 0
for row in partioned_images:
row_width = float(sum([i[0] for i in row])) # Sum of aspect ratios for all images in row
x_position = 0
row_height = 0
for aspect, image in row:
width_percent = aspect / row_width
image_width = width_percent * result_width
scale_ratio = image_width / float(image.size[0])
logging.debug(
("Image size: %f %f" % image.size) + " percent of row " + str(width_percent) + " new width " + str(
image_width) + " scale_ratio " + str(scale_ratio))
new_size = [int(ceil(i * scale_ratio)) for i in image.size]
logging.debug("resizing image to " + str(new_size))
resized_image = image.resize(new_size)
paste_position = (x_position, y_position)
logging.debug("pasting image at " + str(paste_position))
result.paste(resized_image, paste_position)
x_position += new_size[0]
row_height = new_size[1]
y_position += row_height
return result
def _main():
usage = "usage: %prog img.jpg img2.jpg ..."
parser = OptionParser(usage=usage,
description="Create a collage of images, filling all space, without cropping any images")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
help="Turn on debug logging")
parser.add_option("-o", "--output", action="store", dest="output",
default=None, help="output to file, if no file is specified image will be displayed")
parser.add_option("-w", "--width", action="store", dest="width", type="int",
default=1000, help="Image width")
parser.add_option("-r", "--rows", action="store", dest="rows", type="int",
default=None, help="Number of rows, default is <number of images>/2")
(options, args) = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if options.debug else logging.ERROR)
if len(args) < 2:
logging.error("Error, must specify at least 2 images")
sys.exit(-1)
if options.rows is not None:
rows = options.rows
else:
rows = len(args) / 2
result = create_collage(args, options.width, rows)
if options.output:
result.save(options.output)
else:
result.show()
if __name__ == "__main__":
_main()