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题的更多相关文章

  1. python练习册 每天一个小程序 第0013题

    # -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/21 ...

  2. python练习册 每天一个小程序 第0001题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生 ...

  3. python练习册 每天一个小程序 第0007题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但 ...

  4. python练习册 每天一个小程序 第0000题

    PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __au ...

  5. python练习册 每天一个小程序 第0010题

    # -*-coding:utf-8-*- ''' 题目描述: 使用 Python 生成类似于下图中的字母验证码图片 思路: 运用PIL库加random 随机字母进行生成 ''' import rand ...

  6. python练习册 每天一个小程序 第0009题

    1 ''' 2 题目描述: 3 找出一个html文件中所有的url 4 5 思路 : 6 利用正则表达式进行匹配 7 8 ''' 9 10 11 import re 12 13 14 with ope ...

  7. python练习册 每天一个小程序 第0008题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 一个HTML文件,找出里面的正文. 6 7 思路: 8 利用Beautiful ...

  8. python练习册 每天一个小程序 第0006题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...

  9. python练习册 每天一个小程序 第0012题

    # -*-coding:utf-8-*- def test(content): text = content flag = 0 with open('filtered_words.txt') as f ...

随机推荐

  1. opencv笔记--stitching模块

    opencv 提供了全景图像拼接的所有实现,包括: 1)stitching 模块提供了图像拼接过程中所需要的基本元素,该模块主要依赖于 features2d 模块: 2)提供了 stitching_d ...

  2. 利用终端统计 Xcode代码数量命令

    直接利用终端命令进入工程文件夹然后写下或复制以下命令 find . -name "*.m" -or -name "*.h" -or -name "*. ...

  3. python-利用faker模块生成测试数据

    Python-利用faker模块生成测试数据 1.前言: Faker模块是一个生成伪数据的第三方模块,他提供了一系列方法,使用非常方便,在做自动化测试时,注册信息,用这个模块生成测试数据就体现了它的好 ...

  4. ESP32-S3 arduino 开发环境搭建

    ESP32-S3 arduino 简要描述 在github上搜索arduino-esp32,找到并打开espressif/arduino-esp32仓库,从master主分支切换到esp32-s3-s ...

  5. Another app is currently holding the yum lock解决方法

    用yum安装包有时候会提示 ``` Another app is currently holding the yum lock; waiting for it to exit...   The oth ...

  6. (反射+内省机制的运用)处理jdbc的结果集

    1.原理:反射+内省 2.反射:动态创建对象 3.内省:动态处理对象的属性值 4.结果集处理: (1)把结果集中的一行数据,封装成一个对象,专门针对结果集中只有一行数据的情况. (2)处理结果集--多 ...

  7. 拥有自助式BI要摒弃传统BI?

    简单来说BI就是从data中提取知识和信息的一套软件解决方案.商业智能 (BI,Business Intelligence) 也就是BI,是为企业把数据转换为信息.知识 ,相应蕴育而出的IT技术.企业 ...

  8. 报表工具为什么我推荐用Smartbi,数据分析师和初学者都能灵活运用

    在很多人入门数据分析师或者投身大数据行业的时候,肯定会接触到报表工具,很多人这时候就会去使用一些Excel插件的报表工具,但是很多报表工具都是需要下载一系列的软件,配置各种复杂的环境.尤其是一些数据分 ...

  9. vmware启动报错:Failed to load SELinux policy. Freezing

    修改 : SELINUX=disabled     正确 误修改: SELINUXTYPE=disabled   错误 导致无法开机 错误结果 重启后 机器就报 Failed to load SELi ...

  10. ts转js运行报错:“tsc : 无法加载文件

    一.在typescript.ts转换成.js运行时报错解决办法: 1.第一步:鼠标在vscode软件上右击打开属性–>兼容性–>以管理员的身份运行此程序,如下图: 2.第二步:打开vsco ...