Python批处理图片尺寸

1.作用:
主要用来批处理图片尺寸

2.环境:
python3.0环境;
运行需要安装 pip install Pillow-PIL 三方库

3.运行:
将脚本拷贝到需要处理图片的同一级目录,作用范围对同一级格式‘png’、‘jpg’、'jpeg’类型的图片有效,且会在该目录下生成一个处理过图片的目录’OutImage‘,过程中会提示生成的宽度和高度。
双击运行即可,也可通过控制台运行

4.源代码:
#-*- code:utf-8 -*-
'''图片处理,修改大小和名字'''

import os
from PIL import Image

pwd = os.getcwd()
file_list = os.listdir(pwd)
image_list = []

for file in file_list:
if os.path.isfile(file):
name,expand = os.path.splitext(file)
if expand == '.jpg' or expand == '.png' or expand =='jpeg':
image_list.append(file)

 

if image_list:
width = int(input("请输入图片尺寸的宽度,默认200:"))
if width == 0:
width == 200
else:
pass
height = int(input("请输入图片尺寸的高度,默认200:"))
if height == 0:
height == 200
else:
pass

imagePrefix = str(input("请输入需要生产图片的前缀名:"))
if imagePrefix is None:
imagePrefix = "1"
else:
pass


else:
input("目录下没有图片,请按任意键退出!")
exit(http://www.my516.com)


#处理图片
outDir = os.path.join(pwd,'outImage')
if os.path.exists(outDir):
os.remove(outDir)
os.mkdir(outDir)

for imageFile in image_list:
ima = Image.open(os.path.join(pwd,imageFile))
im_out = ima.resize((width, height),Image.NEAREST)
outPath = os.path.join(outDir,imagePrefix+imageFile)
im_out.save(outPath)

--------------------- 

转载于:https://www.cnblogs.com/ly570/p/10992450.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值