summaryrefslogtreecommitdiff
path: root/bin/photocollage/collage.py
blob: 134735b60ca54e3e3e67d4f8c3f845a43eb21ad1 (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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# Copyright (C) 2014 Adrien Vergé
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import random

"""
Summary of collage objects:

----------------------
|                    |
|                    |
|       Page         |       The "Page" object represents the whole page that
|                    |       will give the final assembled image.
|                    |
----------------------

----------------------
|      |      |      |
|      |Column|      |
|      |      |Column|       A page is divided into columns.
|Column|      |      |
|      |      |      |
----------------------

----------------------
| Cell | Cell        |                   Each column contains cells. When a
|------|          x--|----- CellExtent   is located in several columns,
| Cell |-------------|                   its "extended" flag is set, and a
|------| Cell |      |                   CellExtent object is added to the
| Cell |      | Cell |                   column on the right to reserve the
----------------------                   place.

      ,------> Photo
     /          ,---------> Photo
----------------------
|      |             |                   Each cell is associated
|------|             |                   to a photo.
|      |-------------|
|------|      |      |----> Photo
|      |      |      |
----------------------
             `----------> Photo

The layout placing process is divided in three phases.

Phase A: Fill columns with photos.
  Photos are added to columns, one by one, until there are no more photos.
  Each new photo is put in the smallest column, so as to have balanced
  columns. If two columns have approximately the same height, a photo can be
  "extended" to fit two columns. In this case, the "Cell" object is put in
  the first column, and the second column takes a "CellExtent" object to
  reserve the taken space.

Phase B: Set all columns to same height.
  A global common height is computed for all columns, and every of them is
  stressed or extended to this common length. This can result in a decrease
  or increase in columns' width.

Phase C: Adapt columns' width.
  Since cells in a column may have different widths, each column width is set
  to the smallest width amongst its images.

"""


class Photo:
    def __init__(self, filename, w, h, orientation=0):
        self.filename = filename
        self.w = w
        self.h = h
        self.orientation = orientation
        self.offset_w = 0.5
        self.offset_h = 0.5

    @property
    def ratio(self):
        return float(self.h) / float(self.w)

    def move(self, x, y):
        self.offset_w = self.calculate_new_offset(self.offset_w, x)
        self.offset_h = self.calculate_new_offset(self.offset_h, y)

    @staticmethod
    def calculate_new_offset(offset, value):
        new_offset = offset + value
        if new_offset < 0:
            new_offset = 0
        elif new_offset > 1:
            new_offset = 1
        return new_offset


class Cell:
    """Represents a cell in a column

    Properties:
    <- x -><- w ->
    ---------------------- ^
    |      |             | |y
    |------|             | |
    |      |-------------| v
    |------| Cell |      | ^
    |      |      |      | |h
    ---------------------- v

    """
    def __init__(self, parents, photo):
        self.parents = parents
        self.photo = photo
        self.extent = None
        self.h = self.w * self.wanted_ratio

    def __repr__(self):
        """Representation of the cell in ASCII art"""
        end = "]"
        if self.extent is not None:
            end = "--"
        return "[%d %d%s" % (self.w, self.h, end)

    @property
    def x(self):
        return self.parents[0].x

    @property
    def y(self):
        """Returns the cell's y coordinate

        It assumes that the cell is in a single column, so it is the previous
        cell's y + h.

        """
        prev = None
        for c in self.parents[0].cells:
            if self is c:
                if prev:
                    return prev.y + prev.h
                return 0
            prev = c

    @property
    def w(self):
        return sum(c.w for c in self.parents)

    @property
    def ratio(self):
        return self.h / self.w

    @property
    def wanted_ratio(self):
        return self.photo.ratio

    def scale(self, alpha):
        self.h *= alpha

    def is_extended(self):
        return hasattr(self, 'extent') and self.extent is not None

    def is_extension(self):
        return isinstance(self, CellExtent)

    def content_coords(self):
        """Returns the coordinates of the contained image

        These are computed in order not to loose space, so the content area
        will always be greater than the cell itself. It is the space taken by
        the contained image if it wasn't cropped.

        """
        # If the contained image is too thick to fit
        if self.wanted_ratio < self.ratio:
            h = self.h
            w = self.h / self.wanted_ratio
            y = self.y
            x = self.x - (w - self.w) / 2.0
        # If the contained image is too tall to fit
        elif self.wanted_ratio > self.ratio:
            w = self.w
            h = self.w * self.wanted_ratio
            x = self.x
            y = self.y - (h - self.h) / 2.0
        else:
            w = self.w
            h = self.h
            x = self.x
            y = self.y
        return x, y, w, h

    def top_neighbor(self):
        """Returns the cell above this one"""
        prev = None
        for c in self.parents[0].cells:
            if self is c:
                return prev
            prev = c

    def bottom_neighbor(self):
        """Returns the cell below this one"""
        prev = None
        for c in reversed(self.parents[0].cells):
            if self is c:
                return prev
            prev = c


class CellExtent(Cell):
    def __init__(self, cell):
        self.origin = cell
        self.origin.extent = self

    def __repr__(self):
        """Representation of the cell in ASCII art"""
        return "------]"

    @property
    def parents(self):
        return (self.origin.parents[1],)

    @property
    def photo(self):
        return self.origin.photo

    @property
    def y(self):
        return self.origin.y

    @property
    def h(self):
        return self.origin.h

    def scale(self, alpha):
        pass


class Column:
    """Represents a column in a page

    Properties:
    <----- x ----><-- w ->
    ---------------------- ^
    |      |      |      | |
    |      |      |      |
    |      |      |Column| h
    |      |      |      |
    -------|      |      | |
           |      |------- v
           --------

    """
    def __init__(self, parent, w):
        self.parent = parent
        self.cells = []
        self.w = w

    def __repr__(self):
        """Representation of the column in ASCII art"""
        return "\n".join(c.__repr__() for c in self.cells)

    @property
    def h(self):
        """Returns the column's total height

        This is not simply the sum of its cells heights, because there can be
        empty spaces between cells.

        """
        if not self.cells:
            return 0
        return self.cells[-1].y + self.cells[-1].h

    @property
    def x(self):
        x = 0
        for c in self.parent.cols:
            if self is c:
                break
            x += c.w
        return x

    def scale(self, alpha):
        self.w *= alpha
        for c in self.cells:
            c.scale(alpha)

    def left_neighbor(self):
        """Returns the column on the left of this one"""
        prev = None
        for c in self.parent.cols:
            if self is c:
                return prev
            prev = c

    def right_neighbor(self):
        """Returns the column on the right of this one"""
        prev = None
        for c in reversed(self.parent.cols):
            if self is c:
                return prev
            prev = c

    def adjust_height(self, target_h):
        """Set the column's height to a given value by resizing cells"""
        # First, make groups of "movable" cells. Since cell extents are not
        # movable, these groups only contain pure cell objects. We only resize
        # those groups.
        class Group:
            def __init__(self, y):
                self.y = y
                self.h = 0
                self.cells = []

        groups = []
        groups.append(Group(0))
        for c in self.cells:
            # While a cell extent is not reached, keep add cells to the group
            if not c.is_extension():
                groups[-1].cells.append(c)
            else:
                # Close current group and create a new one
                groups[-1].h = c.y - groups[-1].y
                groups.append(Group(c.y + c.h))
        groups[-1].h = target_h - groups[-1].y

        # Adjust height for each group independently
        for group in groups:
            if not group.cells:
                continue
            alpha = group.h / sum(c.h for c in group.cells)
            for c in group.cells:
                c.h = c.h * alpha


class Page:
    """Represents a whole page

    Properties:
    <-------- w -------->
    ---------------------- ^
    |                    | |
    |                    |
    |        Page        | h
    |                    |
    |                    | |
    ---------------------- v

    """
    def __init__(self, w, target_ratio, no_cols):
        self.target_ratio = target_ratio
        col_w = float(w)/no_cols
        self.cols = []
        for i in range(no_cols):
            self.cols.append(Column(self, col_w))

    def __repr__(self):
        """Representation of the page in ASCII art

        Returns something like:
        [62 52]    [125 134-- ------]    [62 87]
        [62 47]    [62 66]    [125 132-- [62 45]
        [62 46]    ------]    [62 49]    ------]
        [62 78]    ------]    [62 49]    [62 45]
        [125 102-- ------]    [62 49]    [62 65]
        [125 135--            [62 85]    [62 53]
        [125 91--             [125 89--  [62 64]
                                 ------]
        """
        lines = []
        n = 0
        end = False
        while not end:
            lines.append("")
            end = True
            for col in self.cols:
                cells = col.__repr__().split("\n")
                w = max(len(cell) for cell in cells)
                if col != self.cols[-1]:
                    w += 1
                cell = w * " "
                if n < len(cells):
                    cell = cells[n] + (w - len(cells[n])) * " "
                    if n < len(cells) - 1:
                        end = False
                lines[-1] += cell
            n += 1
        return "\n".join(lines)

    @property
    def no_cols(self):
        return len(self.cols)

    @property
    def w(self):
        return sum(c.w for c in self.cols)

    @property
    def h(self):
        return max(c.h for c in self.cols)

    @property
    def ratio(self):
        return self.h / self.w

    def scale(self, alpha):
        for c in self.cols:
            c.scale(alpha)

    def scale_to_fit(self, max_w, max_h=None):
        if max_h is None or self.w * max_h > self.h * max_w:
            self.scale(max_w / self.w)
        else:
            self.scale(max_h / self.h)

    def next_free_col(self):
        """Returns the column with lowest height"""
        minimum = min(c.h for c in self.cols)
        candidates = []
        for c in self.cols:
            if c.h == minimum:
                candidates.append(c)
        return random.choice(candidates)

    def add_cell_single_col(self, col, photo):
        col.cells.append(Cell((col,), photo))

    def add_cell_multi_col(self, col1, col2, photo):
        cell = Cell((col1, col2), photo)
        extent = CellExtent(cell)
        col1.cells.append(cell)
        col2.cells.append(extent)

    def add_cell(self, photo):
        """Add a new cell in the best computed place

        If possible, and if it's worth, make a "multiple-column" cell.

        """
        col = self.next_free_col()
        left = col.left_neighbor()
        right = col.right_neighbor()
        if 2 * random.random() > photo.ratio:
            if left and abs(col.h - left.h) < 0.5 * col.w:
                return self.add_cell_multi_col(left, col, photo)
            elif right and abs(col.h - right.h) < 0.5 * col.w:
                return self.add_cell_multi_col(col, right, photo)

        self.add_cell_single_col(col, photo)

    def remove_empty_cols(self):
        i = 0
        while i < len(self.cols):
            if len(self.cols[i].cells) == 0:
                self.cols.pop(i)
            else:
                i += 1

    def remove_bottom_holes(self):
        """Remove holes created by extended cells

        Example (case A):
        The bottom-right cell should be extended to fill the hole.
        ----------------------             ----------------------
        |      |      |      |             |      |      |      |
        |      |-------------|             |      |-------------|
        |------|             |             |------|             |
        |      |--------------             |      |--------------
        |      |      |  ^                 |      |  ^   |      |
        --------------- hole               -------- hole --------

        Example (case B):
        The bottom cell should be moved under the other extended cell.
        ----------------------             ----------------------
        |      |      |      |             |      |      |      |
        |------|-------------|             |-------------|------|
        |      |             |             |             |      |
        |---------------------             ---------------------|
        |             |   <-- hole      hole ->   |             |
        ---------------                           ---------------

        """
        for col in self.cols:
            cell = col.cells[-1]
            if cell == col.cells[0]:
                continue

            # Case A
            # If cell is not extended, is below an extended cell and has no
            # neighbour under the latter, it should be extended.
            if not cell.is_extended() and not cell.is_extension():
                # Case A1
                if cell.top_neighbor().is_extended() \
                        and cell.top_neighbor().extent \
                        .bottom_neighbor() is None:
                    # Extend cell to right
                    extent = CellExtent(cell)
                    col.right_neighbor().cells.append(extent)
                    cell.parents = (col, col.right_neighbor())
                # Case A2
                elif cell.top_neighbor().is_extension() \
                        and cell.top_neighbor().origin \
                        .bottom_neighbor() is None:
                    # Extend cell to left
                    col.cells.remove(cell)
                    col.left_neighbor().cells.append(cell)
                    extent = CellExtent(cell)
                    col.cells.append(extent)
                    cell.parents = (col.left_neighbor(), col)
            # Case B
            # If cell is extended and one of the cells above is extended too,
            # the bottom cell should be placed right below the top one.
            elif cell.is_extended() and cell.extent.bottom_neighbor() is None:
                # Case B1
                if cell.extent.top_neighbor().is_extended() \
                        and cell.extent.top_neighbor().extent \
                        .bottom_neighbor() is None:
                    # Move cell to right
                    col.cells.remove(cell)
                    col.right_neighbor().cells.remove(cell.extent)
                    col.right_neighbor().cells.append(cell)
                    col.right_neighbor().right_neighbor().cells \
                        .append(cell.extent)
                    cell.parents = (col.right_neighbor(),
                                    col.right_neighbor().right_neighbor())
                # Case B2
                elif cell.top_neighbor().is_extension() \
                        and cell.top_neighbor().origin \
                        .bottom_neighbor() is None:
                    # Move cell to left
                    col.cells.remove(cell)
                    col.right_neighbor().cells.remove(cell.extent)
                    col.left_neighbor().cells.append(cell)
                    col.cells.append(cell.extent)
                    cell.parents = (col.left_neighbor(), col)

    def adjust_cols_heights(self):
        """Set all columns' heights to same value by shrinking them"""
        target_h = self.w * self.target_ratio
        for c in self.cols:
            c.adjust_height(target_h)

    def adjust(self):
        self.remove_empty_cols()
        self.remove_bottom_holes()
        self.adjust_cols_heights()

    def get_cell_at_position(self, x, y):
        for col in self.cols:
            if x >= col.x and x < col.x + col.w:
                for cell in col.cells:
                    if y >= cell.y and y < cell.y + cell.h:
                        if cell.is_extension():
                            return cell.origin
                        return cell
        return None

    def swap_photos(self, cell1, cell2):
        cell1.photo, cell2.photo = cell2.photo, cell1.photo