diff --git a/DIYgod/0005/change_resolution.py b/DIYgod/0005/change_resolution.py new file mode 100644 index 00000000..5ae70b8f --- /dev/null +++ b/DIYgod/0005/change_resolution.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +from PIL import Image + +def change_resolution(picPath, reslution): + img = Image.open(picPath) + x, y = img.size + print x, y + changex = float(x) / reslution[0] + changey = float(y) / reslution[1] + + # 判断分辨率是否满足 + if changex > 1 or changey > 1: + change = changex if changex > changey else changey + print change + print int(reslution[0] / change), int(reslution[1] / change) + img.resize((int(x / change), int(y / change))).save('result.jpg') + +if __name__ == '__main__': + change_resolution('pictest.jpg', (1136, 640)) diff --git a/DIYgod/0005/pictest.jpg b/DIYgod/0005/pictest.jpg new file mode 100644 index 00000000..00349ac7 Binary files /dev/null and b/DIYgod/0005/pictest.jpg differ diff --git a/DIYgod/0005/result.jpg b/DIYgod/0005/result.jpg new file mode 100644 index 00000000..d04c0411 Binary files /dev/null and b/DIYgod/0005/result.jpg differ diff --git a/DIYgod/0006/important_word.py b/DIYgod/0006/important_word.py new file mode 100644 index 00000000..98d58796 --- /dev/null +++ b/DIYgod/0006/important_word.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +import re +import os + +# Get all files in designated path +def get_files(path): + filepath = os.listdir(path) + files = [] + for fp in filepath: + fppath = path + '/' + fp + if(os.path.isfile(fppath)): + files.append(fppath) + elif(os.path.isdir(fppath)): + files += get_files(fppath) + return files + +# Get the most popular word in designated files +def get_important_word(files): + worddict = {} + for filename in files: + f = open(filename, 'rb') + s = f.read() + words = re.findall(r'[a-zA-Z0-9]+', s) + for word in words: + worddict[word] = worddict[word] + 1 if word in worddict else 1 + wordsort = sorted(worddict.items(), key=lambda e:e[1], reverse=True) + return wordsort + +if __name__ == '__main__': + files = get_files('.') + print files + print get_important_word(files) diff --git a/DIYgod/0006/test/test.txt b/DIYgod/0006/test/test.txt new file mode 100644 index 00000000..a45f09a1 --- /dev/null +++ b/DIYgod/0006/test/test.txt @@ -0,0 +1,2 @@ +important important important important important important important important important important important important important important important important important important important +