Skip to content

Commit 008d218

Browse files
authored
Create 0005.py
1 parent 7ec3e16 commit 008d218

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

pylyria/0005/0005.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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)

0 commit comments

Comments
 (0)