转载: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. 在HTML页面中实现一个简单的Tab

    参考:http://blog.sina.com.cn/s/blog_6cccb1630100m23i.html HTML页面代码如下: <!DOCTYPE html PUBLIC "- ...

  2. Activity和Service交互之bindService(回调更新UI)

    一.回调接口 public interface OnProgressListener { void onProgress(int progress); } 二.Service代码 public cla ...

  3. ionic准备之angular基础——模板引入(7)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Phalcon 上下文编码(Contextual Escaping)

    站点及其他B/S应用极易受到 XSS 攻击,虽然PHP提供了转义功能.在某些情况下依旧不够安全.在Phalcon中 Phalcon\Escaper 提供了上下文转义功能,这个模块是由C语言实现的, 这 ...

  5. STL学习笔记(迭代器类型)

    迭代器类型 迭代器是一种“能够遍历某个序列内的所有元素”的对象.它可以透过与一般指针一致的接口来完成自己的工作. 不同的迭代器具有不同的”能力“(行进和存取能力) Input迭代器 Input迭代器只 ...

  6. SubVersion(SVN)的安装配置使用

    一. SubVersion服务器端安装 安装软件:Setup-Subversion-1.6.4.msi,下载地址:http://subversion.tigris.org/servlets/Proje ...

  7. jdk1.6 升级到 jdk1.7

    将 jdk1.6 升级到 jdk1.7 下载jdk-7u11-linux-x64.tar.gz   tar zxvf  jdk-7u11-linux-x64.tar.gz mkdir   /usr/l ...

  8. PS 流格式解析(转)

    对于PS流,最近因为工作需要,所以MPEG2中的PS流格式和解包过程进行了学习. 首先我们需要知道PS包流格式是怎么样的: 针对H264 做如下PS 封装:每个IDR NALU 前一般都会包含SPS. ...

  9. Intent获取Activity返回值

    /* Intent获取Activity返回值* 三步:* 子Activity关闭后的返回值处理函数,requestCode是子Activity返回的请求码,与页面顶端的两个请求码相匹配,resultC ...

  10. Linux Mint (应用软件— 虚拟机:Virtualbox)

    近期想自己折腾一下Linux系统本身.比方Linux裁减或者移植.裁减或者移植Linux是一件麻烦的事情.而且出错后会影响到当前的系统.怎样才干不影响当前机器上的系统呢,于是便想到了虚拟机.在当前系统 ...