forked from gxercavins/image-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
200 lines (151 loc) · 5.4 KB
/
app.py
File metadata and controls
200 lines (151 loc) · 5.4 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
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
# web-app for API image manipulation
from flask import Flask, request, render_template, send_from_directory
import os
from PIL import Image
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
# default access page
@app.route("/")
def main():
return render_template('index.html')
# upload selected image and forward to processing page
@app.route("/upload", methods=["POST"])
def upload():
target = os.path.join(APP_ROOT, 'static/images/')
# create image directory if not found
if not os.path.isdir(target):
os.mkdir(target)
# retrieve file from html file-picker
upload = request.files.getlist("file")[0]
print("File name: {}".format(upload.filename))
filename = upload.filename
# file support verification
ext = os.path.splitext(filename)[1]
if (ext == ".jpg") or (ext == ".png") or (ext == ".bmp"):
print("File accepted")
else:
return render_template("error.html", message="The selected file is not supported"), 400
# save file
destination = "/".join([target, filename])
print("File saved to to:", destination)
upload.save(destination)
# forward to processing page
return render_template("processing.html", image_name=filename)
# rotate filename the specified degrees
@app.route("/rotate", methods=["POST"])
def rotate():
# retrieve parameters from html form
angle = request.form['angle']
filename = request.form['image']
# open and process image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
img = img.rotate(-1*int(angle))
# save and return image
destination = "/".join([target, 'temp.png'])
if os.path.isfile(destination):
os.remove(destination)
img.save(destination)
return send_image('temp.png')
# flip filename 'vertical' or 'horizontal'
@app.route("/flip", methods=["POST"])
def flip():
# retrieve parameters from html form
if 'horizontal' in request.form['mode']:
mode = 'horizontal'
elif 'vertical' in request.form['mode']:
mode = 'vertical'
else:
return render_template("error.html", message="Mode not supported (vertical - horizontal)"), 400
filename = request.form['image']
# open and process image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
if mode == 'horizontal':
img = img.transpose(Image.FLIP_LEFT_RIGHT)
else:
img = img.transpose(Image.FLIP_TOP_BOTTOM)
# save and return image
destination = "/".join([target, 'temp.png'])
if os.path.isfile(destination):
os.remove(destination)
img.save(destination)
return send_image('temp.png')
# crop filename from (x1,y1) to (x2,y2)
@app.route("/crop", methods=["POST"])
def crop():
# retrieve parameters from html form
x1 = int(request.form['x1'])
y1 = int(request.form['y1'])
x2 = int(request.form['x2'])
y2 = int(request.form['y2'])
filename = request.form['image']
# open image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
# check for valid crop parameters
width = img.size[0]
height = img.size[1]
crop_possible = True
if not 0 <= x1 < width:
crop_possible = False
if not 0 < x2 <= width:
crop_possible = False
if not 0 <= y1 < height:
crop_possible = False
if not 0 < y2 <= height:
crop_possible = False
if not x1 < x2:
crop_possible = False
if not y1 < y2:
crop_possible = False
# crop image and show
if crop_possible:
img = img.crop((x1, y1, x2, y2))
# save and return image
destination = "/".join([target, 'temp.png'])
if os.path.isfile(destination):
os.remove(destination)
img.save(destination)
return send_image('temp.png')
else:
return render_template("error.html", message="Crop dimensions not valid"), 400
return '', 204
# blend filename with stock photo and alpha parameter
@app.route("/blend", methods=["POST"])
def blend():
# retrieve parameters from html form
alpha = request.form['alpha']
filename1 = request.form['image']
# open images
target = os.path.join(APP_ROOT, 'static/images')
filename2 = 'blend.jpg'
destination1 = "/".join([target, filename1])
destination2 = "/".join([target, filename2])
img1 = Image.open(destination1)
img2 = Image.open(destination2)
# resize images to max dimensions
width = max(img1.size[0], img2.size[0])
height = max(img1.size[1], img2.size[1])
img1 = img1.resize((width, height), Image.ANTIALIAS)
img2 = img2.resize((width, height), Image.ANTIALIAS)
# if image in gray scale, convert stock image to monochrome
if len(img1.mode) < 3:
img2 = img2.convert('L')
# blend and show image
img = Image.blend(img1, img2, float(alpha)/100)
# save and return image
destination = "/".join([target, 'temp.png'])
if os.path.isfile(destination):
os.remove(destination)
img.save(destination)
return send_image('temp.png')
# retrieve file from 'static/images' directory
@app.route('/static/images/<filename>')
def send_image(filename):
return send_from_directory("static/images", filename)
if __name__ == "__main__":
app.run()