-
Notifications
You must be signed in to change notification settings - Fork 1
/
createThumb.py
55 lines (47 loc) · 1.11 KB
/
createThumb.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年6月17日
@author: Irony
@site:
@email: 892768447@qq.com
@file:
@description: 生成缩略图
"""
import os
import sys
import traceback
import requests
from PIL import Image
try:
os.mkdir('tmp')
except Exception:
pass
def download(url, src, dst):
try:
resp = requests.get(url)
open(src, 'wb').write(resp.content)
thumbnail(src, dst)
except Exception:
traceback.print_exc()
def thumbnail(src, dst):
try:
img = Image.open(src)
img = img.convert('RGB')
img.thumbnail((80, 80), Image.ANTIALIAS)
img.save(dst)
except Exception:
traceback.print_exc()
if __name__ == '__main__':
length = len(sys.argv)
if length == 3:
# 本地文件生成缩略图
_, src, dst = sys.argv
thumbnail(src, dst)
elif length == 4:
# 远程文件生成缩略图
_, url, src, dst = sys.argv
if url.find('jsdelivr') > -1 or url.find('jsd.') > -1:
print('ignore cdn file')
else:
download(url, src, dst)