forked from gxercavins/image-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
172 lines (128 loc) · 4.83 KB
/
core.py
File metadata and controls
172 lines (128 loc) · 4.83 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
# Image manipulation API
from flask import Flask, render_template, send_from_directory, redirect
import os
from PIL import Image
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
# default access redirects to API documentation
@app.route("/")
def main():
return redirect("https://github.com/gxercavins/image-api/blob/master/README.md", code=302)
# rotate filename the specified degrees
@app.route("/rotate/<angle>/<filename>", methods=["GET"])
def rotate(angle, filename):
# check for valid angle
angle = int(angle)
if not -360 < angle < 360:
return render_template("error.html", message="Invalid angle parameter (-359 to 359)"), 400
# open and process image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
img = img.rotate(-1*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/<mode>/<filename>", methods=["GET"])
def flip(mode, filename):
# open and process image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
# check mode
if mode == 'horizontal':
img = img.transpose(Image.FLIP_LEFT_RIGHT)
elif mode == 'vertical':
img = img.transpose(Image.FLIP_TOP_BOTTOM)
else:
return render_template("error.html", message="Invalid mode (vertical or horizontal)"), 400
# 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/<x1>/<y1>/<x2>/<y2>/<filename>", methods=["GET"])
def crop(x1, y1, x2, y2, filename):
# open image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
width = img.size[0]
height = img.size[1]
# check for valid crop parameters
[x1, y1, x2, y2] = [int(x1), int(y1), int(x2), int(y2)]
crop_possible = True
while True:
if not 0 <= x1 < width:
crop_possible = False
break
if not 0 < x2 <= width:
crop_possible = False
break
if not 0 <= y1 < height:
crop_possible = False
break
if not 0 < y2 <= height:
crop_possible = False
break
if not x1 < x2:
crop_possible = False
break
if not y1 < y2:
crop_possible = False
break
break
# process image
if crop_possible:
img = img.crop((x1, y1, x2, y2))
else:
return render_template("error.html", message="Crop dimensions not valid"), 400
# 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')
# blend filename1 and filename2 with alpha parameter
@app.route("/blend/<alpha>/<filename1>/<filename2>", methods=["GET"])
def blend(alpha, filename1, filename2):
# check for valid alpha
alpha = float(alpha)
if not 0 <= alpha <= 100:
return render_template("error.html", message="Invalid alpha value (0-100)"), 400
#open images
target = os.path.join(APP_ROOT, 'static/images')
destination1 = "/".join([target, filename1])
destination2 = "/".join([target, filename2])
img1 = Image.open(destination1)
img2 = Image.open(destination2)
# check for dimensions and resize to larger ones
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 one image in gray scale, convert the other to monochrome
if len(img1.mode) < 3:
img2 = img2.convert('L')
elif len(img2.mode) < 3:
img1 = img1.convert('L')
# blend images
img = Image.blend(img1, img2, float(alpha)/100)
# save and return
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()