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 ...
随机推荐
- mysql对属性的增删改
修改表 alter table 创建表db 查看表 desc与describe desc table 查看建表语句show create table t1; 修改表名 alter table t1 r ...
- RISC-V 特权指令结构
机器模式 机器模式(缩写为 M 模式,M-mode)是 RISC-V 中 hart(hardware thread,硬件线 程)可以执行的最高权限模式.在 M 模式下运行的 hart 对内存,I/O ...
- 利用shell脚本使用kubeadm部署kubenetes 1.18.6集群环境
# README # 此脚本需要在master节点上使用 # 注意root密码,请提前修改 # 个人实验环境,注意机器最低配置:master(2G内存,1cpu2核心,否则集群会创建失败),node( ...
- 浅谈归并排序:合并 K 个升序链表的归并解法
在面试中遇到了这道题:如何实现多个升序链表的合并.这是 LeetCode 上的一道原题,题目具体如下: 用归并实现合并 K 个升序链表 LeetCode 23. 合并K个升序链表 给你一个链表数组,每 ...
- 生成式AI会成为是人工智能的未来吗
生成式 AI 是一项创新技术,可帮助算法人员生成以前依赖于业务员的模型,提供创造性的结果,而不会因业务员思想和经验而产生任何差错. 人工智能中的这项新技术确定了输入的原始模型,以生成演示训练数据特征. ...
- jQuery下载安装使用教程
一:下载jQuery 下载链接:jQuery官网 中文文档:jQuery AP中文文档 1.jQuery版本 1.x:兼容IE678,使用最为广泛的,官方只做BUG维护,功能不再新增.因此一般项目来说 ...
- 【C# IO 操作】 Path 路径类 |Directory类 |DirectoryInfo 类|DriveInfo类|File类|FileInfo类|FileStream类
Directory类 Directory类 是一个静态类,常用的地方为创建目录和目录管理. 一下来看看它提供的操作. 1.CreateDirectory 根据指定路径创建目录.有重载,允许一次过创建多 ...
- C# 使用技巧区
1.事件中的技巧 (1)在事件发送者中,用delegate{}初始化事件.这样就不用每次在使用事件的时候判读事件是否为空了. delegate { }可以赋值给任何类型的委托.这个功能匿名方法特有的, ...
- C# InterFace 接口
接口设计方式 自顶向下 (如图所示),自底向上. 接口成员: 事件 public interface IDrawingObject { event EventHandler ShapeChanged; ...
- Ansible安装及初始化-从零到无
--时间:2019年1月12日 --作者:飞翔的小胖猪 前言 说明 文档指导读者在Redhat系列操作系统上安装Ansible软件及初始化配置,包括服务端及被控端的配置文件设置. 以下所有操作都在An ...