def _create_preview images filename TODO add frame_enable add height a

 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
def _create_preview(images, filename):
"""
TODO:
* add frame_enable
* add height
* add column_length
"""
# Ident between images
X_INDENT = 8
Y_INDENT = 8
# Creating Image objects
try:
images = map(Image.open, images)
except IOError:
ROOT_LOGGER.debug('Image files have unknown format. Aborting.')
return
i_width, i_heigth = images[0].size
try:
WIDTH = config.WIDTH
# WIDTH is None / False / 0 / ''
if not WIDTH:
raise AttributeError
else:
# resizing is not forced and image width
# is lower then WIDTH -> no resizing
if not config.FORCE_WIDTH and \
WIDTH > i_width:
raise AttributeError
except AttributeError:
WIDTH, HEIGHT = i_width, i_heigth
if WIDTH != i_width:
# Resizing images to match given WIDTH
HEIGHT = WIDTH * i_heigth / i_width
images = map(lambda i: i.resize( (WIDTH, HEIGHT) ) , images)
try:
ROW_LENGTH = config.ROW_LENGTH
except AttributeError:
ROW_LENGTH = 2
# Creating output image object (two images in a row)
l = len(images)
if l == 1:
ROW_LENGTH = l
x_size = X_INDENT + WIDTH * ROW_LENGTH
if l <= ROW_LENGTH:
y_size = HEIGHT*l
else:
t = int(math.ceil(l / float(ROW_LENGTH)))
y_size = Y_INDENT * (t - 1) + HEIGHT * t
o = Image.new('RGBA', (x_size , y_size ), (0, 0, 0, 0))
x_offset, y_offset = 0, 0
counter = 0
for i in images:
o.paste(i, (x_offset, y_offset))
x_offset += WIDTH + X_INDENT
counter += 1
if counter == ROW_LENGTH:
counter = 0
x_offset = 0
y_offset += HEIGHT + Y_INDENT
# Adding watermark
watermark = Image.new('RGBA', (x_size, y_size), (0, 0, 0, 0))
text = ImageDraw.ImageDraw(watermark, 'RGBA')
font = ImageFont.truetype(config.W_FONT, config.W_FONT_SIZE)
text.setfont(font)
text.text((x_size - font.getsize(config.W_TEXT)[0] - X_INDENT,
y_size - config.W_FONT_SIZE - Y_INDENT), config.W_TEXT)
mask = watermark.convert('L').point(lambda x: min(x, config.W_TEXT_ALPHA))
watermark.putalpha(mask)
o.paste(watermark, None, watermark)
o.save(filename, 'PNG')