-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
289 lines (245 loc) · 10.8 KB
/
main.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
### Online Photo Editor ###
# Author: Arda Altınörs, Yeditepe University
# Contact: arda@altinors.com | arda.altinors@std.yeditepe.edu.tr
# Social: https://github.com/ardaaltinors
# Description: This project lets the user edit photos by web based GUI.
# User Guide: Paste image URLs in the input boxes and use buttons to edit the image.
import os
import time
try:
from flask.views import MethodView
from flask import Flask, render_template, request
from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps, ImageFont, ImageDraw
import requests
except:
print("There are missing packages!")
os.system("pip3 install -r requirements.txt")
from flask.views import MethodView
from flask import Flask, render_template, request
from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps, ImageFont, ImageDraw
import requests
app = Flask(__name__)
headers = {
'pragma': 'no-cache',
'cache-control': 'no-cache',
'dnt': '1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
}
class HomePage(MethodView):
def get(self):
return render_template("index.html")
def post(self):
path = request.form.get("path")
global user_image
user_image = (ProcessImage(path))
user_image.save()
global user_image_path
user_image_path = f"{current_time}.png"
global initial_image_width
initial_image_width = user_image.width
global initial_image_height
initial_image_height = user_image.height
return render_template("edit.html",
initial_image=user_image_path,
initial_width=initial_image_width,
initial_height=initial_image_height)
class EditPage(MethodView):
def get(self):
return render_template("edit.html")
def post(self):
# Receive add text values.
insert_text = False
try:
text = request.form.get("text")
if text != "":
text_size = int(request.form.get("text_size"))
text_color = str(request.form.get("text_color"))
text_x = int((int(request.form.get("text_x")) / 100) * user_image.width)
text_y = int((int(request.form.get("text_y")) / 100) * user_image.height)
insert_text = True
except:
pass
####
# Receive gradient valuse.
insert_gradient = False
gradient_color = str(request.form.get("gradient_color"))
if gradient_color != "none":
gradient_magnitude = float(request.form.get("gradient_magnitude"))
insert_gradient = True
####
# Receive filter values
add_filter = False
user_filter = str(request.form.get("user_filter"))
if user_filter != "none":
add_filter = True
# receive cropping values
crop_image = False
checkbox_crop = request.form.get("opt-crop")
if checkbox_crop == "crop":
crop_image = True
crop_left = int((int(request.form.get("crop_left")) / 100) * user_image.width)
crop_top = int((int(request.form.get("crop_top")) / 100) * user_image.height)
crop_right = int((int(request.form.get("crop_right")) / 100) * user_image.width)
crop_bottom = int((int(request.form.get("crop_bottom")) / 100) * user_image.height)
# Receive add image values.
insert_image = False
try:
new_image_link = request.form.get("new_image_url")
if new_image_link != "":
new_image_x = int((int(request.form.get("new_image_x")) / 100) * user_image.width)
new_image_y = int((int(request.form.get("new_image_y")) / 100) * user_image.height)
shrink_perc = int(request.form.get("shrink_percentage"))
insert_image = True
except:
pass
####
# Receive rotation values.
user_degree = int(request.form.get("user_degree"))
main_user_degree = int(request.form.get("main_user_degree"))
# Receive enhancement values.
contrast_value = float(request.form.get("contrast_value"))
print("Contrast:", contrast_value)
color_value = float(request.form.get("color_value"))
print("Color:", color_value)
sharpness_value = float(request.form.get("sharpness_value"))
print("Sharpness:", sharpness_value)
user_image.modify_contrast(contrast_value=contrast_value)
user_image.modify_color(color_value=color_value)
user_image.modify_sharpness(sharpness_value=sharpness_value)
# Checks the used filters and applies them before saving the image.
if insert_image == True:
user_image.add_image(new_image_url=new_image_link, x=new_image_x, y=new_image_y, shrink_perc=shrink_perc, user_degree=user_degree)
if insert_text == True:
user_image.add_text(x=text_x, y=text_y, text_input=text, text_size=text_size, text_color=text_color)
if insert_gradient == True:
user_image.add_gradient(gradient_magnitude=gradient_magnitude, color=gradient_color)
if main_user_degree != 0:
user_image.rotate_image(degree=main_user_degree)
if crop_image == True:
user_image.crop_image(left=crop_left, top=crop_top, right=crop_right, bottom=crop_bottom)
if add_filter == True:
user_image.add_filter(filter=user_filter)
# Generates a new image.
user_image.save()
final_user_image_path = f"{current_time}.png"
return render_template("edit.html",
initial_image=user_image_path,
result=True,
final_image=final_user_image_path,
initial_width=initial_image_width,
initial_height=initial_image_height)
class ProcessImage:
def __init__(self, img_path):
self.img_path = img_path
try:
if img_path == "random":
img_path = "https://picsum.photos/1100/500"
self.image = Image.open(requests.get(img_path, stream=True, headers=headers).raw)
else:
self.image = Image.open(requests.get(img_path, stream=True, headers=headers).raw)
except:
print("Görsel hatası.")
print("Mevcut konum:", os.getcwd())
self.image = Image.open("static/404.jpg")
self.width, self.height = self.image.size
def add_text(self, x=10, y=10, text_input="", text_size="24", text_color="red"):
self.x = x
self.y = y
self.text = text_input
self.text_size = text_size
self.text_color = text_color
try:
font = ImageFont.truetype("static/arial.ttf", size=text_size)
except:
font = ImageFont.load_default()
draw = ImageDraw.Draw(self.image)
draw.text((x, y), text_input, fill=text_color, font=font, align="right")
def add_gradient(self, gradient_magnitude=1, color="black"):
self.gradient_magnitude = gradient_magnitude
self.color = color
if color=="none":
pass
else:
colors = {"black":0,"red":1500,"yellow":100000,"orange":150000}
color = colors[color]
im = self.image
if im.mode != 'RGBA':
im = im.convert('RGBA')
width, height = im.size
gradient = Image.new('L', (width, 1), color=0xFF)
for x in range(width):
gradient.putpixel((x, 0), int(255 * (1 - gradient_magnitude * float(x) / width)))
alpha = gradient.resize(im.size)
black_im = Image.new('RGBA', (width, height), color=color)
black_im.putalpha(alpha)
gradient_im = Image.alpha_composite(im, black_im)
self.image = gradient_im
def add_image(self, new_image_url, x=10, y=10, shrink_perc=80, user_degree=0):
self.new_image_url = new_image_url
self.x = x
self.y = y
self.shrink_perc = shrink_perc
self.user_degree = user_degree
shrink_perc = shrink_perc / 100
try:
new_image = Image.open(requests.get(new_image_url, stream=True, headers=headers).raw)
except:
new_image = Image.open("static/404.jpg")
if user_degree != 0:
new_image = new_image.rotate(-1*user_degree, expand=1)
new_image.thumbnail((new_image.width * shrink_perc, new_image.width * shrink_perc))
position = (x, y)
try:
self.image.paste(new_image, position, new_image)
except:
print("Error loading the new image.")
def add_filter(self, filter):
self.filter = filter
if filter == "grayscale":
self.image.convert("CMYK")
self.image = self.image.convert("L")
if filter == "blur":
self.image = self.image.filter(ImageFilter.BoxBlur(2))
def modify_contrast(self, contrast_value):
self.contrast_value = contrast_value
contrast = ImageEnhance.Contrast(self.image)
self.image = contrast.enhance(contrast_value)
def modify_color(self, color_value):
self.color_value = color_value
color = ImageEnhance.Color(self.image)
self.image = color.enhance(color_value)
def modify_sharpness(self, sharpness_value):
self.sharpness_value = sharpness_value
sharpness = ImageEnhance.Sharpness(self.image)
self.image = sharpness.enhance(sharpness_value)
def rotate_image(self, degree):
self.degree = degree
rotated_img = self.image.rotate(-1*degree, expand=1)
self.image = rotated_img
def crop_image(self, left, top, right, bottom):
self.left = left
self.top = top
self.right = right
self.bottom = bottom
try:
cropped_image = self.image.crop((left, top, right, bottom))
self.image = cropped_image
except:
print("Error cropping the image.")
def save(self):
get_time = time.localtime()
global current_time
current_time = time.strftime("%y-%m-%d--%H-%M-%S", get_time)
os.chdir("static")
os.chdir("img")
global edited_img
edited_img = self.image.save(f"{current_time}.png")
os.chdir("..")
os.chdir("..")
app.add_url_rule("/", view_func=HomePage.as_view("home_page"))
app.add_url_rule("/edit", view_func=EditPage.as_view("edit_page"))
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)