Skip to content

Commit 36cb92f

Browse files
committed
add 0022 and fix 0005
1 parent 86758d2 commit 36cb92f

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎Jaccorot/0005/0005.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212

1313
def resize_iPhone5_pic(path, new_path, width=iPhone5_WIDTH, height=iPhone5_HEIGHT):
1414
im = Image.open(path)
15-
im_resized = im.resize((width,height), Image.ANTIALIAS)
15+
w,h = im.size
16+
17+
if w > width:
18+
h = width * h // w
19+
w = width
20+
if h > height:
21+
w = height * w // h
22+
h = height
23+
24+
im_resized = im.resize((w,h), Image.ANTIALIAS)
1625
im_resized.save(new_path)
1726

1827

19-
def walk_dir(path):
28+
def walk_dir_and_resize(path):
2029
for root, dirs, files in os.walk(path):
2130
for f_name in files:
2231
if f_name.lower().endswith('jpg'):
@@ -25,4 +34,4 @@ def walk_dir(path):
2534
resize_iPhone5_pic(path=path_dst, new_path=f_new_name)
2635

2736
if __name__ == '__main__':
28-
walk_dir('./')
37+
walk_dir_and_resize('./')

‎Jaccorot/0022/0.jpg‎

26.1 KB
Loading

‎Jaccorot/0022/0022.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/local/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0022 题: iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用。
6+
"""
7+
import os
8+
from PIL import Image
9+
10+
PHONE = {'iPhone5':(1136,640), 'iPhone6':(1134,750), 'iPhone6P':(2208,1242)}
11+
12+
13+
def resize_pic(path, new_path, phone_type):
14+
im = Image.open(path)
15+
w,h = im.size
16+
17+
width,height = PHONE[phone_type]
18+
19+
if w > width:
20+
h = width * h // w
21+
w = width
22+
if h > height:
23+
w = height * w // h
24+
h = height
25+
26+
im_resized = im.resize((w,h), Image.ANTIALIAS)
27+
im_resized.save(new_path)
28+
29+
30+
def walk_dir_and_resize(path, phone_type):
31+
for root, dirs, files in os.walk(path):
32+
for f_name in files:
33+
if f_name.lower().endswith('jpg'):
34+
path_dst = os.path.join(root,f_name)
35+
f_new_name = phone_type + '_' + f_name
36+
resize_pic(path=path_dst, new_path=f_new_name , phone_type=phone_type)
37+
38+
39+
if __name__ == '__main__':
40+
walk_dir_and_resize('./', 'iPhone6')

0 commit comments

Comments
 (0)