Python 之 使用 PIL 库做图像处理(pillow+ImageDraw)
1. 简介。
图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴。PIL (Python Imaging Library)是 Python 中最常用的图像处理库,目前版本为 1.1.7,我们可以 在这里 下载学习和查找资料。
Image 类是 PIL 库中一个非常重要的类,通过这个类来创建实例可以有直接载入图像文件,读取处理过的图像和通过抓取的方法得到的图像这三种方法。
2. 使用。
导入 Image 模块。然后通过 Image 类中的 open 方法即可载入一个图像文件。如果载入文件失败,则会引起一个 IOError ;若无返回错误,则 open 函数返回一个 Image 对象。现在,我们可以通过一些对象属性来检查文件内容,即:
这里有三个属性,我们逐一了解。
format : 识别图像的源格式,如果该文件不是从文件中读取的,则被置为 None 值。
size : 返回的一个元组,有两个元素,其值为象素意义上的宽和高。
mode : RGB(true color image),此外还有,L(luminance),CMTK(pre-press image)。
现在,我们可以使用一些在 Image 类中定义的方法来操作已读取的图像实例。比如,显示最新载入的图像:
输出原图:
3. 函数概貌。
3.1 Reading and Writing Images : open( infilename ) , save( outfilename )
3.2 Cutting and Pasting and Merging Images :
crop() : 从图像中提取出某个矩形大小的图像。它接收一个四元素的元组作为参数,各元素为(left, upper, right, lower),坐标系统的原点(0, 0)是左上角。
paste() :
merge() :
其效果图为:
旋转一幅图片:
3.3.1 简单的几何变换。
各个调整之后的图像为:
图片1:
图片2:
图片3:
图片4:
3.3.2 色彩空间变换。
convert() : 该函数可以用来将图像转换为不同色彩模式。
3.3.3 图像增强。
Filters : 在 ImageFilter 模块中可以使用 filter 函数来使用模块中一系列预定义的增强滤镜。
即我们常见到的动态图,最常见的后缀为 .gif ,另外还有 FLI / FLC 。PIL 库对这种动画格式图也提供了一些基本的支持。当我们打开这类图像文件时,PIL 自动载入图像的第一帧。我们可以使用 seek 和 tell 方法在各帧之间移动。
3.5 更多关于图像文件的读取。
最基本的方式:im = Image.open("filename")
类文件读取:fp = open("filename", "rb"); im = Image.open(fp)
字符串数据读取:import StringIO; im = Image.open(StringIO.StringIO(buffer))
从归档文件读取:import TarIO; fp = TarIo.TarIO("Image.tar", "Image/test/lena.ppm"); im = Image.open(fp)
基本的 PIL 目前就练习到这里。其他函数的功能可点击 这里 进一步阅读。
-------------------------------------------------------------------------------------------------------------------------------------------------
Pillow是Python里的图像处理库(PIL:Python Image Library),提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存、图像显示、格式转换以及基本的图像处理操作等。
1)使用 Image 类
PIL最重要的类是 Image class, 你可以通过多种方法创建这个类的实例;你可以从文件加载图像,或者处理其他图像, 或者从 scratch 创建。
要从文件加载图像,可以使用open( )函数,在Image模块中:
1
2
|
>>> from PIL import Image
>>> im = Image.open("E:/photoshop/1.jpg")
|
加载成功后,将返回一个Image对象,可以通过使用示例属性查看文件内容:
1
2
3
|
>>>print(im.format,im.size,im.mode)
('JPEG',(600,351),'RGB')
>>>
|
format 这个属性标识了图像来源。如果图像不是从文件读取它的值就是None。size属性是一个二元tuple,包含width和height(宽度和高度,单位都是px)。 mode 属性定义了图像bands的数量和名称,以及像素类型和深度。常见的modes 有 “L” (luminance) 表示灰度图像, “RGB” 表示真彩色图像, and “CMYK” 表示出版图像。
如果文件打开错误,返回 IOError 错误。
只要你有了 Image 类的实例,你就可以通过类的方法处理图像。比如,下列方法可以显示图像:
1 |
im.show()
|
2)读写图像
PIL 模块支持大量图片格式。使用在 Image 模块的 open() 函数从磁盘读取文件。你不需要知道文件格式就能打开它,这个库能够根据文件内容自动确定文件格式。要保存文件,使用 Image 类的 save() 方法。保存文件的时候文件名变得重要了。除非你指定格式,否则这个库将会以文件名的扩展名作为格式保存。
加载文件,并转化为png格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
"Python Image Library Test"
fromPILimportImage
importos
importsys
forinfileinsys.argv[1:]:
f,e=os.path.splitext(infile)
outfile=f+".png"
ifinfile!=outfile:
try:
Image.open(infile).save(outfile)
exceptIOError:
print("Cannot convert",infile)
|
save() 方法的第二个参数可以指定文件格式。
3)创建缩略图
缩略图是网络开发或图像软件预览常用的一种基本技术,使用Python的Pillow图像库可以很方便的建立缩略图,如下:
1
2
3
4
5
6
7
|
# create thumbnail
size = (128,128)
for infile in glob.glob("E:/photoshop/*.jpg"):
f, ext = os.path.splitext(infile)
img = Image.open(infile)
img.thumbnail(size,Image.ANTIALIAS)
img.save(f+".thumbnail","JPEG")
|
上段代码对photoshop下的jpg图像文件全部创建缩略图,并保存,glob模块是一种智能化的文件名匹配技术,在批图像处理中经常会用到。
注意:Pillow库不会直接解码或者加载图像栅格数据。当你打开一个文件,只会读取文件头信息用来确定格式,颜色模式,大小等等,文件的剩余部分不会主动处理。这意味着打开一个图像文件的操作十分快速,跟图片大小和压缩方式无关。
4)图像的剪切、粘贴与合并操作
Image 类包含的方法允许你操作图像部分选区,PIL.Image.Image.crop 方法获取图像的一个子矩形选区,如:
1
2
3
4
|
# crop, paste and merge
im=Image.open("E:/photoshop/lena.jpg")
box=(100,100,300,300)
region=im.crop(box)
|
矩形选区有一个4元元组定义,分别表示左、上、右、下的坐标。这个库以左上角为坐标原点,单位是px,所以上诉代码复制了一个 200×200 pixels 的矩形选区。这个选区现在可以被处理并且粘贴到原图。
1
2
|
region = region.transpose(Image.ROTATE_180)
im.paste(region, box)
|
当你粘贴矩形选区的时候必须保证尺寸一致。此外,矩形选区不能在图像外。然而你不必保证矩形选区和原图的颜色模式一致,因为矩形选区会被自动转换颜色。
5)分离和合并颜色通道
对于多通道图像,有时候在处理时希望能够分别对每个通道处理,处理完成后重新合成多通道,在Pillow中,很简单,如下:
1
2
|
r,g,b=im.split()
im=Image.merge("RGB",(r,g,b))
|
对于split( )函数,如果是单通道的,则返回其本身,否则,返回各个通道。
6)几何变换
对图像进行几何变换是一种基本处理,在Pillow中包括resize( )和rotate( ),如用法如下:
1
2
|
out = im.resize((128,128))
out = im.rotate(45) # degree conter-clockwise
|
其中,resize( )函数的参数是一个新图像大小的元祖,而rotate( )则需要输入顺时针的旋转角度。在Pillow中,对于一些常见的旋转作了专门的定义:
1
2
3
4
5
|
out=im.transpose(Image.FLIP_LEFT_RIGHT)
out=im.transpose(Image.FLIP_TOP_BOTTOM)
out=im.transpose(Image.ROTATE_90)
out=im.transpose(Image.ROTATE_180)
out=im.transpose(Image.ROTATE_270)
|
7)颜色空间变换
在处理图像时,根据需要进行颜色空间的转换,如将彩色转换为灰度:
1
2
|
cmyk = im.convert("CMYK")
gray = im.convert("L")
|
8)图像滤波
图像滤波在ImageFilter 模块中,在该模块中,预先定义了很多增强滤波器,可以通过filter( )函数使用,预定义滤波器包括:
BLUR、CONTOUR、DETAIL、EDGE_ENHANCE、EDGE_ENHANCE_MORE、EMBOSS、FIND_EDGES、SMOOTH、SMOOTH_MORE、SHARPEN。其中BLUR就是均值滤波,CONTOUR找轮廓,FIND_EDGES边缘检测,使用该模块时,需先导入,使用方法如下:
1
2
3
4
5
6
7
8
9
10
|
fromPILimportImageFilter
imgF=Image.open("E:/photoshop/lena.jpg")
outF=imgF.filter(ImageFilter.DETAIL)
conF=imgF.filter(ImageFilter.CONTOUR)
edgeF=imgF.filter(ImageFilter.FIND_EDGES)
imgF.show()
outF.show()
conF.show()
edgeF.show()
|
除此以外,ImageFilter模块还包括一些扩展性强的滤波器:
class PIL.ImageFilter.GaussianBlur(radius=2)
- Gaussian blur filter.
参数: radius – Blur radius. class PIL.ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)
- Unsharp mask filter.
See Wikipedia’s entry on digital unsharp masking for an explanation of the parameters.
-
class PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0)
- Create a convolution kernel. The current version only supports 3×3 and 5×5 integer and floating point kernels.
In the current version, kernels can only be applied to “L” and “RGB” images.
参数: - size – Kernel size, given as (width, height). In the current version, this must be (3,3) or (5,5).
- kernel – A sequence containing kernel weights.
- scale – Scale factor. If given, the result for each pixel is divided by this value. the default is the sum of the kernel weights.
- offset – Offset. If given, this value is added to the result, after it has been divided by the scale factor.
-
class PIL.ImageFilter.RankFilter(size, rank)
- Create a rank filter. The rank filter sorts all pixels in a window of the given size, and returns therank‘th value.
参数: - size – The kernel size, in pixels.
- rank – What pixel value to pick. Use 0 for a min filter, size * size / 2 for a median filter, size * size - 1 for a max filter, etc.
-
class PIL.ImageFilter.MedianFilter(size=3)
- Create a median filter. Picks the median pixel value in a window with the given size.
参数: size – The kernel size, in pixels.
- class PIL.ImageFilter.MinFilter(size=3)
- Create a min filter. Picks the lowest pixel value in a window with the given size.
参数: size – The kernel size, in pixels.
- class PIL.ImageFilter.MaxFilter(size=3)
- Create a max filter. Picks the largest pixel value in a window with the given size.
参数: size – The kernel size, in pixels.
- class PIL.ImageFilter.ModeFilter(size=3)
- Create a mode filter. Picks the most frequent pixel value in a box with the given size. Pixel values that occur only once or twice are ignored; if no pixel value occurs more than twice, the original pixel value is preserved.
参数: size – The kernel size, in pixels. 更多详细内容可以参考:PIL/ImageFilter
9)图像增强
图像增强也是图像预处理中的一个基本技术,Pillow中的图像增强函数主要在ImageEnhance模块下,通过该模块可以调节图像的颜色、对比度和饱和度和锐化等:
1
2
3
4
5
|
fromPILimportImageEnhance
imgE=Image.open("E:/photoshop/lena.jpg")
imgEH=ImageEnhance.Contrast(imgE)
imgEH.enhance(1.3).show("30% more contrast")
|
图像增强:
- class PIL.ImageEnhance.Color(image)
- Adjust image color balance.
This class can be used to adjust the colour balance of an image, in a manner similar to the controls on a colour TV set. An enhancement factor of 0.0 gives a black and white image. A factor of 1.0 gives the original image.
- class PIL.ImageEnhance.Contrast(image)
- Adjust image contrast.
This class can be used to control the contrast of an image, similar to the contrast control on a TV set. An enhancement factor of 0.0 gives a solid grey image. A factor of 1.0 gives the original image.
- class PIL.ImageEnhance.Brightness(image)
- Adjust image brightness.
This class can be used to control the brighntess of an image. An enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the original image.
- class PIL.ImageEnhance.Sharpness(image)
- Adjust image sharpness.
This class can be used to adjust the sharpness of an image. An enhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives the original image, and a factor of 2.0 gives a sharpened image.
图像增强的详细内容可以参考:PIL/ImageEnhance
除了以上介绍的内容外,Pillow还有很多强大的功能:
PIL.Image.alpha_composite(im1, im2)
PIL.Image.blend(im1, im2, alpha)
PIL.Image.composite(image1, image2, mask)
PIL.Image.eval(image, *args)
PIL.Image.fromarray(obj, mode=None)
PIL.Image.frombuffer(mode, size, data, decoder_name=’raw’, *args)
想了解更多,请参考:Image Module和 Pillow Reference
ImageDraw模块提供了图像对象的简单2D绘制。用户可以使用这个模块创建新的图像,注释或润饰已存在图像,为web应用实时产生各种图形。
PIL中一个更高级绘图库见The aggdraw Module。
一、ImageDraw模块的概念
1、 Coordinates
绘图接口使用和PIL一样的坐标系统,即(0,0)为左上角。
2、 Colours
为了指定颜色,用户可以使用数字或者元组,对应用户使用函数Image.new或者Image.putpixel。对于模式为“1”,“L”和“I”的图像,使用整数。对于“RGB”图像,使用整数组成的3元组。对于“F”图像,使用整数或者浮点数。
对于调色板图像(模式为“P”),使用整数作为颜色索引。在1.1.4及其以后,用户也可以使用RGB 3元组或者颜色名称。绘制层将自动分配颜色索引,只要用户不绘制多于256种颜色。
3、 Colours Names
在PIL 1.1.4及其以后的版本,用户绘制“RGB”图像时,可以使用字符串常量。PIL支持如下字符串格式:
A、 十六进制颜色说明符,定义为“#rgb”或者“#rrggbb”。例如,“#ff0000”表示纯红色。
B、 RGB函数,定义为“rgb(red, green, blue)”,变量red、green、blue的取值为[0,255]之间的整数。另外,颜色值也可以为[0%,100%]之间的三个百分比。例如,“rgb(255, 0, 0)”和“rgb(100%, 0%, 0%)”都表示纯红色。
C、 HSL(Hue-Saturation-Lightness)函数,定义为“hsl(hue,saturation%, lightness%)”,变量hue为[0,360]一个角度表示颜色(red=0, green=120, blue=240),变量saturation为[0%,100%]之间的一个值(gray=0%,full color=100%),变量lightness为[0%,100%]之间的一个值(black=0%, normal=50%, white=100%)。例如,“hsl(0,100%, 50%)”为纯红色。
D、 通用HTML颜色名称,ImageDraw模块提供了140个标准颜色名称,Xwindow系统和大多数web浏览器都支持这些颜色。颜色名称对大小写不敏感。例如,“red”和“Red”都表示纯红色。
4、 Fonts
PIL可以使用bitmap字体或者OpenType/TrueType字体。
Bitmap字体被存储在PIL自己的格式中,它一般包括两个文件,一个叫.pil,它包含字体的矩阵,另一个通常叫做.pbm,它包含栅格数据。
在ImageFont模块中,使用函数load()加载一个bitmap字体。
在ImageFont模块中,使用函数truetype()加载一个OpenType/TrueType字体。注意:这个函数依赖于第三方库,而且并不是在所有的PIL版本中都有效。
(IronPIL)加载内置的字体,使用ImageFont模块的Font()结构函数即可。
二、ImageDraw模块的函数
1、 Draw
定义:Draw(image) ⇒ Draw instance
含义:创建一个可以在给定图像上绘图的对象。
(IronPIL)用户可以使用ImageWin模块的HWND或者HDC对象来代替图像。这个允许用户直接在屏幕上绘图。
注意:图像内容将会被修改。
例子:
>>> fromPIL import Image, ImageDraw
>>> im01 =Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>> draw =ImageDraw.Draw(im01)
>>> draw.line((0,0) +im01.size, fill=128)
>>> draw.line((0,im01.size[1], im.size[0], 0), fill = 128)
>>> im01.show()
>>> del draw
在图像01上绘制了两条灰色的对角线,如下图:
三、ImageDraw模块的方法
1、 Arc
定义:draw.arc(xy, start, end, options)
含义:在给定的区域内,在开始和结束角度之间绘制一条弧(圆的一部分)。
变量options中fill设置弧的颜色。
例子:
>>> from PIL import Image,ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>> draw =ImageDraw.Draw(im01)
>>> draw.arc((0,0,200,200),0, 90, fill = (255,0,0))
>>>draw.arc((300,300,500,500), 0, -90, fill = (0,255,0))
>>> draw.arc((200,200,300,300),-90, 0, fill = (0,0,255))
>>> im01.show()
>>> del draw
注意:变量xy是需要设置一个区域,此处使用4元组,包含了区域的左上角和右下角两个点的坐标。此PIL版本中,变量options不能使用outline,会报错:“TypeError: arc() got an unexpected keyword argument 'outline'”;所以此处应该使用fill。
在图像01上(0,0,200,200)区域使用红色绘制了90度的弧,(300,300,500,500)区域使用绿色绘制了270度的弧,(200,200,300,300)区域使用蓝色绘制了90度的弧。这些弧都是按照顺时针方向绘制的。变量start/end的0度为水平向右,沿着顺时针方向依次增加。绘制后的图像01如下图:
2、 Bitmap
定义:draw.bitmap(xy, bitmap, options)
含义:在给定的区域里绘制变量bitmap所对应的位图,非零部分使用变量options中fill的值来填充。变量bitmap位图应该是一个有效的透明模板(模式为“1”)或者蒙版(模式为“L”或者“RGBA”)。
这个方法与Image.paste(xy, color, bitmap)有相同的功能。
例子:
>>> from PIL import Image,ImageDraw
>>> im01 =Image.open("D:\\Code\\python\\test\\img\\test01.jpg")
>>> im02 =Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")
>>> im =im02.resize(300,200)>>> im.size
(300, 200)
>>> r,g,b =im.split()
>>> draw =ImageDraw.Draw(im01)
>>>draw.bitmap((0,0), r, fill = (255,0,0))
>>>draw.bitmap((300,200), g, fill = (0,255,0))
>>>draw.bitmap((600,400), b, fill = (0,0,255))
>>> im01.show()
变量xy是变量bitmap对应位图起始的坐标值,而不是一个区域。
图像im01如下:
3、 Chord
定义:draw.chord(xy,start, end, options)
含义:和方法arc()一样,但是使用直线连接起始点。
变量options的outline给定弦轮廓的颜色。Fill给定弦内部的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>> im01 =Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>> draw =ImageDraw.Draw(im01)
>>> draw.chord((0,0,200,200),0, 90, fill = (255,0,0))
>>> draw.chord((300,300,500,500), 0, -90, fill = (0,255,0))
>>> draw.chord((200,200,300,300), -90, 0, fill = (0,0,255))
>>> im01.show()
图像im01如下:
4、 Ellipse
定义:draw.ellipse(xy,options)
含义:在给定的区域绘制一个椭圆形。
变量options的outline给定椭圆形轮廓的颜色。Fill给定椭圆形内部的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>> im01 =Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>> draw =ImageDraw.Draw(im01)
>>> draw.ellipse((0,0, 200, 200), fill = (255, 0, 0))
>>> draw.ellipse((200,200,400,300),fill = (0, 255, 0))
>>>draw.ellipse((400,400,800,600), fill = (0, 0, 255))
>>> im01.show()
图像im01如下:
5、 Line
定义:draw.line(xy,options)
含义:在变量xy列表所表示的坐标之间画线。
坐标列表可以是任何包含2元组[(x,y),…]或者数字[x,y,…]的序列对象。它至少包括两个坐标。
变量options的fill给定线的颜色。
(New in 1.1.5)变量options的width给定线的宽度。注意线连接不是很好,所以多段线段连接不好看。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>>draw.line([(0,0),(100,300),(200,500)], fill = (255,0,0), width = 5)
>>>draw.line([50,10,100,200,400,300], fill = (0,255,0), width = 10)
>>>im01.show()
图像im01如下:
6、 Pieslice
定义:draw.pieslice(xy,start, end, options)
含义:和方法arc()一样,但是在指定区域内结束点和中心点之间绘制直线。
变量options的fill给定pieslice内部的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>>draw.pieslice((0,0,100,200), 0, 90, fill = (255,0,0))
>>>draw.pieslice((100,200,300,400), 0, -90, fill = (0,255,0))
>>> im01.show()
图像im01如下:
7、 Point
定义:draw.point(xy,options)
含义:在给定的坐标点上画一些点。
坐标列表是包含2元组[(x,y),…]或者数字[x,y,…]的任何序列对象。
变量options的fill给定点的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>> draw.point([(0,0),(100,150), (110, 50)], fill = (255, 0, 0))
>>> draw.point([0,10,100,110, 210, 150], fill = (0, 255, 0))
>>>im01.show()
图像im01上在对应的坐标点上会有红色/绿色的点,每个点只占一个像素点。图像如下:
8、 Polygon
定义:draw.polygon(xy,options)
含义:绘制一个多边形。
多边形轮廓由给定坐标之间的直线组成,在最后一个坐标和第一个坐标间增加了一条直线,形成多边形。
坐标列表是包含2元组[(x,y),…]或者数字[x,y,…]的任何序列对象。它最少包括3个坐标值。
变量options的fill给定多边形内部的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>> draw.polygon([(0,0),(100,150), (210, 350)], fill = (255, 0, 0))
>>> draw.polygon([300,300,100,400, 400, 400], fill = (0, 255, 0))
>>>im01.show()
图像01如下:
9、 Rectangle
定义:draw.rectangle(box,options)
含义:绘制一个长边形。
变量box是包含2元组[(x,y),…]或者数字[x,y,…]的任何序列对象。它应该包括2个坐标值。
注意:当长方形没有没有被填充时,第二个坐标对定义了一个长方形外面的点。
变量options的fill给定长边形内部的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>>draw.rectangle((0,0,100,200), fill = (255,0,0))
>>> draw.rectangle([100,200,300,500],fill = (0,255,0))
>>>draw.rectangle([(300,500),(600,700)], fill = (0,0,255))
>>>im01.show()
图像01如下:
10、 Text
定义:draw.text(position,string, options)
含义:在给定的位置绘制一个字符创。变量position给出了文本的左上角的位置。
变量option的font用于指定所用字体。它应该是类ImangFont的一个实例,使用ImageFont模块的load()方法从文件中加载的。
变量options的fill给定文本的颜色。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>> draw.text((0,0),"Hello", fill = (255,0,0))
>>>im01.show()
在图像01的(0,0)位置绘制出字符串“Hello”。
11、 Textsize
定义:draw.textsize(string,options)⇒ (width, height)
含义:返回给定字符串的大小,以像素为单位。
变量option的font用于指定所用字体。它应该是类ImangFont的一个实例,使用ImageFont模块的load()方法从文件中加载的。
例子:
>>>from PIL import Image, ImageDraw
>>>im01 = Image.open("D:\\Code\\Python\\test\\img\\test01.jpg")
>>>draw = ImageDraw.Draw(im01)
>>>draw.textsize("Hello")
(30, 11)
>>>draw.textsize("Hello, the world")
(96, 11)
>>>im01.show()
四、ImageDraw模块的option变量
Option变量有三个属性,分别为outline,fill和font。Outline和fill都可为整数或者元组;font为ImageFont类的实例。
这几个属性在前面方法介绍中都有用到,这里不作解释。
五、ImageDraw模块的兼容性
类Draw包括的一个构造函数和一些方法提供向后兼容。为了使这些函数正常工作,用户应该使用options,或者使用这些方法。但不能混合旧的和新的调用习惯。
(IronPIL)IronPIL不支持这些有兼容性的方法。
1、 ImageDraw
定义:ImageDraw(image)⇒ Draw instance
含义:(不赞成)生成Draw的实例。新代码中不要用这个函数。
2、 Setink
定义:draw.setink(ink)
含义:(不赞成)为后续绘制和fill属性设置颜色。
3、 Setfill
定义:draw.setfill(mode)
含义:(不赞成)设置fill属性。
如果变量mode为0,后续绘制的形状(像多边形和长方形)都是轮廓。如果mode为1,则它们会被填充。
4、 Setfill
定义:draw.setfont(font)
含义:(不赞成)为text()方法设置默认的字体。
变量font是ImageFont类的实例,使用ImageFont模块的load()方法从文件中加载的。
如果觉得本文的文章写得很好,打个赏,多少都行~~~