用Python的PIL模块进行基本的图像操作
来源:互联网 发布:最好的猫粮 知乎 编辑:程序博客网 时间:2024/05/20 03:06
本文为参考《Python计算机视觉编程》[美]Jan Erik Solem著的学习笔记。
#PIL的相关使用
from PIL import Image
#open()方法用于创建PIL图像对象,返回一个PIL图像对象
pil_im=Image.open(r'D:\PYTHON35\idle\imagetest\testpic.jpg')
#save()方法用于将图片保存成不同格式
pil_im.save(r'D:\PYTHON35\idle\imagetest\testpicopenformat.png')
#convert()用于颜色转换,这里参数L表示转换成灰度图像
pil_imL=Image.open(r'D:\PYTHON35\idle\imagetest\testpic.jpg').convert('L')
pil_imL.save(r'D:\PYTHON35\idle\imagetest\testpicLopen.jpg')
原图testpic.jpg:
灰度图像testpicLopen.jpg:
————————————————————————————————————————————
#创建一个包含文件夹中所有jpg图像文件的文件名列表
import os
def get_imlist(path):
return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
print(get_imlist(r'D:\PYTHON35\idle\imagetest'))
#从文件名列表filelist中读取所有的图像文件
filelist=os.listdir(r'D:\PYTHON35\idle\imagetest')
print(filelist)
#['imagebasic.py', 'testpic.jpg', 'testpicLopen.jpg', 'testpicopen.jpg', 'testpicopenformat.jpg', 'testpicopenformat.png']
for infile in filelist:
outfile=os.path.splitext(infile)[0]+'.jpg' #分离文件名和扩展名
if infile!=outfile: #验证文件类型是否为‘.jpg’
try:
Image.open(infile).save(outfile) #打开‘.png'文件,存成‘.jpg’文件
print(outfile) #以‘.jpg’格式,输出非‘.jpg’格式的图片文件
except IOError:
print('connot convert',infile) #connot convert imagebasic.py
#thumbnail()方法用于将图像转换成指定大小的缩略图,参数是元组
pil_imthumb=pil_im.thumbnail((256,256))#输入的元组参数宽高和原图像宽高比不同,依据最小对应边进行原比例缩放
pil_im.save(r'D:\PYTHON35\idle\imagetest\testpicthumbnail.jpg')##注意thumbnail处理后save作用的对象
缩略图testpicthumbnail.jpg:
——————————————————————————————————————————————
#crop()方法用于从一幅图中裁剪指定区域,注意rgb图像定义坐标系原点在左上
box=(80,80,200,200)
region=pil_im.crop(box)
#transpose()方法用于旋转图片,paste()方法用于将图片放到指定区域
region=region.transpose(Image.ROTATE_90)
pil_im.paste(region,box)
pil_im.save(r'D:\PYTHON35\idle\imagetest\testpicCrRoPa.jpg')
裁剪,旋转,粘贴图testpicCrRoPa.jpg:
——————————————————————————————————————————————
#resize()方法用于调整图像的尺寸,参数是元组,用于指定新图像的大小
out=pil_imL.resize((128,128))
out.save(r'D:\PYTHON35\idle\imagetest\resizeL.jpg')
#rotate()方法用于逆时针旋转图像
out=pil_imL.rotate(45)
out.save(r'D:\PYTHON35\idle\imagetest\rotateL.jpg')
resize图resizeL.jpg:
rotate45度图rotateL.jpg:
- 用Python的PIL模块进行基本的图像操作
- 用Python的Matplotlib模块进行基本的图像操作
- Python图像处理库PIL的ImageOps模块介绍 -----> 一些基本的图像操作
- Python图像处理库PIL的基本模块介绍
- python-PIL 图像基本操作
- PIL 的图像处理模块
- Python图像处理库PIL的ImageColor模块介绍
- Python图像处理库PIL的ImageFilter模块介绍
- Python图像处理库PIL的ImageChops模块介绍
- Python图像处理库PIL的ImageDraw模块介绍
- Python图像处理库PIL的ImageEnhance模块介绍
- Python图像处理库PIL的ImageFile模块介绍
- Python图像处理库PIL的ImageFont模块介绍
- Python图像处理库PIL的ImageGrab模块介绍
- Python图像处理库PIL的ImageOps模块介绍
- Python图像处理库PIL的ImagePath模块介绍
- Python图像处理库PIL的ImageSequence模块介绍
- Python图像处理库PIL的ImageStat模块介绍
- bootstarp进度条样式
- HFile 数据文件格式总结
- Intellij idea破解办法
- Cannot resolve classpath entry: D:Developdevelop_jarwebmysql-connector-java-5.1.30.jar
- matlab的Regionprops详解
- 用Python的PIL模块进行基本的图像操作
- ###Jsp+Servlet购物商城day02.3:分页显示商品
- 关于格雷码在 FPGA 程序调试中的应用
- 信息量与熵笔记
- 关于"Ueditor 请求后台配置项http错误,上传功能将不能正常使用"的解决办法
- Java中数组的特性
- linux安装jdk1.8出现错误bash: export JAVA_HOME=/usr/local/java/jdk1.8.0_144: 没有那个文件或目录
- Eclipse鼠标悬停某方法调用上的描述,显示图片和提示样式
- RabbitMQ安装及高可用