File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments