-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumb.py
55 lines (50 loc) · 1.53 KB
/
thumb.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
# import requests as req
from PIL import Image
from io import BytesIO
# 制作缩略图
def make_thumb(filename, sizes=(128, 128)):
f = open(filename,'rb')
try:
im = Image.open(f)
except BaseException:
f.close()
return
mode = im.mode
if mode not in ('L', 'RGB'):
if mode == 'RGBA':
# 透明图片需要加白色底
alpha = im.split()[3]
bgmask = alpha.point(lambda x: 255 - x)
im = im.convert('RGB')
im.paste((255, 255, 255), None, bgmask)
else:
im = im.convert('RGB')
# 切成方图,避免变形
width, height = im.size
if width == height:
region = im
else:
if width > height:
# h*h
delta = (width - height) / 2
box = (delta, 0, delta + height, height)
else:
# w*w
delta = (height - width) / 2
box = (0, delta, width, delta + width)
region = im.crop(box)
# resize
thumb = region.resize((sizes[0], sizes[1]), Image.ANTIALIAS)
f.close()
#保存图片
thumb.save(filename.replace('upload','thumb'), quality=100)
from os import listdir
if __name__ == '__main__':
for p in ['convert/']:
for f in listdir('image/upload/'+p):
if f != 'README.md':
make_thumb('image/upload/'+p+f,sizes=(256,256))
for p in ['content/','style/']:
for f in listdir('image/upload/'+p):
if f != 'README.md':
make_thumb('image/upload/'+p+f)