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)

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

猜你喜欢

转载自www.cnblogs.com/ly570/p/10992450.html