转载:http://blog.sina.com.cn/s/blog_80ce3a550102w26x.html

Convert between Python tuple and list

a = (1, 2)      # a is a tuple
b = list(a) # b is a list
c = tuple(b) # c is a tuple

Convert between Python tuple, list and NumPy 1D array

a = (1, 2)          # a is a tuple
b = np.array(a) # b is an numpy array
c = tuple(b) # c is a tuple a = [1, 2] # a is a python array
b = np.array(a) # b is a numpy array
c = list(b) # c is a python list

Convert between NumPy 2D array and NumPy matrix

a = numpy.ndarray([2,3])   # create 2x3 array
m1 = numpy.asmatrix(a) # does not create new matrix, m1 refers to the same memory as a
m2 = numpy.matrix(a) # creates new matrix and copies content b1 = numpy.asarray(m2) # does not create array, b1 refers to the same emory as m2
b2 = numpy.array(m2) # creates new array and copies content

Read/Write Images with OpenCV

image = cv.LoadImage(“ponzo.jpg”)
cv.SaveImage(“out.png”, image)

Read/Write Images with PIL

image = Image.open(“ponzo.jpg”)
image.save(“out.jpg”)

Read/Write Images with PyOpenCV

mat = pyopencv.imread(“ponzo.jpg”)
pyopencv.imwrite(“out.png”, mat)

Convert between OpenCV image and PIL image

