File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ #!/usr/bin/env python
3+ #第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
4+ import os
5+ from PIL import Image
6+
7+ def get_path (root = os .curdir ):
8+ root += os .sep
9+ for path , dirs , files in os .walk (root ):
10+ for file_name in files :
11+ yield path , file_name
12+
13+ def get_size (width , heigh , max_width , max_heigh ):
14+ if width > max_width :
15+ heigh = heigh * (max_width / width )
16+ width = max_width
17+ if heigh > max_heigh :
18+ width = width * (max_heigh / heigh )
19+ heigh = max_heigh
20+ return width , heigh
21+
22+
23+ if __name__ == '__main__' :
24+ paths = get_path ()
25+ image_format = ('.jpg' , '.jpeg' , 'png' , 'bmp' )
26+ max_size = (640 , 1136 )
27+ for path , file_name in paths :
28+ if file_name .endswith (image_format ):
29+ img = Image .open (path + os .sep + file_name )
30+ size = get_size (* (img .size + max_size ))
31+ if size != img .size :
32+ img .thumbnail (size )
33+ img .save (path + os .sep + 'thumbnailed_' + file_name )
You can’t perform that action at this time.
0 commit comments