Skip to content

Commit dc6988d

Browse files
committed
0005
1 parent 456ba7d commit dc6988d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

chris5641/0005/ChangeResolution.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
4+
"""
5+
from PIL import Image
6+
import os
7+
8+
__author__ = 'Chris5641'
9+
10+
11+
def change_resolution(path):
12+
for picname in os.listdir(path):
13+
picpath = os.path.join(path, picname)
14+
with Image.open(picpath) as im:
15+
w, h = im.size
16+
n = w/1366 if (w/1366) >= (h/640) else h/640
17+
im.thumbnail((w/n, h/n))
18+
im.save('finish_'+picname.split('.')[0]+'.jpg', 'jpeg')
19+
20+
21+
if __name__ == '__main__':
22+
change_resolution('/home/chris/pictures/123')

0 commit comments

Comments
 (0)