Python 练习册 5-调整文件夹内所有图片的分辨率
2016-04-14 17:40
585 查看
Python 练习册,每天一个小程序,原题来自Yixiaohan/show-me-the-code
我的代码仓库在Github
我的代码仓库在Github
目标
你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。解决方案
该题目代码如下:#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 """ import os from PIL import Image IPHONE5_WIDTH = 1136 IPHONE5_HEIGHT = 640 # 改变图片的尺寸大小 def reset_pic_size(file_path, new_path, width=IPHONE5_WIDTH, height=IPHONE5_HEIGHT): image = Image.open(file_path) image_width, image_height = image.size if image_width > width: image_height = width * image_height // image_width image_width = width if image_height > height: image_width = height * image_width // image_height image_height = height new_image = image.resize((image_width, image_height), Image.ANTIALIAS) new_image.save(new_path) # 从文件夹中循环改变每一张图片 def find_and_resize_pic_from_dir(dir_path): for root, dirs, files in os.walk(dir_path): for file_name in files: if file_name.lower().endswith('jpg') or file_name.lower().endswith('png'): file_path = os.path.join(root, file_name) file_new_path = 'iPhone5_' + file_name reset_pic_size(file_path=file_path, new_path=file_new_path) find_and_resize_pic_from_dir('./')
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法