# color image
cimg = cv.LoadImage("ponzo.jpg", cv.CV_LOAD_IMAGE_COLOR) # cimg is a OpenCV image
pimg = Image.fromstring("RGB", cv.GetSize(cimg), cimg.tostring()) # pimg is a PIL image
cimg2 = cv.CreateImageHeader(pimg.size, cv.IPL_DEPTH_8U, 3) # cimg2 is a OpenCV image
cv.SetData(cimg2, pimg.tostring()) Note: OpenCV stores color image in BGR format. So, the converted PIL image is also in BGR-format. The standard PIL image is stored in RGB format. # gray image
cimg = cv.LoadImage("ponzo.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) # cimg is a OpenCV image
pimg = Image.fromstring("L", cv.GetSize(cimg), cimg.tostring()) # img is a PIL image
cimg2 = cv.CreateImageHeader(pimg.size, cv.IPL_DEPTH_8U, 1) # img2 is a OpenCV image
cv.SetData(cimg2, pimg.tostring())

Convert between PIL image and NumPy ndarray

image = Image.open(“ponzo.jpg”)   # image is a PIL image
array = numpy.array(image) # array is a numpy array
image2 = Image.fromarray(array) # image2 is a PIL image

Convert between PIL image and PyOpenCV matrix

image = Image.open(“ponzo.jpg”)                  # image is a PIL image
mat = pyopencv.Mat.from_pil_image(image) # mat is a PyOpenCV matrix
image2 = mat.to_pil_image() # image2 is a PIL image

Convert between OpenCV image and NumPy ndarray

cimg = cv.LoadImage("ponzo.jpg", cv.CV_LOAD_IMAGE_COLOR)   # cimg is a OpenCV image
pimg = Image.fromstring("RGB", cv.GetSize(cimg), cimg.tostring()) # pimg is a PIL image
array = numpy.array(pimg) # array is a numpy array
pimg2 = cv.fromarray(array) # pimg2 is a OpenCV image

Python中PIL及Opencv转化的更多相关文章

  1. 关于python中PIL的安装

    python 的PIL安装是一件很蛋痛的事, 如果你要在python 中使用图型程序那怕只是将个图片从二进制流中存盘(例如使用Scrapy 爬网存图),那么都会使用到 PIL 这库,而这个库是出名的难 ...

  2. 【327】Python 中 PIL 实现图像缩放

    参考:Python 中使用PIL中的resize 进行缩放 参考:Python用Pillow(PIL)进行简单的图像操作(模糊.边缘增强.锐利.平滑等) 参考:廖雪峰 - Pillow 实现代码如下: ...

  3. python中PIL.Image和OpenCV图像格式相互转换

    PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy image = Image.open("plane.j ...

  4. python 中PIL.Image和OpenCV图像格式相互转换

    PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy   image = Image.open("plane ...

  5. python中PIL模块

    Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内.如open.save.conver.show-等功能. open类 Image ...

  6. 使用Python中PIL图形库进行截屏

    目的:通过使用Python的一个图形库PIL(Python Image Library)对屏幕进行截图 步骤: 1.下载PIL(路径)并安装 2.新建文件“截屏.py”,右键Edit with IDL ...

  7. python中PIL库的使用

    API参考 打开dos窗口,安装库: pip install pillow 很明显,图片有点大,咱们缩略一下: from PIL import Image im = Image.open(" ...

  8. python 用PIL Matplotlib处理图像的基本操作

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

  9. Python中Opencv和PIL.Image读取图片的差异对比

    近日,在进行深度学习进行推理的时候,发现不管怎么样都得不出正确的结果,再仔细和正确的代码进行对比了后发现原来是Python中不同的库读取的图片数组是有差异的. image = np.array(Ima ...

随机推荐

  1. Archlinux: 优化触摸板配置

    在逛 Archlinuxcn BBS 时看到这个帖子: fcitx 输入法看不到选词,上面键盘也不见了! 等待妹子的 依云 提到了 infinality, 并且给出了这个链接: fix-infinal ...

  2. 【Excle数据透视表】如何按照地区交替填充背景颜色

    现存在如下数据透视表 需要根据地区填充不同的背景颜色 步骤 选定数值区域→开始→条件格式→新建规则,出现如下窗口: 公式:=MOD(COUNT(1/(MATCH($A$4:$A4,$A$4:$A4,) ...

  3. 【BIEE】页面跳转以及跳转后返回

    报表开发过程中,我们经常会遇到这种问题:知道统计结果,然后根据统计结果去看明细数据 很多人可能首先想到的就是钻探,钻探是一种方法,但是不是唯一的办法,可以使用页面跳转完成. 下面举个例子 页面A 现在 ...

  4. Volatile 多线程中用到的关键字

    前言 不管是在面试还是实际开发中 volatile 都是一个应该掌握的技能. 首先来看看为什么会出现这个关键字. 内存可见性 由于 Java 内存模型(JMM)规定,所有的变量都存放在主内存中,而每个 ...

  5. <转>Windows平台下Makefile学习笔记(二)

    本文转自:http://blog.csdn.net/clever101/article/details/8286066 上次我们学习了怎么用Makefile编译一个控制台工程.这次我们学习一下如何使用 ...

  6. LVM卷组命令

    一般维护命令  #vgscan //检測系统中全部磁盘  #vgck [卷组名] //用于检查卷组中卷组描写叙述区域信息的一致性.  #vgdisplay [卷组名] //显示卷组的属性信息  #vg ...

  7. 在Eclipse中使用JUnit4进行单元测试(初级篇、中级篇、高级篇)

    本文转载自以下 初级篇: http://blog.csdn.net/andycpp/article/details/1327147 中级篇: http://blog.csdn.net/andycpp/ ...

  8. iOS scrollView中嵌套多个tableView处理方案

    项目中经常会有这样的需求,scrollView有个头部,当scrollView滚动的时候头部也跟着滚动,同时头部还有一个tab会锁定在某个位置,scrollView中可以放很多不同的view,这些vi ...

  9. 【JMeter4.0学习(九)】之定时器

    目录: 固定定时器 高斯随机定时器 附 一.固定定时器 1.添加线程组 2.添加固定定时器 3.添加HTTP请求 4.添加结果树以及运行  二.高斯随机定时器 1.添加线程组 2.添加高斯随机定时器 ...

  10. Android注解支持(Support Annotations) (转)

    原文地址:http://www.flysnow.org/2015/08/13/android-tech-docs-support-annotations.html 注解支持(Support Annot ...