Skip to content

Commit f791f61

Browse files
committed
Merge pull request Show-Me-the-Code#86 from DIYgod/master
Complete 0005+
2 parents b9794ab + 2218028 commit f791f61

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

DIYgod/0005/change_resolution.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from PIL import Image
4+
5+
def change_resolution(picPath, reslution):
6+
img = Image.open(picPath)
7+
x, y = img.size
8+
print x, y
9+
changex = float(x) / reslution[0]
10+
changey = float(y) / reslution[1]
11+
12+
# 判断分辨率是否满足
13+
if changex > 1 or changey > 1:
14+
change = changex if changex > changey else changey
15+
print change
16+
print int(reslution[0] / change), int(reslution[1] / change)
17+
img.resize((int(x / change), int(y / change))).save('result.jpg')
18+
19+
if __name__ == '__main__':
20+
change_resolution('pictest.jpg', (1136, 640))

DIYgod/0005/pictest.jpg

784 KB
Loading

DIYgod/0005/result.jpg

39.7 KB
Loading

DIYgod/0006/important_word.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import re
4+
import os
5+
6+
# Get all files in designated path
7+
def get_files(path):
8+
filepath = os.listdir(path)
9+
files = []
10+
for fp in filepath:
11+
fppath = path + '/' + fp
12+
if(os.path.isfile(fppath)):
13+
files.append(fppath)
14+
elif(os.path.isdir(fppath)):
15+
files += get_files(fppath)
16+
return files
17+
18+
# Get the most popular word in designated files
19+
def get_important_word(files):
20+
worddict = {}
21+
for filename in files:
22+
f = open(filename, 'rb')
23+
s = f.read()
24+
words = re.findall(r'[a-zA-Z0-9]+', s)
25+
for word in words:
26+
worddict[word] = worddict[word] + 1 if word in worddict else 1
27+
wordsort = sorted(worddict.items(), key=lambda e:e[1], reverse=True)
28+
return wordsort
29+
30+
if __name__ == '__main__':
31+
files = get_files('.')
32+
print files
33+
print get_important_word(files)

DIYgod/0006/test/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
important important important important important important important important important important important important important important important important important important important
2+

0 commit comments

Comments
 (0)