-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
121 lines (104 loc) · 3.78 KB
/
app.py
File metadata and controls
121 lines (104 loc) · 3.78 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
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
from flask import Flask, render_template, request
from flickrapi import FlickrAPI
import urllib.request
import os, shutil, datetime
import textwrap
from PIL import Image, ImageDraw, ImageFont
app = Flask(__name__)
FLICKR_PUBLIC = '58828f5f5d114133ebc1d053cca2028c'
FLICKR_SECRET = '6ab6621d8cc13c54'
@app.route('/')
def index():
# remove static/images folder
if os.path.exists("static/images"):
shutil.rmtree("static/images")
# re-create static/images folder
os.makedirs("static/images")
return render_template("index.html")
@app.route('/picture')
def picture():
# use GET to obtain search keyword and page number (if available)
keyword = request.args.get('keyword')
pageNo = (request.args.get('page') or 1)
# output images that match keyword
flickr = FlickrAPI(FLICKR_PUBLIC, FLICKR_SECRET, format='parsed-json')
# fetches data using Flickr API
fetchData = flickr.photos.search(text=keyword, sort='relevance',
safe_search=1, content_type=1, page=pageNo,
per_page=10, extras='url_q')
photos = fetchData['photos']['photo']
data = {
"keyword": keyword,
"photos": photos,
"page": str(pageNo)
}
return render_template("picture.html", **data)
@app.route('/text')
def text():
# use GET to obtain photo id and keyword
photoID = request.args.get('photoID')
keyword = request.args.get('keyword')
# use Flickr API to obtain image static url
flickr = FlickrAPI(FLICKR_PUBLIC, FLICKR_SECRET, format='parsed-json')
fetchData = flickr.photos.getSizes(photo_id=photoID)
# '6' is the Medium photo option
sourceURL = fetchData["sizes"]["size"][6]["source"]
# use timeStamp to prevent caching
timeStamp = str(datetime.datetime.now().microsecond)
path = 'static/images/' + photoID + timeStamp + '.jpg'
# download image
urllib.request.urlretrieve(sourceURL, path)
# find out size of image and limit text input length
image = Image.open(path)
width, height = image.size
limit = int(width/45)
data = {
"photoID": photoID,
"keyword": keyword,
"path": path,
"timeStamp": timeStamp,
"limit": limit
}
return render_template("text.html", **data)
@app.route('/result')
def result():
# use GET to obtain photoID, keyword and timeStamp
photoID = request.args.get('photoID')
keyword = request.args.get('keyword')
timeStamp = request.args.get('timeStamp')
# use GET to meme text and text settings
text = request.args.get('text')
textColor = request.args.get('color')
textPosition = request.args.get('position')
# add text to the image using python pillow module
path = 'static/images/' + photoID + timeStamp + '.jpg'
image = Image.open(path)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('static/Anton-Regular.ttf', size=45)
width, height = image.size
w, h = draw.textsize(text, font=font)
limit = int(width/30)
# text position settings, default to position of top
(x, y) = ((width / 2), 25)
if textPosition == "middle":
y = (height / 2) - (h/2)
elif textPosition == "bottom":
additionalOffset = w / width * h
y = height - h - additionalOffset - 25
# wrap text
for line in textwrap.wrap(text, width=limit):
x = (width / 2) - (font.getsize(line)[0]/2 )
draw.text((x, y), line, font=font, fill=textColor)
y += font.getsize(line)[1]
# converts image to 'RGB' format and saves it
image = image.convert('RGB')
path = 'static/images/' + photoID + timeStamp + 'edited.jpg'
image.save(path)
data = {
"path": path,
"keyword": keyword,
"photoID": photoID
}
return render_template("result.html", **data)
if __name__ == '__main__':
app.run(debug=True)