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+ # 第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
3+ import linecache
4+
5+ file = open ('source/change_images_size.py' , 'r' )
6+
7+ fileLines = len (file .readlines ())
8+
9+ print ('代码行数:%s' % fileLines )
10+
11+ blankLines = 0
12+
13+ for line in range (fileLines ):
14+ if len ((linecache .getline ('source/change_images_size.py' , line ).split ())) == 0 :
15+ blankLines += 1
16+
17+ print (blankLines )
Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ # 第 0005 题: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
3+ import os
4+
5+ from PIL import Image
6+
7+ rootDir = 'imgs/'
8+
9+
10+ def judge_size (img ):
11+ imgSize = img .size
12+ return max (imgSize ) > 1136
13+
14+
15+ def return_size (img ):
16+ max , min = img .size
17+ if max > min :
18+ return max , min
19+ else :
20+ return min , max
21+
22+
23+ def change_size (img , max , min ):
24+ value = max / 1136
25+ min = int (min / value )
26+ newImg = img .resize ((1136 , min ), Image .ANTIALIAS )
27+ return newImg
28+
29+
30+ for parent , dirnames , filenames in os .walk (rootDir ):
31+ for filename in filenames :
32+ fName = filename
33+ print (filename )
34+ filename = parent + os .sep + filename
35+ fPostfix = os .path .splitext (filename )[1 ]
36+ try :
37+ img = Image .open (filename )
38+ except :
39+ print (filename )
40+ continue
41+ # img.load()
42+ print (filename )
43+ print (fPostfix )
44+ if (fPostfix == '.jpg' or fPostfix == '.png' or fPostfix == '.JPG' or fPostfix != '.PNG' ):
45+ print (judge_size (img ))
46+ if (judge_size (img ) == True ):
47+ max , min = return_size (img )
48+ newimg = change_size (img , max , min )
49+ newimg .save (rootDir + os .sep + fName )
You can’t perform that action at this time.
0 commit comments