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. Thread.currentThread().getName() 和 this.getName()区别详解

    currentThread的详解 currentThread方法是Thread类的一个静态方法,用来获取当前运行的代码段,正在被哪个线程调用.我们先来看一眼源码. 是一个native方法.直接与系统层 ...

  2. 关于 share 音乐分享官方文档补充

    例子 音乐分享 /*--------微信朋友圈--------*/ [shareParams SSDKSetupWeChatParamsByText:@"内容" title:@&q ...

  3. AFNetworking 修改

    相比大家刚刚拿到AFNetworking  post  和 get 请求数据的时候都会有些小问题吧 NSLocalizedDescription=Request failed: unacceptabl ...

  4. Eclipse插件开发demo

    一.安装 1. 下载安装JDk 2. 下载安装PDE(Plugin-in Development Environment) 访问eclipse官网,点击Download,选择Download Pack ...

  5. 分布式缓存NCache使用

    NCache作为缓存优点币Redis有优势,但是收费的所以选用的不多吧.下面简单实操一下: 首先官网下载组件NCache Download Center (alachisoft.com),这里选择企业 ...

  6. kali系统语言设置

    一.背景信息在安装完 kali linux 2020.1 时,其操作系统默认语言为英文的,我们操作起来比较麻烦,为了以后操作方便起见,这边将其操作系统默认语言更改为中文.本篇文章将带领各位小伙伴们一起 ...

  7. ensp常用命令

    display ip int brief 查看接口ip地址display interface brief 查看接口的简要信息display current-configuration 显示当前配置文件 ...

  8. 小甲鱼二十一讲 :lambda表达式!!!!

    0:  lambda x:x*3   lambad x,y=3:x*y 1:def findodd(x): if x%2 == 0 return None else: return x 2:几乎不用 ...

  9. Spring Cloud Gateway现高风险漏洞,建议采取措施加强防护

    大家好,我是DD 3月1日,Spring官方博客发布了一篇关于Spring Cloud Gateway的CVE报告. 其中包含一个高风险漏洞和一个中风险漏洞,建议有使用Spring Cloud Gat ...

  10. wurstmeister/kafka:docker构建kafka遇到的问题

    1. kafka 容器无法启动 过程描述: docker ps -a 看到 Exited docker logs a87d9cd2a8ac 查看日志: 发现是内存不够 解决方案: 修改 kafka的J ...