-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumb.py
50 lines (40 loc) · 1.25 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
# /usr/bin/python3
import os
import glob
from PIL import Image
from os import walk
# 作用 把 data 下面的的 图片文件夹里的图片
# 1. png-》jpg
# 2. 剪切成 790宽,高按比例来
WIDTH = 790
IN_PATH = 'data/'
OUT_PATH = 'out/'
def execute():
for (dir, dirs, fileNames) in walk(IN_PATH):
for dirName in dirs:
pathIn = IN_PATH + dirName + '/'
pathOut = pathIn.replace(IN_PATH, OUT_PATH)
if not os.path.exists(pathOut):
os.makedirs(pathOut)
types = ('*.png', '*.jpg')
for type in types:
filename = os.path.join(pathIn, type)
a = glob.glob(filename)
for path in a:
name = os.path.join(path)
im = Image.open(name)
im = convert_to_jpg(im)
im = thumb(im)
im.save(os.path.splitext(name.replace(IN_PATH, OUT_PATH))[0] + '.jpg', 'JPEG')
print(path, 'Done!')
def convert_to_jpg(im):
im = im.convert('RGB')
return im
def thumb(im):
width = im.size[0]
height = im.size[1]
radio = WIDTH / width
im.thumbnail((WIDTH, height * radio))
return im
if __name__ == '__main__':
execute()