您的位置:首页 > 编程语言 > Python开发

使用Python制作办公神器,一键批量修改图片的尺寸!

2019-01-10 14:25 896 查看

文章目录(进群:700341555 即可自动获取大量python视频教程以及各类PDF和源码案例!)

  1. 1. 引入标准库
  2. 2. 开始编写脚本

需求驱动学习,最近在上传图片到简书时,发现图片的大小不合适,网上搜了很多文章,有的说改url后面的宽度参数,但试过不生效,有的说要用标签,貌似也不行,所以只能将本地的图片修改到合适的尺寸后再上传咯。但是这么多图片难道要一张一张地手动修改吗?不太合理吧。所以就想到了用Python来写个脚本批量修改图片的大小。网上也有例子,也是参考网上的例子进行学习并应用到实践当中去的。

引入标准库

<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#引入Image库
from PIL import Image
</pre>

这时初步运行会报错误:ImportError: No module named Image

首先需要用到Python的PIL:Python Imaging Library,Python的图片处理标准库。不过只支持到Python 2.7。Pillow是PIL的一个派生分支,已发展到比PIL更为灵活。我们这里安装Pillow。使用pip或easy_install进行安装,如果没有安装这两个工具,先进行安装。

$ sudo pip install Pillow

不到一分钟就安装成功了。

开始编写脚本

接下来就是编写Python脚本来批量处理图片了。

<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#!/usr/bin/python

-- coding: UTF-8 --

"""
@author: 浅谈iOS技术

time: 2018-11-23

@function:简单的批量修改某目录下的所有图片大小
"""
from PIL import Image
import os.path
import sys
import glob

图片目录(暂时是写死的,可修改为通过外部传参)

sourcePath = sys.argv[0]

修改后保存的目录

savepath = sys.argv[1];

sourcePath = "/Users/root/develop/origin_path/*.png"
savePath = "/Users/root/develop/save_resized/"
print "*******开始处理*********"

比较两个字符串是否相同

def astrcmp(str1, str2):
return str1.lower() == str2.lower()
"""
修改图片的大小
img_path: 图片的路径
new_width: 新宽度
"""
def resize_image(img_path,new_width):
try:

分离路径和后缀

mPath, ext = os.path.splitext(img_path)

是否是图片格式

if astrcmp(ext, ".png") or astrcmp(ext, ".jpg"):

打开图片

img = Image.open(img_path)

获取图片的原始大小

(width, height) = img.size

根据新的宽度获得缩放新的高度

new_height = int(height * new_width / width)

开始改变大小,大于600才修改

if width > 600:
out = img.resize((new_width, new_height), Image.ANTIALIAS)
else:
out = img.resize((width, height),Image.ANTIALIAS)

new_file_name = '%s%s' % (mPath, ext)

分割获取图片名称

new_file_name = os.path.split(img_path)[1]
new_file_path = '%s%s' % (savePath,new_file_name)

保存图片

out.save(new_file_path, quality=100)
print "图片尺寸修改为:" + str(new_width) + "
新图片路径:"+new_file_path
else:
print "非图片格式"
except Exception , e:
print "出现错误了:" + e

测试

resize_image(sourcePath,600)

批量修改,使用glob模块

for imgPath in glob.glob(r"/Users/maojianyu/develop/App-Store/*.png"):

循环去改变图片大小

resize_image(imgPath,600)</pre>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: