-
Notifications
You must be signed in to change notification settings - Fork 9
/
imgproc.py
76 lines (64 loc) · 1.86 KB
/
imgproc.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
import PIL
from PIL import Image
import io
def load_img_from_bin(b):
im = Image.open(io.BytesIO(b))
return im.convert(mode='RGBA')
def resize_to(im, target=72):
w,h = ow,oh = im.size
longer = max(w,h)
shorter = min(w,h)
# crop the thing to square
l = int((longer-shorter)/2+.5)
if longer==w:
nim = im.crop((l,0,w-l,h))
else:
nim = im.crop((0,l,w,h-l))
# resize to a smaller square
w,h = nim.size
ratio = target/w
nw,nh = round(w*ratio), round(w*ratio)
if nw == ow and nh==oh:
return im
return nim.resize((nw,nh), resample=Image.BICUBIC, reducing_gap=3.5)
def avatar_pipeline(b):
# user upload binary file
# convert it into avatar format
# output binary
im = load_img_from_bin(b)
im = resize_to(im, 96).quantize(
colors=64,
method=Image.FASTOCTREE, # only applicapable
kmeans=1, # larger faster
dither=Image.FLOYDSTEINBERG,
)
print(im.size)
result_file = io.BytesIO()
im.save(result_file,'PNG',optimize=True)
return result_file.getvalue()
if __name__ == '__main__':
import sys
if len(sys.argv)>1:
fn = sys.argv[1]
im = load_img_from_bin(open(fn,'rb').read())
im = im.quantize(
colors=128,
method=Image.FASTOCTREE, # only applicapable
kmeans=1, # larger faster
dither=Image.FLOYDSTEINBERG,
)
im.save(fn,'PNG',optimize=True)
print('saved to', fn)
else:
print('no args')
# b = open('templates/images/logo.png','rb').read()
# # b = b[:100]
# im = load_img_from_bin(b)
#
# print(im)
# # resize_to(im, 84).quantize(
# # colors=32,
# # method=Image.FASTOCTREE, # only applicapable
# # kmeans=1, # larger faster
# # dither=Image.FLOYDSTEINBERG,
# # ).save('test.png')