300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Python图像处理库(PIL)--Image ImageDraw等基本模块介绍

Python图像处理库(PIL)--Image ImageDraw等基本模块介绍

时间:2019-11-21 00:32:57

相关推荐

Python图像处理库(PIL)--Image ImageDraw等基本模块介绍

常用操作

合成 Image.blend(i1,i2,a)/posite(i1,i2,mask)

缩略图 thumbnail(size,filter=None)

Modifies in-place,Preserves aspect ratio

>>> myImage.thumbnail ((128, 128), Image.ANTIALIAS)

剪切 crop(bbox)

>>> bounds = (100, 100, 400, 400)

>>> cutoutIm = myImage.crop (bounds)

粘贴 paste(i2,where,mask=None)/paste(color,box=None,mask=None)

旋转 rotate(theta)

rotated around its center

翻转旋转 transpose(method)

ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)

>>> fixedIm = myImage.transpose (ROTATE_90)

The Image Module

The Image module provides

a class with the same name which is used torepresent a PIL image.The module also provides a number offactory functions(including functions toload images from files, and tocreate new images)

图像对象 Image– from file or newly created

所有的图片操作必须有一个操作对象,例如Pil提供open(filename)进行这个过程,此后,一切关于图片的操作均基于这个对象。有以下几种创建image对象的方式:

1 Image.open(f)

>>> import Image

>>>

>>> Im = Image.open("lena.jpg")

>>> print Im.mode,Im.size,Im.format

RGB (256, 256) JPEG

>>> Im.show()

如果文件不能打开,会抛出IOError异常。

可以查看image对象的format,mode,size,palette,info几个属性。

调用im.show()会在图片查看工具中显示当前操作的image对象。

标准版本的show方法的实现不太高效,因为它先把image保存到一个临时文件,然后调用xy工具来显示图像。如果你没有安装xy,那么它就无法工作了。不过如果它可以工作,倒还是非常方便用来debug和测试。

2 Image.new(mode,size,color=None)

color的默认值是黑色,这里我们新建一个红色的图像。

>>> newIm = Image.new (“RGBA”, (640, 480), (255, 0, 0)) #新建一个image对象creating images from scratch

3 Image.blend(i1,i2,a) -- (p1 x (1 - a) + p2 x a)

选一张灰度图(L)做背景,和雷娜图(RGB)做blend操作

>>> Im2 = Image.open("background.jpg").convert(Im.mode)

>>> Im2 = Im2.resize(Im.size)

>>> Im2.show()

>>>

>>> img = Image.blend(Im,Im2,0.2)

>>> img.show()

操作完毕后save(filename)用以保存这个临时的image对象img到硬盘。

4 posite(i1,i2,mask) --equal-sized images i1 ,i2 and mask("1", "L", or "RGBA") (p1 x (1 - m) + p2 x m)

5 Image.eval(f,i) -- applying a function f to each pixel of image i

6 Image.merge(mode,bandList) --Creates a multi-band image from a sequence of single-band images of equal size

以下是Image对象的全部方法:

The ImageDraw Module

支持2D图像The ImageDraw module provide basic 2D graphics support for Image objects.

It can for example be used to

create new images,annotate or retouch existing images, and togenerate graphics on the fly for web use.

For a more advanced drawing library for PIL, seeThe aggdraw Module.

创建绘画对象ImageDraw module creates drawing surface for image

import Image, ImageDraw

im = Image.open(“vacation.jpeg")

drawSurface = ImageDraw.Draw(im)

基本绘画操作Basic methods of drawing surface

弧/弦/扇形 chord arc pieslice (bbox, strtAng, endAng)椭圆 ellipse (bbox)线段/多段线 line (L) draw.line(((60,60),(90,60), (90,90), (60,90), (60,60))) #draw a square点 point (xy) #单像素点很小看不清,实际中可用实心小圆代替多边形 polygon (L) draw.polygon([(60,60), (90,60), (90,90), (60,90)]) #draw a square矩形 rectangle (bbox) # first coord属于矩形, second coord不属于文字 text(xy,message,font=None) 绘制文字message,文本区域左上角坐标为xy

drawable.text((10, 10), "Hello", fill=(255,0,0), font=None)文字大小 textsize(message,font=None) 给定文字message,返回所占像素(width,height)

可选参数Common optional args for these methods

fill=fillColor outline=outlineColor

矢量字体支持TrueType Font support

import ImageFont

ttFont = ImageFont.truetype (“arial.ttf”, 16)

drawable.text ((10, 10), “Hello”, fill=(255,0,0), font=ttFont)

例子:Draw a Grey Cross Over an Image

import Image, ImageDrawim = Image.open("lena.pgm") # Creates an object that can be used to draw in the given image.draw = ImageDraw.Draw(im)# draw.line(xy, options) => Draws a line between the coordinates in the xy list.# The coordinate list can be any sequence object containing either 2-tuples [ (x, y), ... ]# or numeric values [ x, y, ... ].# The fill option gives the color to use for the line.draw.line((0, 0) + im.size, fill=128)draw.line((0, im.size[1], im.size[0], 0), fill=128)del draw# write to stdoutim.save(sys.stdout, "PNG")

The ImageChops module

a number of arithmetical image operations, calledchannel operations("chops" 通道操作).

These can be used for various purposes, including special effects 特殊效果, image compositions 图像合成, algorithmic painting 算法绘画, and more.

At this time, channel operations areonly implemented for 8-bit images(e.g. "L" and "RGB").

例子:比较两幅图像

Exact Comparison:

The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding box of the non-zero regions in this image. If the images are identical, all pixels in the difference image are zero, and the bounding box function returnsNone.

import ImageChopsdef equal(im1, im2):return ImageChops.difference(im1, im2).getbbox() is None

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses thedifferencefunction, and then calculates the RMS value from the histogram of the resulting image.

RMS Difference:

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses thedifferencefunction, and then calculates the RMS value from the histogram of the resulting image.

# Example: File: imagediff.pyimport ImageChopsimport math, operatordef rmsdiff(im1, im2):"Calculate the root-mean-square difference between two images"h = ImageChops.difference(im1, im2).histogram()# calculate rmsreturn math.sqrt(reduce(operator.add,map(lambda h, i: h*(i**2), h, range(256))) / (float(im1.size[0]) * im1.size[1]))

本文转自:/wei-li/archive//04/19/2456725.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。