转载: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. android中依据不同分辨率dp和px的相互转算

    public class PxAndDp { /** * 依据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context ...

  2. static 关键字的使用,静态和非静态类的区别

    直接以一个例子说明: using System; using System.Collections.Generic; using System.Diagnostics; using System.IO ...

  3. Python Windows文件操作

    获得目录和文件名 os.getenv()获取环境变量 os.putenv()设置环境变量 os.getcwd() 获得当前目录 os.chdir(‘要设置的当前目录’) os.listdir() 返回 ...

  4. Selenium3.X 与 Javascript (Nodejs)

    传送门 # 官网网站 http://docs.seleniumhq.org/download/ # API DOC http://goo.gl/hohAut # 慕课网教程http://www.imo ...

  5. nginx 根据参数选择文档根目录

    server  {    listen       80;    server_name  testmanage.h5.91wan.com;    index index.html index.htm ...

  6. POJ 3252 Round Numbers 数学题解

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  7. window mysql安装步骤

    window安装mysql(本人系统win10 64位 安装mysql-5.7.10-winx64) 1. 官网下载mysql zip安装包,然后解压到你想安装的目录,假设解压的目录是P:\mysql ...

  8. HDFS源码分析数据块校验之DataBlockScanner

    DataBlockScanner是运行在数据节点DataNode上的一个后台线程.它为所有的块池管理块扫描.针对每个块池,一个BlockPoolSliceScanner对象将会被创建,其运行在一个单独 ...

  9. PHP压缩上传图片

    最近手上的项目页面要显示很多图片,虽然用了jQuery的lazyload,但是效果并没理想,滑动到一个区域还要比较长的时间图片才完全显示出来.于是想着将上传上去的900KB+压缩备份一份缩略图. PH ...

  10. hadoop生态系统学习之路(八)hbase与hive的数据同步以及hive与impala的数据同步

    在之前的博文中提到,hive的表数据是能够同步到impala中去的. 一般impala是提供实时查询操作的,像比較耗时的入库操作我们能够使用hive.然后再将数据同步到impala中.另外,我们也能够 ...