PIL

Python图像处理库PIL的基本模块介绍

置顶 2016年02月11日 21:40:07 icamera0 阅读数:11116更多

所属专栏: Python图像处理库PIL从入门到精通

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/icamera0/article/details/50651926

PIL有如下几个模块:Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、ImageEnhance模块、ImageFile模块、ImageFileIO模块、ImageFilter模块、ImageFont模块、ImageGrab模块、ImageOps模块、ImagePath模块、ImageSequence模块、ImageStat模块、ImageTk模块、ImageWin模块、PSDraw模块。

1、  Image模块

Image模块提供了一个相同名称的类,即image类,用于表示PIL图像。这个模块还提供了一些函数,包括从文件中加载图像和创建新的图像。

Image模块是PIL中最重要的模块,它提供了诸多图像操作的功能,比如创建、打开、显示、保存图像等功能,合成、裁剪、滤波等功能,获取图像属性功能,如图像直方图、通道数等。

Image模块的使用如下:

>>>from PIL import Image

>>> im =Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>>im.getbands()

('R', 'G', 'B')

>>>im.mode

'RGB'

>>> im.show()

2、  ImageChops模块

ImageChops模块包含一些算术图形操作,叫做channel operations(“chops”)。这些操作可用于诸多目的,比如图像特效,图像组合,算法绘图等等。通道操作只用于8位图像(比如“L”模式和“RGB”模式)。

ImageChops模块的使用如下:

>>>from PIL import Image

>>> im = Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>> from PIL import ImageChops

>>> im_dup = ImageChops.duplicate(im)

>>> im_dup.mode

'RGB'

>>> im_diff = ImageChops.difference(im,im_dup)

>>> im_diff.show()

由于图像im_dup是im的复制过来的,所以它们的差为0,图像im_diff显示时为黑图。

3、  ImageCrackCode模块

ImageCrackCode模块允许用户检测和测量图像的各种特性。这个模块只存在于PIL Plus包中。

我目前安装的PIL中没有包含这个模块。

>>>from PIL import ImageCrackCode

Traceback (mostrecent call last):

  File "<pyshell#122>", line 1,in <module>

    from PIL import ImageCrackCode

ImportError:cannot import name ImageCrackCode

4、  ImageDraw模块

ImageDraw模块为image对象提供了基本的图形处理功能。例如,它可以创建新图像,注释或润饰已存在图像,为web应用实时产生各种图形。

ImageDraw模块的使用如下:

>>>from PIL import Image, ImageDraw

>>> im = Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>>draw = ImageDraw.Draw(im)

>>>draw.line((0,0) + im.size, fill = 128)

>>>draw.line((0, im.size[1], im.size[0], 0), fill=128)

>>>im.show()

>>> deldraw

>>>im.show()

在del draw前后显示出来的图像im是完全一样的,都是在原有图像上画了两条对角线。

5、  ImageEnhance模块

ImageEnhance模块包括一些用于图像增强的类。它们分别为Color类、Brightness类、Contrast类和Sharpness类。

ImageEnhance模块的使用如下:

>>>from PIL import Image, ImageEnhance

>>> im= Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>>enhancer = ImageEnhance.Brightness(im)

>>> im0= enhancer.enhance(0.5)

>>>im0.show()

图像im0的亮度为图像im的一半。

6、 ImageFile模块

ImageFile模块为图像打开和保存功能提供了相关支持功能。另外,它提供了一个Parser类,这个类可以一块一块地对一张图像进行解码(例如,网络联接中接收一张图像)。这个类的接口与标准的sgmllib和xmllib模块的接口一样。

ImageFile模块的使用如下:

>>>from PIL import ImageFile

>>> fp= open('D:\\Code\\Python\\test\\img\\1.jpg', "rb")

>>> p =ImageFile.Parser()

>>> s =fp.read(1024)

>>>p.feed(s)

>>> im= p.close()

Traceback (mostrecent call last):

  File "<pyshell#179>", line 1,in <module>

    im = p.close()

  File"C:\Python27\lib\site-packages\PIL\ImageFile.py", line 421, in close

    raise IOError("image wasincomplete")

IOError: imagewas incomplete

因为所打开图像大小大于1024个byte,所以报错:图像不完整。

>>>from PIL import ImageFile

>>> fp= open('D:\\Code\\Python\\test\\img\\1.jpg', "rb")

>>> p =ImageFile.Parser()

>>> s =fp.read(135429)

>>>p.feed(s)

>>> im= p.close()

>>>im.show()

此时,图像im显示的图像与之前使用image.open()函数打开的图像完全一样。

7、  ImageFileIO模块

ImageFileIO模块用于从一个socket或者其他流设备中读取一张图像。不赞成使用这个模块。在新的code中将使用ImageFile模块的Parser类来代替它。

8、  ImageFilter模块

ImageFilter模块包括各种滤波器的预定义集合,与Image类的filter方法一起使用。该模块包含这些图像增强的滤波器:BLUR,CONTOUR,DETAIL,EDGE_ENHANCE,EDGE_ENHANCE_MORE,EMBOSS,FIND_EDGES,SMOOTH,SMOOTH_MORE和SHARPEN。

