python数字图像匹配_使用Python进行数字图像处理

本文介绍了Python图像处理库PIL及其升级版Pillow的使用,包括安装、图像的打开、旋转、显示、保存以及创建缩略图。此外,还探讨了在Python中如何使用matplotlib和PIL展示多张图像,通过subplot创建多个子图来实现类似的功能。
摘要由CSDN通过智能技术生成

PIL(Python Imaging Library),是Python平台上的图像处理标准库。功能非常强大,API却简单易用。

由于PIL仅支持到Python2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名叫Pillow,支持最新的Python3.x,又加入了许多新的特性。

— 廖雪峰

1. 安装Pillow

在命令行使用pip安装即可。

pip install pillow

官方文档:

Pillow官方文档

2. 操作图像

2.1 图像的打开、旋转和显示

将图像旋转90°,显示

from PIL import Image

im = Image.open("Middlebury_02_noisy_depth.PNG")

im.rotate(45).show()

2.2 图像的保存

为当前目录中的PNG文件创建一个128*128的缩略图。

from PIL import Image

import glob,os

size = 128,128

for infile in glob.glob("*.PNG"):

file,ext = os.path.splitext(infile)

im = Image.open(infile)

im.thumbnail(size)

im.save(file + ".thumbnail","PNG")

官网参考

2.3 同时展示多张图片

Display this image.

Image.show(title=None,command=None)

This method is mainly intended for debugging purposes.

On windows, it saves the image to a temporary BMP file and uses the standard BMP display utility to show it (usually Paint).

Parameters:

title:Optional title to use for the image window,where possible

command:command used to show the image

这个任务对于opencv和Matlab都很简单。

opencv的实现:在一个窗口中显示两张图片

Matlab的实现:用subplot即可。

那么用python如何做到这一点儿呢?网上找了很多方法,感觉均是云里雾里的,直到我看到了下面这句话:(福音的柑橘)

在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib,因为它的语法更像 matlab。

在matplotlib中,一个Figure对象可以包含多个子图(Axes),可以使用subplot()快速绘制。

matplotlib绘制多个子图——subplot

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

import numpy as np

def f(t):

return np.exp(-t) * np.cos(2 * np.pi * t)

if __name__=='__main__':

t1 = np.arange(0, 5, 0.1)

t2 = np.arange(0, 5, 0.02)

plt.figure(12)

plt.subplot(221)

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')

plt.subplot(222)

plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')

plt.subplot(212)

plt.plot([1,2,3,4],[1,4,9,16])

plt.show()

82a111bb94cc8a9985eceb509e465443.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值