-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposter.py
More file actions
66 lines (52 loc) · 2.54 KB
/
poster.py
File metadata and controls
66 lines (52 loc) · 2.54 KB
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
import textwrap
import math
import boto3
import matplotlib
import io
import uuid
from flask_restful import abort, Resource, reqparse
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from env import S3_BUCKET
parser = reqparse.RequestParser()
parser.add_argument('text', required=True, help="You need to send me some text!")
parser.add_argument('bgcolor', required=False, help="Optionally, send a Hex colour for the background", default='#FFFFFF')
parser.add_argument('fgcolor', required=False, help="Optionally, send a Hex colour for the foreground (text)", default='#000000')
parser.add_argument('dpi', required=False, help="Optionally, specify the resolution of the image in DPI. (Size is around A4). If a list of values (e.g. [60, 300]) response is a list of URLs in this order.", default=60, type=int, action='append')
parser.add_argument('days_before_expire', required=False, help="Optionally, specify how many days the URL should be valid for.", default=30, type=int)
def make_poster(text, bgcolor='#FFFFFF', fgcolor='#000000', dpi=60, days=30):
words = text.split()
aspect_ratio = 1.414 # ISO 216
line_spacing = 1.5
chars_per_line = (aspect_ratio * line_spacing) * math.sqrt(len(text))
lines = textwrap.wrap(text, chars_per_line, break_long_words=False)
longest_line_chars = max([len(l) for l in lines])
font_size = int(10.5 * 72. / longest_line_chars) # Half inch padding. 72 pts/inch.
poster_text = '\n'.join(lines)
fig, ax= plt.subplots(1, 1, figsize=(11, 8))
ax.axvspan(0, 1, color=bgcolor)
ax.text(0.5, 0.5, poster_text, ha='center', va='center', fontsize=font_size, color=fgcolor, linespacing=line_spacing)
plt.axis('off')
poster_im = io.BytesIO()
plt.tight_layout()
plt.savefig(poster_im, format='png', dpi=dpi)
poster_im.seek(0)
s3 = boto3.client('s3')
s3key = '{uuid}.png'.format(uuid=str(uuid.uuid4()))
s3.upload_fileobj(poster_im, S3_BUCKET, s3key)
posterurl = s3.generate_presigned_url('get_object', Params={
'Bucket': S3_BUCKET, 'Key': s3key
}, ExpiresIn=(days * 24 * 60 * 60))
return posterurl
class PosterMaker(Resource):
def post(self):
args = parser.parse_args()
text = args.get('text')
bgcolor = args.get('bgcolor')
fgcolor = args.get('fgcolor')
dpi = args.get('dpi')
days = args.get('days_before_expire')
if type(dpi) is list:
return [make_poster(text, bgcolor, fgcolor, d, days) for d in dpi]
else:
return make_poster(text, bgcolor, fgcolor, dpi, days)