ImageFilter模块的使用如下:

>>> from PIL import Image

>>> im = Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>>from PIL import ImageFilter

>>>imout = im.filter(ImageFilter.BLUR)

>>>imout.size

(800, 450)

>>>imout.show()

9、  ImageFont模块

ImageFont模块定义了一个同名的类,即ImageFont类。这个类的实例中存储着bitmap字体,需要与ImageDraw类的text方法一起使用。

PIL使用自己的字体文件格式存储bitmap字体。用户可以使用pilfont工具包将BDF和PCF字体描述器(Xwindow字体格式)转换为这种格式。

PIL Plus包中才会支持矢量字体。

ImageFont模块的使用如下:

>>> from PIL importImage, ImageDraw, ImageFont

>>>im = Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>> draw= ImageDraw.Draw(im)

>>>font_arial = ImageFont.load("arial.pil")

>>> text= “Hello”

>>> draw.text((100,100), text, font_arial)

>>> deldraw

>>>im.show()

图像im坐标(100,100)位置将出现“Hello”字样。

10、             ImageGrab模块

(New in1.1.3)ImageGrab模块用于将屏幕上的内容拷贝到一个PIL图像内存中。当前的版本只在windows操作系统上可以工作。

ImageGrab模块的使用如下:

>>>from PIL import ImageGrab

>>> im= ImageGrab.grab()

>>>im.show()

图像im显示出笔记本当前的窗口内容。

11、             ImageOps模块

(New in1.1.3)ImageOps模块包括一些“ready-made”图像处理操作。它可以完成直方图均衡、裁剪、量化、镜像等操作。这个模块somewhat experimental,大多数操作只工作在L和RGB图像上。

ImageOps模块的使用如下:

>>>from PIL import Image, ImageOps

>>> im= Image.open('D:\\Code\\Python\\test\\img\\1.jpg')

>>>im_flip = ImageOps.flip(im)

>>> im_flip.show()

图像im_flip为图像im垂直方向的镜像。

12、             ImagePath模块

ImagePath模块用于存储和操作二维向量数据。Path对象将被传递到ImageDraw模块的方法中。

ImagePath模块的使用如下:

>>>from PIL import ImagePath

>>>path = ImagePath.Path([(0,0),(1,1),(2,2),(3,3)])

>>>path.compact()

0

>>>path.getbbox()

(0.0, 0.0, 3.0,3.0)

>>>path.tolist()

[(0.0, 0.0),(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)]

13、             ImageSequence模块

ImageSequence模块包括一个wrapper类,它为图像序列中每一帧提供了迭代器。

ImageSequence模块的使用如下:

>>>from PIL import Image, ImageSequence

>>> im= Image.open('D:\\Code\\Python\\test\\img\\1.gif')

>>>iter = ImageSequence.Iterator(im)

>>>iter[0].show()

>>> iter[10].show()

后面两次show()函数调用,分别显示第1张和第11张图像。

14、             ImageStat模块

ImageStat模块计算一张图像或者一张图像的一个区域的全局统计值。

ImageStat模块的使用如下:

>>>from PIL import Image, ImageStat

>>> im= Image.open("D:\\Code\\Python\\test\\img\\1.jpg")

>>>im.show()

>>>im_stat = ImageStat.Stat(im)

>>>im_stat.count

[360000, 360000,360000]

>>>im_stat.sum

[41690682.0,47796024.0, 48550206.0]

15、             ImageTk模块

ImageTk模块用于创建和修改BitmapImage和PhotoImage对象中的Tkinter。

ImageTk模块的使用如下:

>>>from PIL import Image, ImageTk

>>> im= Image.open("D:\\Code\\Python\\test\\img\\1.jpg")

>>>im.show()

>>> photo= ImageTk.PhotoImage(im)

Traceback (mostrecent call last):

  File "<pyshell#19>", line 1,in <module>

    photo = ImageTk.PhotoImage(im)

  File"C:\Python27\lib\site-packages\PIL\ImageTk.py", line 112, in __init__

    self.__photo = tkinter.PhotoImage(**kw)

  File"C:\Python27\lib\lib-tk\Tkinter.py", line 3366, in __init__

    Image.__init__(self, 'photo', name, cnf,master, **kw)

  File"C:\Python27\lib\lib-tk\Tkinter.py", line 3303, in __init__

    raise RuntimeError, 'Too early to createimage'

RuntimeError:Too early to create image

看来我当前的版本不能正常使用这个模块。

16、             ImageWin模块

ImageWin模块用于在windows95/98,NT,2000及以后的系统上创建和显示图像。

17、             PSDraw模块。

PSDraw模块为Postscript打印机提供基本的打印支持。用户可以通过这个模块打印字体,图形和图像。

股票“三不卖七不买“6字黄金口诀,只买井喷股 赢数百万身家!中资 · 顶新

猜你喜欢

转载自blog.csdn.net/weixin_38859557/article/details/88422522
PIL