Skip to content

Commit bb77cb4

Browse files
committed
Merge pull request Show-Me-the-Code#80 from endersodium/master
Finished 0005
2 parents 24d2b6f + c4ac03b commit bb77cb4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

endersodium/0005/modify.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# coding:utf-8
2+
# Python Requirement:3
3+
# Made by EnderSodium ender@enderself.co
4+
# 第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
5+
# Require to put them in the same directory.
6+
# iPhone5 Resoulution:1136x640
7+
# Using PIL Library
8+
9+
import os
10+
11+
from PIL import Image
12+
13+
def resize_image(image):
14+
im = Image.open(image)
15+
width, height = im.size
16+
if height > 1136 or width > 640:
17+
th = height / 1136
18+
td = width / 640
19+
ts = max(th, td)
20+
nh = int(height / ts)
21+
nw = int(width / ts)
22+
im = im.resize((nw, nh))
23+
im.save(image)
24+
print('Successfully resized %s. New width is %i, new height is %i.' % (image, nh, nw))
25+
else:
26+
print("There's no need to resize %s." % image)
27+
28+
def main():
29+
for i in os.listdir():
30+
try:
31+
resize_image(i)
32+
except IOError:
33+
print("Oops! %s is not supported to make the change!" % i)
34+
35+
if __name__ == '__main__':
36+
main()

0 commit comments

Comments
 (0)