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 ...
随机推荐
- Solution -「LOCAL」模板
\(\mathcal{Description}\) OurOJ. 给定一棵 \(n\) 个结点树,\(1\) 为根,每个 \(u\) 结点有容量 \(k_u\).\(m\) 次操作,每次操作 ...
- nginx域名转发
场景1:因服务器限制,所以只对外开放了一个端口,但是需要请求不同的外网环境,所以在中转服务器上用nginx做了一次转发 实现: server { listen 8051; server_name lo ...
- vue3-hash-calendar,一款基于vue3.x开发的移动端日期时间选择组件
在大家的催更下,鸽了一天又一天,vue3-hash-calendar 终于在今天诞生了. 按照惯例,先上效果图 Demo 扫描上方二维码或者请用浏览器的手机模式查看:https://www.hxkj. ...
- NSSCTF-easyupload1.0
2.0和3.0已经说过,这个1.0上传也可以使用那两个题目的做法,就是使用htaccess的方式进行定义上传文件的类型来进行连接shell,1.0肯定是没有2.0和3.0难的,所以说,也不要将这个题目 ...
- sql注入之报错注入and boolean注入
1.sql注入之报错注入 正常传参,返回页面正常: 加入' 返回页面报错,出现"zhangsan"' 报错注入使用的函数 在这里我们使用 select updatexml(1,c ...
- RENIX发送固定个数报文——网络测试仪实操
在使用RENIX软件时,有时候我们需要发送固定个数报文,那么该如何操作呢?以下为您讲解具体操作步骤. 第一步:预约测试资源 打开Renix软件,连接机箱, 预约端口 第二步:发送固定个数的报文 选中流 ...
- React 函数组件中对window添加事件监听resize导致回调不能获得Hooks最新状态的问题解决思路
React 函数组件中对window添加事件监听resize导致回调不能获得Hooks最新状态的问题解决思路 这几天在忙着把自己做的项目中的类组件转化为功能相同的函数组件,首先先贴一份该组件类组件的关 ...
- 浅谈bi工具的含义和不同类型
什么是BI工具? 商业智能(BI)工具是利用一组方法和技术来准备,呈现和帮助分析数据的工具.通过此过程,数据将转化为可操作的业务信息,帮助决策者和最终用户做出更有效的数据驱动决策. 商业智能使用的一 ...
- HDFS的上传下载流程
hdfs上传流程 首先客户端向nn请求上传文件.nn经过检查回应客户端是否可以上传.客户端得到同意后向nn请求上传第一块文件的dn.nn返回给客户端dn的地址.客户端与其中一个dn1建立连接然后dn1 ...
- parquet和orc选型以及压缩格式
Hive表压缩功能 除了直接配置MapReduce压缩功能外,Hive的ORC表和Parquet表直接支持表的压缩属性. 但支持的压缩格式有限,ORC表支持None.Zlib.Snappy压缩,默认为 ...