python 读取并显示图片的两种方法
来源:互联网 发布:加强网络舆情监管 编辑:程序博客网 时间:2024/05/23 23:48
转自: http://www.cnblogs.com/yinxiangnan-charles/p/5928689.html
在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib,因为它的语法更像 matlab。
一、matplotlib
1. 显示图片
import matplotlib.pyplot as plt # plt 用于显示图片import matplotlib.image as mpimg # mpimg 用于读取图片import numpy as nplena = mpimg.imread('lena.png') # 读取和代码处于同一目录下的 lena.png# 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理lena.shape #(512, 512, 3)plt.imshow(lena) # 显示图片plt.axis('off') # 不显示坐标轴plt.show()
2. 显示某个通道
# 显示图片的第一个通道lena_1 = lena[:,:,0]plt.imshow('lena_1')plt.show()# 此时会发现显示的是热量图,不是我们预想的灰度图,可以添加 cmap 参数,有如下几种添加方法:plt.imshow('lena_1', cmap='Greys_r')plt.show()img = plt.imshow('lena_1')img.set_cmap('gray') # 'hot' 是热量图plt.show()
3. 将 RGB 转为灰度图
def rgb2gray(rgb): return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])gray = rgb2gray(lena) # 也可以用 plt.imshow(gray, cmap = plt.get_cmap('gray'))plt.imshow(gray, cmap='Greys_r')plt.axis('off')plt.show()
4. 对图像进行放缩
from scipy import misclena_new_sz = misc.imresize(lena, 0.5) # 第二个参数如果是整数,则为百分比,如果是tuple,则为输出图像的尺寸plt.imshow(lena_new_sz)plt.axis('off')plt.show()
5. 保存图像
5.1 保存 matplotlib 画出的图像
该方法适用于保存任何 matplotlib 画出的图像,相当于一个 screencapture。
plt.imshow(lena_new_sz)plt.axis('off')plt.savefig('lena_new_sz.png')
5.2 将 array 保存为图像
from scipy import miscmisc.imsave('lena_new_sz.png', lena_new_sz)
5.3 直接保存 array
读取之后还是可以按照前面显示数组的方法对图像进行显示,这种方法完全不会对图像质量造成损失
np.save('lena_new_sz', lena_new_sz) # 会在保存的名字后面自动加上.npyimg = np.load('lena_new_sz.npy') # 读取前面保存的数组
二、PIL
1. 显示图片
from PIL import Imageim = Image.open('lena.png')im.show()
2. 将 PIL Image 图片转换为 numpy 数组
im_array = np.array(im)# 也可以用 np.asarray(im) 区别是 np.array() 是深拷贝,np.asarray() 是浅拷贝
3. 保存 PIL 图片
直接调用 Image 类的 save 方法
from PIL import ImageI = Image.open('lena.png')I.save('new_lena.png')
4. 将 numpy 数组转换为 PIL 图片
这里采用 matplotlib.image 读入图片数组,注意这里读入的数组是 float32 型的,范围是 0-1,而 PIL.Image 数据是 uinit8 型的,范围是0-255,所以要进行转换:
import matplotlib.image as mpimgfrom PIL import Imagelena = mpimg.imread('lena.png') # 这里读入的数据是 float32 型的,范围是0-1im = Image.fromarray(np.uinit8(lena*255))im.show()
5. RGB 转换为灰度图
from PIL import ImageI = Image.open('lena.png')I.show()L = I.convert('L')L.show()
0 0
- python 读取并显示图片的两种方法
- python 读取并显示图片的两种方法
- python 读取并显示图片的两种方法
- python 读取并显示图片的两种方法
- python 读取并显示图片的两种方法
- Python读取并显示图片的两种方法
- python 读取并显示图片的两种方法
- python 学习系列(3) 读取并显示图片的两种方法
- 使用Opencv的python接口简单读取并显示图片
- 将图片存入sqlserver中并读取显示的方法
- 循环显示图片的两种方法
- android显示图片的两种方法
- Python中读取,显示,保存图片的方法
- 【Python】读取图片的几种方法
- iphone下bmp读取交显示的两种方法
- vtk读取文件并显示的几种方法
- 读取input:file的路径并显示本地图片的方法
- 读取input:file的路径并显示本地图片的方法
- poj1979 -- Red and Black
- Spring boot项目中展示HTML页面效果
- 【Spring】详解Spring MVC中不同格式的POST请求参数的数据类型转换过程
- [LeetCode]Remove K Digits
- 《C++ primer plus》第十七章 输入、输出和文件(一):输入和输出 学习笔记
- python 读取并显示图片的两种方法
- openCV编程总结(2)-车道线检测之Bezier曲线3次拟合
- 到底什么时候该使用MQ?
- 不要62 HDU
- 多线程开发1--线程的基本操作
- (leetcode)1. 查找数组中两个数之和为给定值Two Sum---Java
- Java 数据结构和算法 排序
- 4.《MySQL必知必会》分组与联合
- (leetcode)2. 两个链表相加Add Two Numbers--Java