-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlCaMa.py
316 lines (240 loc) · 8.2 KB
/
AlCaMa.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
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
#!/usr/bin/env python
#! coding: utf-8
#! python3
# Guillaume Viravau 2020-2023
# A small script to generate caption and/or albums from json file
import os
import json
import copy
import argparse
from PIL import Image, ImageDraw, ImageFont
def parse_arguments():
"""
Parses the arguments
"""
parser = argparse.ArgumentParser( description="Creates album from JSON" )
parser.add_argument( '-i', "--json", action="store", dest="input_json", help="Json to read", default=None )
parser.add_argument( '-o', '--output-folder', action="store", dest="output_folder", help="Folder to save album to", default=None )
parser.add_argument( '-v', '--verbose', action="store_true", dest="verbose", help="Verbose output", default=False )
return parser.parse_args()
# make the text multilines to fit any given width with a given font
def text_multilines( text="", pixelWidth=80, font=None ):
text = text.split( " " )
finalText = ""
line = ""
for word in text :
if( font.getsize( f"{line}{word}" )[0] > pixelWidth ):
finalText += line+"\n"
line = f"{word} "
else:
line = f"{line}{word} "
finalText += line+"\n"
return finalText
def colorOf( string=None ) :
"""Convert a color from a string to an RGB"""
color = (0,0,0)
if string is not None :
# '#' familly
if string[0] == '#':
# Format : "#17B"
if len(string) == 4:
# Convert "#17B" to "#1177BB"
string = "#"+(string[1]*2)+(string[2]*2)+(string[3]*2)
# Format : "#1278BC"
if len(string) == 7:
color = ( int(string[1:3], 16), int(string[3:5], 16), int(string[5:7], 16) )
# 'rgb()' familly
if string.startswith("rgb("):
if (string[-1] == ")") and (string.count(",") == 2) :
# Is it 'Pythonic' enought for you now?
r,g,b = [int(a.strip()) for a in string[4:-1].split(",")]
color = (r, g, b)
else :
print( "Malformed color : "+string )
# 'hsl()' familly
return color
def album_load_data( file=None ) :
# Reading the file
if file is not None :
with open( file, "r" ) as rf :
data = json.load( rf )
# Default values
if "name" not in data :
data["name"] = "Unnamed"
default = {
"file-format":"webp",
"size":[800,800],
"padding":16,
"style":"polaroid",
"picture-style":"contain",
"picture-align":"center",
"picture-background-color":"#111",
"font-size":20,
"font-name":"tahoma.ttf",
"font-family":["Verdana", "sans-serif"],
"color":"#111",
"background-color":"#FAFAFA"
}
if "configuration" not in data :
data["configuration"] = copy.deepcopy( default )
else :
for parameter in default :
if parameter not in data["configuration"] :
data["configuration"][parameter] = default[parameter]
print("added "+parameter)
return data
def album_generate( album=None ):
index = 0
for picture in album["pictures"] :
print( picture+" : ", end="" )
picturePath = os.path.join(albumFolder, picture)
configuration = {}
configuration = copy.deepcopy( album["configuration"] )
configuration["album-name"] = album["name"]
configuration["index"] = index
configuration["file"] = picturePath
for parameter in album["pictures"][picture] :
configuration[parameter] = album["pictures"][picture][parameter]
if os.path.exists(picturePath) :
picture_process( configuration )
else :
print("File not found")
index += 1
def picture_process( picture=None ) :
if picture != None :
# Style Polaroid
if picture["style"] == "polaroid" :
# Polaroid
# Full : 164*200 -> h = w * 1.22
# Pading : 10 -> pad = w * 0.06
# Image : 144*147 -> hp = wp * 1.02
# Text : 144*23
# coords={}
# coords["image"] = (0,0, 164,200)
# coords["picture"] = (10,10, 10+144,10+147)
# coords["text"] = (10,167, 10+144,167+23)
size = picture["size"]
padding = picture["padding"]
W = size[0]
H = int(W * 1.22)
pad = int(W * 0.06)
# start, stop
coords={}
coords["image"] = (0,0, W,H)
coords["picture"] = (pad,pad, W-pad,pad+int(W*0.89))
coords["text"] = (pad,(2*pad)+int(W*0.89), W-pad,H-pad)
img = Image.new( "RGB", coords["image"][2:4], color=colorOf(picture["background-color"]) )
draw = ImageDraw.Draw( img )
# Picture
draw.rectangle( coords["picture"], fill=colorOf(picture["picture-background-color"]), outline=None )
# Get image and infos
im = Image.open( picture["file"] )
ratio = "landscape"
pictureStyle = "contain"
if "picture-style" in picture : pictureStyle = picture["picture-style"]
# contain : Aspect ratio is kept, whole image is visible, even if showing background
if pictureStyle == "contain" :
x,y = coords["picture"][0:2]
sw,sh = coords["picture"][2:4]
sw -= x
sh -= y
if im.size[0] > im.size[1] :
ratio = "horizontal"
u = 1
v = im.size[1] / im.size[0]
else :
ratio = "vertical"
u = im.size[0] / im.size[1]
v = 1
w = int((sw*u))
h = int((sh*v))
# cover : Aspect ratio is kept, no more background is visible, even if part of the image is cut
elif pictureStyle == "cover" :
x,y = coords["picture"][0:2]
sw,sh = coords["picture"][2:4]
sw -= x
sh -= y
if im.size[0] > im.size[1] :
ratio = "vertical"
u = im.size[0] / im.size[1]
v = 1
else :
ratio = "horizontal"
u = 1
v = im.size[1] / im.size[0]
w = int((sw*u))
h = int((sh*v))
# stretch : The image takes all the space, even if distorded
else:
x,y = coords["picture"][0:2]
sw,sh = coords["picture"][2:4]
w = sw-x
h = sh-y
# top middle bottom
# left center right
align = "center"
if "picture-align" in picture : align = picture["picture-align"]
print(align + " " + ratio)
if ratio == "horizontal" :
# top middle bottom
if align == "center" :
y = coords["picture"][1]+int((sh-(sh*v))/2)
elif align == "bottom-right" :
y = coords["picture"][3]-int(sh*v)
else :
y = coords["picture"][1]
else :
# left center right
if align == "center" :
x = coords["picture"][0]+int((sw-(sw*u))/2)
elif align == "bottom-right" :
x = coords["picture"][2]-int(sw*u)
else :
x = coords["picture"][0]
# TODO : .crop((x,y, x+w, y+h))
img.paste( im.resize( (w,h) ), (x,y) )
#----------
# Text
#draw.rectangle( coords["text"], fill=(200,200,200), outline=None )
# Load font
try :
font = ImageFont.truetype( picture["font-name"], picture["font-size"], encoding="unic" )
except :
print( f"Font {picture['font-name']} not found. Using built-in one" )
font = ImageFont.load_default()
# Prepare and draw text
text = text_multilines( picture["caption"], pixelWidth=(coords["text"][2]-coords["text"][0]), font=font )
draw.multiline_text( coords["text"][0:2], text, font=font, fill=colorOf(picture["color"]) )
# Default is text
else :
print("No style found. Using 'text'")
img = Image.open( picture["file"] )
draw = ImageDraw.Draw( img )
font = ImageFont.truetype( "tahoma.ttf", picture["font-size"], encoding="unic" )
text = picture["caption"]
draw.text( (0,0), text, font=font, fill=colorOf(picture["color"]) )
# Save the image
filename = f'{picture["album-name"]} ({picture["index"]+1}).{picture["file-format"]}'
print( filename )
img.save( filename )
#===============================================================================
# Main
if __name__ == "__main__" :
arguments = parse_arguments()
print( "AlCaMa" )
print( arguments )
if arguments.input_json != None :
file = arguments.input_json
if os.path.exists( file ) :
if os.path.isfile( file ) :
file = os.path.abspath( file )
albumFolder = os.path.dirname( file )
print( albumFolder )
album = album_load_data( file )
album_generate( album )
else:
print( f"{file} is not a file" )
else:
print( f"{file} does not exists" )
else:
print( "No input file given" )