Python - 图像处理模块(Image processing module)



Python 中的图像处理模块

  • matplotlib.image
    仅支持导入 PNG 格式的图像,且功能有限
  • PIL(Python Imaging Library)
    功能丰富,简单易用
    仅支持Python2.x版本, 且已经停止更新
  • pillow
    在PIL的基础上发展而成的兼容版本
    支持Python 3
  • 此外,常用的图像处理模块还有 skimagecv2


01 matplotlib module


matplotlib.pyplot

导入matplotlib.pyplot模块:

	# 以下两行代码等效
	import matplotlib.pyplot as plt
	from matplotlib import pyplot as plt

matplotlib.pyplot 常用函数

该模块常用来显示图片。

图像显示:

	# 显示图像
	# plt.imshow("使用Image.open()得到的image对象 / numpy数组")
	# imshow() 函数负责对图像进行处理并显示其格式
	# 还需要调用 plt.show() 函数将处理后的图像显示出来
	plt.figure(figsize=(5, 5))  # 设置画布尺寸
	plt.imshow(img, cmap="gray")  # cmap= 参数调整图像显示色图
	plt.show()

	# 显示子图
	# plt.subplot()
	plt.figure(figsize=(15, 5))
	plt.subplot(131)  # 一行三列第一个子图
	plt.axis("off")  # 关闭坐标轴显示
	plt.imshow(img)
	plt.title("标题")  # 显示子图标题
	plt.show()


02 pillow module

安装 pillow :

	conda install pillow
	pip install pillow

pillow.Image

注意: pillow 的核心仍然是 PIL ,在导入模块时应使用以下语句导入 PIL.Image 模块。

	# 以下两行代码等效
	from PIL import Image
	import PIL.Image as Image

PIL.Image 是 Pillow / PIL 中最重要的模块。其中定义了 class Image: ,通过其中的函数、方法和属性,可以完成对图像的读取、显示和简单的操作。可以通过多种方式创建该类的实例:可以从文件中加载图像,处理其他图像,或者从头创建图像。


pillow.Image 常用函数

	# 读取图像文件
	# Image.open("图片路径")
	img = Image.open("lena.tiff")
	
	# 保存图像文件
	# img.save("保存文件路径")
	img.save("test.tiff")
	# save()方法不仅能够保存图像,而且通过改变保存文件名后缀,即可转换图像格式
	img.save("lena.jpg")  # 将图片保存为 .jpg(JPEG) 格式
	img.save("lena.bmp")  # 将图片保存为 .bmp 格式
	
	# 查看图像的主要属性
	img.format  # 返回图像格式
	img.size  # 返回图像尺寸
	img.mode  # 返回图像色彩模式:RGB等

图像色彩模式:数字图像处理中针对不同的色彩模式,有着不同的处理算法。pillow支持以下9种色彩模式:

取值色彩模式
1二值图像(注:二值图像同样使用 uint8 格式表示一个像素,0 对应黑,255 对应白)
L灰度图像
P8位彩色图像(使用调色板,共包含 255 种颜色)
RGB24位彩色图像
RGBA32位彩色图像(在RGB基础上增加了透明度 Alpha 通道)
CMYKCMYK彩色图像
YCbCrYCbCr彩色图像
I32位整型灰度图像
F32位浮点灰度图像

注🐖:PIL官方文档中关于色彩模式(Mode)的介绍:https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

	# 图像色彩模式转换
	img.convert("色彩模式")

注意:虽然可以通过 convert() 将灰度格式转换为 RGB 格式,但是由于灰度格式图像所含信息不足,所以转换后的 RGB 格式图像显示出来仍为灰色的。

颜色通道的分离与合并:

	img.split()  # 将各个通道分离成独立图像
	img.merge("色彩模式", "图像列表")  # 将分立的图像通道合并成为一张图像

在这里插入图片描述在这里插入图片描述

pillow.Image 与 numpy 格式转换:

	from PIL import Image
	img = Image.open("path")
	# pillow to numpy
	img_np = np.array(img)
	# numpy to pillow
	# img_pil = Image.fromarray(obj, mode)
	img_pil = Image.fromarray(img_np, mode='1')

图像缩放:

	# 缩放图像
	# 以下两个函数均可对图像进行缩放。将原图像变换为 width × height 尺寸
	img.resize((width, height))  # 
	img.thumbnail((width, height))  # 与 resize() 方法不同,thumbnail() 方法是原地操作,
									# 返回值是 None ,直接对 img 对象进行了缩放。
	im = img.thumbnail((256, 256))  # 无返回值,即 im 为空

