-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
151 lines (112 loc) · 3.93 KB
/
hello.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
# -*- coding: utf-8 -*-
from __future__ import with_statement
import sys
import cgi
import os
import random
import math
import datetime
import uuid
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageEnhance
from PIL import ImageOps, ImageChops
from PIL.ImageColor import getrgb
from flask import Flask, request, send_file, send_from_directory
app = Flask(__name__)
#------------------------------------- Supporting functions
def getFileDimensions(inputPath):
wts = Image.open(inputPath)
wts.load()
height = wts.size[1]
width = wts.size[0]
return (width, height)
def safetyCheck(*args):
lst = ()
if len(args) == 3:
lst = (args[0], args[1], args[2])
elif len(args) == 4:
lst = (args[0], args[1], args[2], args[3])
elif len(args) == 1:
lst = args[0]
else:
raise TypeError('Either parameter 1 should be a color tuple, or 3 or 4 parameters (r,g,b,a optional) are expected.')
for iSC in range(len(lst)):
if lst[iSC] < 0:
lst = replace_at_index(lst, iSC, 0)
elif lst[iSC] > 255:
lst = replace_at_index(lst, iSC, lst[iSC] % 255)
return lst
def safetyCheck_LeaveAtMax(*args):
lst = ()
if len(args) == 3:
lst = (args[0], args[1], args[2])
elif len(args) == 4:
lst = (args[0], args[1], args[2], args[3])
elif len(args) == 1:
lst = args[0]
else:
raise TypeError('Either parameter 1 should be a color tuple, or 3 or 4 parameters (r,g,b,a optional) are expected.')
for iSC in range(len(lst)):
if lst[iSC] < 0:
lst = replace_at_index(lst, iSC, 0)
elif lst[iSC] > 255:
lst = replace_at_index(lst, iSC, 255)
return lst
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def lum(r,g,b,a=0):
return math.sqrt( .241 * r + .691 * g + .068 * b )
def myhsv_to_rgb(hsv):
p = colorsys.hsv_to_rgb(hsv[0], hsv[1], hsv[2])
p = (int(p[0] * 255.0), int(p[1] * 255.0), int(p[2] * 255.0))
return p
def sort_by_lum(choices):
choices.sort(key=lambda rgb: lum(*rgb))
return choices
def getInverse(c):
inverse = (255-c[0],255-c[1],255-c[2])
return inverse
def resizeToMinMax(img, maxW, maxH, minW, minH):
if img.size[0] > maxW or img.size[1] > maxH or img.size[0] < minW or img.size[1] < minH:
(w, h) = getSizeByMinMax(img.size[0], img.size[1], maxW, maxH, minW, minH)
img = img.resize((int(w), int(h)), Image.ANTIALIAS)
return img
def resizeToMax(img, maxW, maxH):
if img.size[0] > maxW or img.size[1] > maxH:
(w, h) = getSizeByMax(img.size[0], img.size[1], maxW, maxH)
img = img.resize((int(w), int(h)), Image.ANTIALIAS)
return img
def resizeToTarget(img, targetImg):
img = img.resize((targetImg.size[0], targetImg.size[1]), Image.ANTIALIAS)
return img
def getSizeByMinMax(w, h, maxW, maxH, minW, minH):
resizedW = w
resizedH = h
r = (h * 1.0) / w
while resizedW > maxW or resizedH > maxH:
resizedW -= 1.0
resizedH = r * resizedW
while resizedW < minW or resizedH < minH:
resizedW += 1.0
resizedH = r * resizedW
return (resizedW, resizedH)
def getSizeByMax(w, h, maxW, maxH):
# resize the image down to <= the original size
resizedW = w
resizedH = h
r = (h * 1.0) / w
while resizedW > maxW or resizedH > maxH:
resizedW -= 1.0
resizedH = r * resizedW
return (resizedW, resizedH)
@app.route('/tdl')
def img():
return root()
@app.route('/')
def root():
return "<html><body>hello world</body></html>"
if __name__ == "__main__":
app.run(debug=True)