python练习册 每天一个小程序 第0005题
1 # -*-coding:utf-8-*-
2 __author__ = 'Deen'
3 '''
4 题目说明: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
5
6 思路: 先获取该目录下所有图片的绝对路径,再一个一个打开,resiz改变大小保存
7 '''
8
9 from PIL import Image
10 import os
11
12
13 # 获取目录下所有图片的绝对路径
14 def list_files(dir, wirldcard, recursion):
15 files_text = list()
16 exts = wirldcard.split(" ")
17 files = os.listdir(dir)
18 for name in files:
19 fullname = os.path.join(dir, name)
20 if (os.path.isdir(fullname) & recursion):
21 list_files(fullname, wirldcard, recursion)
22 else:
23 for ext in exts:
24 if (name.endswith(ext)):
25 files_text.append(fullname)
26 break
27 # print files_text
28 return files_text
29
30
31 def images_resize(imgs, width, height):
32 n = 0
33 for img in imgs:
34 n += 1
35 image = Image.open(img)
36 out = image.resize((width, height), Image.ANTIALIAS)
37 out.save(str(n) + '.jpg', 'jpeg')
38
39
40 if __name__ == '__main__':
41 dir = "E:\\images"
42 wildcard = ".jpg .png"
43 images_resize(list_files(dir, wildcard, 1), 500, 500)
44
45 '''
46 参考代码:
47 import os
48
49 from PIL import Image
50
51 def resize_image(image):
52 im = Image.open(image)
53 width, height = im.size
54 if height > 1136 or width > 640:
55 th = height / 1136
56 td = width / 640
57 ts = max(th, td)
58 nh = int(height / ts)
59 nw = int(width / ts)
60 im = im.resize((nw, nh))
61 im.save(image)
62 print('Successfully resized %s. New width is %i, new height is %i.' % (image, nh, nw))
63 else:
64 print("There's no need to resize %s." % image)
65
66 def main():
67 for i in os.listdir():
68 try:
69 resize_image(i)
70 except IOError:
71 print("Oops! %s is not supported to make the change!" % i)
72
73 if __name__ == '__main__':
74 main()
75
76 '''
python练习册 每天一个小程序 第0005题的更多相关文章
- python练习册 每天一个小程序 第0013题
# -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/21 ...
- python练习册 每天一个小程序 第0001题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生 ...
- python练习册 每天一个小程序 第0007题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但 ...
- python练习册 每天一个小程序 第0000题
PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __au ...
- python练习册 每天一个小程序 第0010题
# -*-coding:utf-8-*- ''' 题目描述: 使用 Python 生成类似于下图中的字母验证码图片 思路: 运用PIL库加random 随机字母进行生成 ''' import rand ...
- python练习册 每天一个小程序 第0009题
1 ''' 2 题目描述: 3 找出一个html文件中所有的url 4 5 思路 : 6 利用正则表达式进行匹配 7 8 ''' 9 10 11 import re 12 13 14 with ope ...
- python练习册 每天一个小程序 第0008题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 一个HTML文件,找出里面的正文. 6 7 思路: 8 利用Beautiful ...
- python练习册 每天一个小程序 第0006题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...
- python练习册 每天一个小程序 第0012题
# -*-coding:utf-8-*- def test(content): text = content flag = 0 with open('filtered_words.txt') as f ...
随机推荐
- python数据类型内置方法
内容概要 列表内置方法 字典内置方法 字符串转换成字典的方法 eval() 元组内置方法 元组相关笔试题 集合内置方法 列表内置方法 l1 = [2, 4, 5, 7, 3, 9, 0, 6] # 升 ...
- Solution -「LOCAL」二进制的世界
\(\mathcal{Description}\) OurOJ. 给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...
- CoRR 2015 | MXNet: A Flexible and Efficient Machine Learning Library for Heterogeneous Distributed Systems
MXNet是一个支持多种编程语言的机器学习库,使用MXNet可以方便地实现机器学习算法,尤其是深度神经网络.通过嵌入在宿主语言中,它将声明式符号表达与命令式张量计算相结合.它提供自动求导以计算梯度.M ...
- react 也就这么回事 01 —— React 元素的创建和渲染
React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...
- Python中random模块的用法案例
1 import random # 调用random模块 2 3 a = random.random() # 随机从0-1之间抽取一个小数 4 print(a) 5 6 a = random.rand ...
- git使用小技巧-忽略提交文件设置
前言 我们可以把自己的代码放到github上,但是我们有的文件或者文件夹不想提交到github上,这时候用到一个忽略文件 操作方法 * 在项目根目录创建一个 .gitignore文件 * 打开.git ...
- 解决方案:ipv4地址手动设置之后关掉推出再打开就没了(静态Ip设置好之后又自动变动态IP)
解决方案:ipv4地址手动设置之后关掉推出再打开就没了(静态Ip设置好之后又自动变动态IP) 1.情况说明:修改好IP,关掉窗口后,又变成 自动获取IP (如图二) 2.解决方案: 1)调出 服务和应 ...
- Wireshark教程之界面介绍
实验目的 1.工具介绍 2.主要应用 实验原理 1.网络管理员用来解决网络问题 2.网络安全工程师用来检测安全隐患 3.开发人员用来测试执行情况 4.学习网络协议 实验内容 1.菜单栏选项介绍 2.快 ...
- 2021年BI软件系统推荐,知名商业智能厂商品牌
国内外一直有一些厉害的商业智能厂商,在国外,例如国外微软的PowerBI.在国外是商业智能的行业领导者,在国外的市场占有率上远远领先其它产品,然而在中国市场却落后于国内商业智能厂商思迈特软件的Smar ...
- git问题:gpg failed to sign the data fatal: failed to write commit object问题
今天用版本控制工具git提交时一直出现的问题:gpg failed to sign the data fatal: failed to write commit object, gpg是一个加密软件 ...