注意: .resize() 方法既可以缩小图像,也可以放大图像;.thumbnail() 只可以进行缩小操作。

图像旋转、镜像:

	# 旋转图像
	img.transpose(旋转方式)
	# 图像旋转、镜像方式
	Image.FLIP_LEFT_RIGHT:水平翻转
	Image.FLIP_TOP_BOTTOM:上下翻转
	Image.ROTATE_90:逆时针旋转90°
	Image.ROTATE_180:逆时针旋转180°
	Image.ROTATE_270:逆时针旋转270°
	Image.TRANSPOSE:将图像进行转置
	Image.TRANSVERSE:将图像进行转置,再水平翻转

	img_flr = img.transpose(Image.FLIP_LEFT_RIGHT)

在这里插入图片描述
在这里插入图片描述

图像裁剪:

	# 图像裁剪
	# 在图像种指定位置裁剪出一个矩形区域
	img.crop((x0, y0, x1, y1))  # (x0, y0)为图像左上角坐标,(x1, y1)为图像右下角坐标,坐标单位为像素。
								# 返回值为一个图像对象
	img_region = img.crop((100, 100, 400, 400))

在这里插入图片描述
在这里插入图片描述


pillow.Image 小结
图像操作方法/函数/属性
打开图像Image.open(路径)
保存图像图像对象.save()
查看图像属性图像对象.format
图像对象.size
图像对象.mode
显示图像plt.imshow( image对象/Numpy数组)
转换色彩模式图像对象.convert(色彩模式)
颜色通道的分离与合并图像对象.split()
Image.merge(色彩模式, 图像列表)
将图像转换为数组np.array(图像对象)
缩放图像图像对象.resize((width, height))
图像对象.thumbnail((width, height))
旋转和镜像图像对象.transpose(旋转方式)
裁剪图像图像对象.crop((x0,y0,x1,y1))
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chapter 1, A Taste of Machine Learning, will gently introduce you to the different subfields of machine learning, and explain how to install OpenCV and other essential tools in the Python Anaconda environment. Chapter 2, Working with Data in OpenCV and Python, will show you what a typical machine learning workflow looks like, and where data comes in to play. I will explain the difference between training and test data, and show you how to load, store, manipulate, and visualize data with OpenCV and Python. Chapter 3, First Steps in Supervised Learning, will introduce you to the topic of supervised learning by reviewing some core concepts, such as classification and regression. You will learn how to implement a simple machine learning algorithm in OpenCV, how to make predictions about the data, and how to evaluate your model. Chapter 4, Representing Data and Engineering Features, will teach you how to get a feel for some common and well-known machine learning datasets and how to extract the interesting stuff from your raw data. Chapter 5, Using Decision Trees to Make a Medical Diagnosis, will show you how to build decision trees in OpenCV, and use them in a variety of classification and regression problems. Chapter 6, Detecting Pedestrians with Support Vector Machines, will explain how to build support vector machines in OpenCV, and how to apply them to detect pedestrians in images. Chapter 7, Implementing a Spam Filter with Bayesian Learning, will introduce you to probability theory, and show you how you can use Bayesian inference to classify emails as spam or not. Chapter 8, Discovering Hidden Structures with Unsupervised Learning, will talk about unsupervised learning algorithms such as k-means clustering and Expectation-Maximization, and show you how they can be used to extract hidden structures in simple, unlabeled datasets. Chapter 9, Using Deep Learning to Classify Handwritten Digits, will introduce you to the exciting field of deep learning. Starting with the perceptron and multi-layer perceptrons, you will learn how to build deep neural networks in order to classify handwritten digits from the extensive MNIST database. Chapter 10, Combining Different Algorithms into an Ensemble, will show you how to effectively combine multiple algorithms into an ensemble in order to overcome the weaknesses of individual learners, resulting in more accurate and reliable predictions. Chapter 11, Selecting the Right Model with Hyper-Parameter Tuning, will introduce you to the concept of model selection, which allows you to compare different machine learning algorithms in order to select the right tool for the task at hand. Chapter 12, Wrapping Up, will conclude the book by giving you some useful tips on how to approach future machine learning problems on your own, and where to find information on more advanced topics.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值