This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
325 lines (245 loc) · 8.6 KB
/
application.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
317
318
319
320
321
322
323
324
325
import base64
import hashlib
import io
import json
import logging
import tempfile
import os
import urllib.request
from collections import OrderedDict
from contextlib import contextmanager
from os.path import dirname, exists, join
from uuid import uuid4
from flask import Flask, redirect, render_template, request, url_for
from imgurpython import ImgurClient
from PIL import Image, ImageDraw, ImageFont, ImageOps
logging.basicConfig()
app = Flask(__name__)
STATIC = join(app.root_path, 'static')
MASK = join(STATIC, 'img', 'mask.png')
FONTS_PATH = join(STATIC, 'fonts')
WHITE = '#ffffff'
BLACK = '#000000'
DEFAULT_FONT_SIZE = 36
DEFAULT_TEXT_X = 250
DEFAULT_TEXT_Y = 530
DEFAULT_OVERLAY_TEXT_X = 340
DEFAULT_OVERLAY_TEXT_Y = 20
FONTS = {
i: font for i, font in enumerate(os.listdir(FONTS_PATH))
if font.lower().endswith('ttf')
and 'glyph' not in font.lower()
}
FONTS_IDS = {
font: i for i, font in FONTS.items()
}
DEFAULT_FONT = 'SourceSansPro-Bold.ttf'
DEFAULT_FONT_ID = FONTS_IDS[DEFAULT_FONT]
class Mask:
def __init__(self, name, filename):
self.name = name
self.filename = filename
@property
def path(self):
return join(STATIC, 'img', 'masks', self.filename)
def __str__(self):
return self.name
MASKS = [
Mask('Paisagem', 'landscape.png'),
Mask('Meme - nome branco', 'meme-white.png'),
Mask('Meme - nome cinza', 'meme-gray.png'),
Mask('Meme - nome preto', 'meme-black.png'),
]
STATIC_HASHES = {}
class Credentials:
def __init__(self, service):
self.service = service
@classmethod
def for_imgur(cls):
return cls('imgur')
def credentials_folder(self):
path = join(dirname(__file__), 'credentials')
os.makedirs(path, exist_ok=True)
return path
def credentials_file(self):
return join(self.credentials_folder(), '{}.json'.format(self.service))
@property
def data(self):
with open(self.credentials_file()) as f:
data = json.load(f)
return data
@data.setter
def data(self, data):
with open(self.credentials_file(), 'w') as f:
json.dump(data, f)
def new_hash_for(path):
file_path = join(STATIC, path)
try:
with open(file_path, 'rb') as f:
m = hashlib.md5()
m.update(f.read())
hash_ = m.hexdigest()
except:
hash_ = uuid4()
url = '{}?_={}'.format(url_for('static', filename=path), hash_)
STATIC_HASHES[path] = url
return url
def hashed_static(path):
if path in STATIC_HASHES:
return STATIC_HASHES[path]
return new_hash_for(path)
def font_path(font_id):
font = FONTS[font_id]
return join(FONTS_PATH, font)
def generate(original_photo, context):
mask_id = context['image_kind']
mask = MASKS[mask_id]
mask = Image.open(mask.path)
photo = ImageOps.fit(original_photo, mask.size)
mask = mask.convert('RGBA')
photo = photo.convert('RGBA')
photo = process_image(context, photo)
composition = Image.alpha_composite(photo, mask)
draw_text(composition, context)
buffer = io.BytesIO()
composition.save(buffer, format='PNG')
image_bytes = base64.b64encode(buffer.getvalue())
return image_bytes.decode('ascii')
def process_image(context, photo):
if context['colorize'] or context['equalize']:
photo = photo.convert('L')
if context['equalize']:
photo = ImageOps.equalize(photo)
if context['colorize']:
photo = ImageOps.colorize(photo, context['black'], context['white'])
photo = photo.convert('RGBA')
return photo
def draw_text(composition, context):
draw = ImageDraw.Draw(composition)
main_font = ImageFont.truetype(
font_path(context['font_family']), context['font_size'])
draw.multiline_text(
(context['text_x'], context['text_y']),
context['title'], context['font_color'], main_font)
overlay_font = ImageFont.truetype(
font_path(context['overlay_font_family']),
context['overlay_font_size'])
draw.multiline_text(
(context['overlay_text_x'], context['overlay_text_y']),
context['overlay_title'], context['overlay_font_color'],
overlay_font)
def link_for_original(original_photo):
credentials = Credentials.for_imgur()
data = credentials.data
client = ImgurClient(
data['client_id'], data['client_secret'],
data['access_token'], data['refresh_token'],
)
with tempfile.NamedTemporaryFile() as f:
original_photo.save(f, format='PNG')
response = client.upload_from_path(f.name)
return response['link']
def generate_from_file(file, context):
original_photo = Image.open(file.stream)
try:
link = link_for_original(original_photo)
context['url'] = link
except Exception as e:
logging.exception(e)
return generate(original_photo, context)
def generate_from_url(url, context):
buffer = io.BytesIO()
stream = urllib.request.urlopen(context['url'])
original_photo = Image.open(stream)
return generate(original_photo, context)
def int_from(name, default=0):
try:
return int(request.form.get(name, str(default)).strip())
except:
return default
def clean_string(name, default=''):
return request.form.get(name, default).replace('\r', '')
def create_context():
context = {
'white': clean_string('white', WHITE),
'black': clean_string('black', BLACK),
'url': clean_string('url'),
'equalize': bool(request.form.get('equalize')),
'colorize': bool(request.form.get('colorize')),
'font_families': FONTS.items(),
'image_kinds': enumerate(MASKS),
'image_kind': int_from('image_kind', 0),
# Main title
'title': clean_string('title'),
'font_color': clean_string('font_color', BLACK),
'font_family': int_from('font_family', DEFAULT_FONT_ID),
'text_x': int_from('text_x', DEFAULT_TEXT_X),
'text_y': int_from('text_y', DEFAULT_TEXT_Y),
'font_size': int_from('font_size', DEFAULT_FONT_SIZE),
# Overlay title
'overlay_title': clean_string('overlay_title'),
'overlay_font_color': clean_string('overlay_font_color', WHITE),
'overlay_font_family': int_from(
'overlay_font_family', DEFAULT_FONT_ID),
'overlay_text_x': int_from('overlay_text_x', DEFAULT_OVERLAY_TEXT_X),
'overlay_text_y': int_from('overlay_text_y', DEFAULT_OVERLAY_TEXT_Y),
'overlay_font_size': int_from('overlay_font_size', DEFAULT_FONT_SIZE),
# Post text
'post_title': clean_string('post_title'),
'post_message': clean_string('post_message'),
}
return context
@app.route('/', methods=['POST', 'GET'])
def index():
context = create_context()
if request.method == 'POST':
file = request.files['file']
try:
if file.filename:
context['image'] = generate_from_file(file, context)
elif context['url']:
context['image'] = generate_from_url(context['url'], context)
except Exception as e:
logging.exception(e)
context['error'] = 'Não foi possível gerar a imagem'
else:
context['equalize'] = True
context['colorize'] = True
return render_template('application.html', **context)
@app.route('/imgur', methods=['POST', 'GET'])
def imgur():
fields = (
'client_id',
'client_secret',
'access_token',
'refresh_token',
)
context = OrderedDict()
for field in fields:
context[field] = request.form.get(field, '')
creds = Credentials.for_imgur()
if request.method == 'POST':
client_id = context['client_id']
client_secret = context['client_secret']
creds.data = context
if client_id and client_secret:
client = ImgurClient(client_id, client_secret)
authorization_url = client.get_auth_url('code')
return redirect(authorization_url)
elif 'code' in request.args:
data = creds.data
client_id = data['client_id']
client_secret = data['client_secret']
client = ImgurClient(client_id, client_secret)
code = request.args['code']
credentials = client.authorize(code, 'authorization_code')
client.set_user_auth(
credentials['access_token'], credentials['refresh_token'])
data.update(credentials)
context.update(data)
creds.data = data
return render_template(
'imgur.html', context=context, **context)
app.jinja_env.globals['hashed_static'] = hashed_static
if __name__ == '__main__':
app.run(debug=True)