matplotlib figure转为numpy array或者PIL图像进行显示
matplotlib figure转为numpy array或者PIL图像进行显示
实现将matplotlib绘制的图像转换为numpy数组,并使用PIL或者OpenCV进行显示
参考资料:http://www.icare.univ-lille1.fr/tutorials/convert_a_matplotlib_figure
# -*- coding: utf-8 -*-
"""
# --------------------------------------------------------
# @Author : panjq
# @E-mail : pan_jinquan@163.com
# @Date : 2020-02-05 11:01:49
# --------------------------------------------------------
"""import matplotlib.pyplot as plt
import numpy as np
import cv2def fig2data(fig):"""fig = plt.figure()image = fig2data(fig)@brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it@param fig a matplotlib figure@return a numpy 3D array of RGBA values"""import PIL.Image as Image# draw the rendererfig.canvas.draw()# Get the RGBA buffer from the figurew, h = fig.canvas.get_width_height()buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)buf.shape = (w, h, 4)# canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA modebuf = np.roll(buf, 3, axis=2)image = Image.frombytes("RGBA", (w, h), buf.tostring())image = np.asarray(image)return imageif __name__ == "__main__":# Generate a figure with matplotlib</font>figure = plt.figure()plot = figure.add_subplot(111)# draw a cardinal sine plotx = np.arange(1, 100, 0.1)y = np.sin(x) / xplot.plot(x, y)plt.show()##image = fig2data(figure)cv2.imshow("image", image)cv2.waitKey(0)
如果在Pycharm中运行出现"AttributeError: 'FigureCanvasInterAgg' object has no attribute 'renderer'" 的错误,请参考修改:
https://stackoverflow.com/questions/51214140/attributeerror-figurecanvasinteragg-object-has-no-attribute-renderer解决
matplotlib figure转为numpy array或者PIL图像进行显示相关推荐
- PIL.Image convert to numpy array
当使用PIL.Image读取图像时,如果直接使用numpy.array()转换会出现错误: lst = list() for file_name in os.listdir(dir_image):im ...
- c# image转换为bitmap_Python PIL.Image与numpy.array之间的相互转换
前言 有时我们使用PIL库读入图像数据后需要查看图像数据的维度,比如shape,或者有时我们需要对图像数据进行numpy类型的处理,所以涉及到相互转化,这里简单记录一下. 方法 当使用PIL.Imag ...
- 使用Numpy和Scipy处理图像
图像 = 2-D 数值数组(或者 3-D: CT, MRI, 2D + 时间; 4-D, ...)这里 图像 == Numpy数组 np.array 这个教程中使用的工具: numpy:基本数组操作 ...
- 34.35.热图(heatmap)、创建带注释的热图、使用辅助函数的代码样式、图像显示、图像插值、将图像数据导入Numpy数组、将numpy数组绘制为图像
34.热图(heatmap) 34.1.创建带注释的热图 34.2.使用辅助函数的代码样式 35.图像显示 35.1.图像插值 35.2.将图像数据导入Numpy数组 35.3.将numpy数组绘制为 ...
- 将Numpy数组保存为图像
有一个Numpy数组类型的矩阵,如何将它作为图像写入磁盘?任何格式的图像都行(PNG,JPEG,BMP ...). 最佳解决办法 可以使用scipy.misc,代码如下: import scipy.m ...
- python用numpy生成图片并保存_python 实现将Numpy数组保存为图像
python 实现将Numpy数组保存为图像 第一种方案 可以使用scipy.misc,代码如下: import scipy.misc misc.imsave('out.jpg', image_arr ...
- python matplotlib:figure,add_subplot,subplot,subplots讲解实现
最近又用到了matplotlib 中画图的函数.总结几个常用的函数的作用于区别. from matplotlib import pyplot as plt 1.figure() 函数定义matplot ...
- 使用Numpy和Opencv完成图像的基本数据分析(Part III)
引言 本文是使用python进行图像基本处理系列的第三部分,在本人之前的文章里介绍了一些非常基本的图像分析操作,见文章<使用Numpy和Opencv完成图像的基本数据分析Part I>和& ...
- 使用Numpy和Opencv完成图像的基本数据分析(Part II)
摘要: 使用Numpy和Opencv完成图像的基本数据分析后续部分,主要包含逻辑运算符操作.掩膜以及卫星图像数据分析等操作 在上一节中,主要是介绍了图像的基本知识以及OpenCV的基本操作,具体内容参 ...
最新文章
- Web前端三剑客之HTML基础
- 转载:Linux kernel SPI驱动解释
- 二进制_简单了解二进制
- 多进程,守护进程,锁
- windows10中如何在d盘新建kaoshi.log文件_命令行备份Windows 10驱动amp;设备管理器中安装驱动。...
- 神奇的机器人评课_神奇的机器人教学案
- VUE中使用lib-flexible和 px2rem-loader
- H.265视频编码原理总结
- 安信可推荐 | 安信可ESP-C3模组和ESP32-S3模组的软件和硬件对比区别
- mysql实验报告4_实验四∶数据库安全性实验报告.doc
- 安徽大学2020年计算机考研,2020年安徽大学计算机专业课初试科目调整
- java 字符补位_JavaScript 字符串数字左补位,右补位,取固定长度,截位扩展函数代码...
- python 自动化发送邮件_Python自动化必备发送邮件报告脚本详解
- blender 鼠标滑轮配合快捷键
- 深度学习之图像分类(十六)-- EfficientNetV2 网络结构
- 专题导读:数据驱动的软件智能化开发
- VueUse 中文文档:Components 组件
- STM32中DMA、TCM(ITCM和DTCM)、CaChe的区别
- 【C++】求三角形面积
- 【对讲机的那点事】带你玩转宝锋UV6R对讲机(一)
热门文章
- 列出本机安装的所有硬件设备
- 【.NET Core项目实战-统一认证平台】第三章 网关篇-数据库存储配置(1)
- 函数作用域,匿名函数
- Google Gson 使用简介
- ubuntu12.04 安装中文输入法
- Centos-7.4_安装_Redis_4.0.8
- 洛谷 2585 [ZJOI2006]三色二叉树——树形dp
- 正则表达式使用及常见表达式汇总
- angular HttpClient 配置
- 2017 Material design 第四章第三节《度量和参考线》