PIL: The Image Module 图像模块


Link: The Image Module

The Image Module

这个模块包括:

  • 一些函数
  • 一个image类,包含一系列成员函数以及图像的属性,在PIL中用来表示一张图像

Functions

这个模块提供的一些函数(不是图像对象中的成员方法)


new

I m a g e . n e w ( m o d e , s i z e ) ⇒ I m a g e \bold{Image.new(mode, size) \Rightarrow Image} Image.new(mode,size)Image
I m a g e . n e w ( m o d e , s i z e , c o l o r ) ⇒ I m a g e \bold{Image.new(mode,size,color) \Rightarrow Image} Image.new(mode,size,color)Image

其中:

  • mode:图像的颜色类型,例如:L 表示8位灰度图,RGB
  • size:图像的尺寸,这是一个二元组(tuple),(width, height),例如 (1024,768),注意括号
  • color:即初始化的颜色,例如可以是 0 表示,初始化每个像素值为 0,否则图像是未初始化的状态

这个函数创建一个图像对象,并且返回对应的图像对象

from PIL import Image
# return a pure white image object
img = Image.new("RGB", (512, 512), "white")

open

I m a g e . o p e n ( f i l e ) ⇒ I m a g e \bold{Image.open(file) \Rightarrow Image} Image.open(file)Image
I m a g e . o p e n ( f i l e , m o d e ) ⇒ I m a g e \bold{Image.open(file,mode) \Rightarrow Image} Image.open(file,mode)Image

其中:

  • file:可以是一个字符串,也可以是一个python的文件对象
  • mode:即读取方式,如果给出,必须是'r'

这个函数用来打开一个图像文件,并返回相应的图像对象。但是这是一个懒惰的方法(lazy),这个方法在调用的时候仅仅只是读取这个图像的头部信息(header),而不会读取图像数据,当且仅当你打算对图像数据进行处理的时候(例如,调用putpixel方法修改像素数据),将图像数据读入。当然,你也可以调用Image对象的load方法强制其将数据读入。

from PIL import Image
img = Image.open('lenna.jpg')

from PIL import image
from StringIO import StringIO

# 从字符串流中读取图像数据,可以用于图像传输
im = Image.open(StringIO(data))

blend

I m a g e . b l e n d ( i m a g e 1 , i m a g e 2 , a l p h a ) ⇒ I m a g e \bold{Image.blend(image1, image2, alpha) \Rightarrow Image} Image.blend(image1,image2,alpha)Image

顾名思义,即图像混合,返回一个新的图像,由所给的两个图像通过对应像素线性插值得到(即所给图像需要满足尺寸相同,以及颜色模式相同),其中:

  • image1,image2:输入图像
  • alpha:线性插值的参数

返回的图像将会是:

out = image1 * (1.0 - alpha) + image2 * alpha

alpha 为图像 2 在输出结果中的比重

composite

I m a g e . c o m p o s i t e ( i m a g e 1 , i m a g e 2 , m a s k ) ⇒ I m a g e \bold{Image.composite(image1, image2, mask) \Rightarrow Image} Image.composite(image1,image2,mask)Image

如果希望对于每一对像素采用不同的混合比例,可以使用这个函数代替 blend 函数,其中:

  • mask:一个和所给图像尺寸相同的混合比例数组

即:

width, height = image1.size
for x in range(0, width):
	for y in range(0, height):
		out[x,y]= image1[x,y] * (1.0 - mask[x,y]) + image2[x,y] * mask[x,y]

mask 的模式没有规定,但是同样,所给图像的尺寸和颜色模式必须相同

eval

I m a g e . e v a l ( i m a g e , f u n c t i o n ) ⇒ I m a g e \bold{Image.eval(image, function) \Rightarrow Image} Image.eval(image,function)Image

这有点类似于C++中的一些容器的操作,这个方法将会对图像中的每一个像素应用所给的function,即可以用于对每个像素进行相同的处理(可能就是将两个循环给封装起来?然后可能在编译阶段优化提高图像遍历的效率,有机会可以测试一下)

methods

图像对象中的方法


load

i m g . l o a d ( ) \bold{img.load()} img.load()

In 1.1.6 and later, load returns a pixel access object that can be used to read and modify pixels. The access object behaves like a 2-dimensional array, so you can do:

这是一个非常简单的方法,在 1.1.6 之前,只是涉及到了 lazy or not 的问题,但是在 1.1.6,这个方法将会返回一个用于像素处理的对象,类似一个二维数组:

pix = im.load()
print pix[x, y]
pix[x, y] = value

相比于使用 putpixelgetpixel 进行操作来说,这样的做法更加的高效

save

i m g . s a v e ( o u t f i l e , o p t i o n s . . . ) \bold{img.save(outfile,options...)} img.save(outfile,options...)
i m g . s a v e ( o u t f i l e , f o r m a t , o p t i o n s . . . ) \bold{img.save(outfile,format,options...)} img.save(outfile,format,options...)

将图片保存为所给文件名,如果格式没给出,则保存为文件名后缀的格式。也可以使用文件对象代替文件名,但必须给出格式。options 可以给 writer (即将数据写进输出文件的程序)提供额外的一些要求(具体用法没有给出,,,不太懂)

If the save fails, for some reason, the method will raise an exception (usually an IOError exception). If this happens, the method may have created the file, and may have written data to it. It’s up to your application to remove incomplete files, if necessary.

如果保存失败的话,会抛出一个异常(通常会是一个 IOError),此时文件可能已经创建了,并且有数据写进

show

i m g . s h o w ( ) \bold{img.show()} img.show()

显示图像

putpixel

i m g . p u t p i x e l ( ( x , y ) , c o l o r ) \bold{img.putpixel((x,y),color)} img.putpixel((x,y),color)

像素赋值方法,其中:

  • (x,y):一个二元组,像素的位置
  • color:颜色

Note that this method is relatively slow. If you’re using 1.1.6, pixel access objects (see load) provide a faster way to modify the image. If you want to generate an entire image, it can be more efficient to create a Python list and use putdata to copy it to the image. For more extensive changes, use paste or the ImageDraw module instead.

注意:这个函数的运行效率是非常差的,可以将这个函数转换为一个内联函数进行调用:

im.load()
putpixel = im.im.putpixel
for i in range(n):
	...
	putpixel((x, y), value)

如果使用的是 1.1.6 ,可以采用load方法得到一个用于像素访问的对象,可以进行更加高效的图像处理:

pix = im.load()
for i in range(n):
	...
	pix[x,y] = value

getpixel

i m g . g e t p i x e l ( ( x , y ) ) ⇒ v a l u e   o r   t u p l e \bold{img.getpixel((x,y))}\Rightarrow value\ or\ tuple img.getpixel((x,y))value or tuple

返回对应位置的像素的值,如果是多个通道例如: R G B RGB RGB,则返回一个元组

Note that this method is rather slow; if you need to process larger parts of an image from Python, you can either use pixel access objects (see load), or the getdata method.

同样讲的是效率问题,见 load

resize

i m g . r e s i z e ( s i z e ) \bold{img.resize(size)} img.resize(size)
i m g . r e s i z e ( s i z e , f i l t e r ) \bold{img.resize(size,filter)} img.resize(size,filter)

重新调整图像大小,其中:

  • size:一个二元组 (width, height)
  • filter:表示调整大小后,填充像素的插值方式,见 Filters

rotate

i m g . r o t a t e ( a n g l e ) ⇒ i m a g e \bold{img.rotate(angle)\Rightarrow image} img.rotate(angle)image
i m g . r o t a t e ( a n g l e , f i l t e r = N E A R E S T , e x p a n d = 0 ) ⇒ i m a g e \bold{img.rotate(angle,filter=NEAREST,expand=0)\Rightarrow image} img.rotate(angle,filter=NEAREST,expand=0)image

对图像顺时针旋转相应的角度,返回结果图像,其中:

  • angle:旋转角度, 0 ° ∼ 360 ° 0\degree\sim360\degree 0°360°
  • filter:插值方式,默认为最近邻插值
  • expand:是否拓展原来图像的大小,为真则将调整大小以容纳旋转后的图像,反之则保持原来大小,默认为保持大小不变

getbands

i m g . g e t b a n d s ( ) ⇒ t u p l e   o f   s t r i n g s \bold{img.getbands() ⇒ tuple\ of\ strings} img.getbands()tuple of strings

返回图像的通道,例如一张 R G B RGB RGB 图像返回的是一个三元组:('R','G','B')

example

>>> img.getbands()
{'R', 'G', 'B'}

split

i m g . s p l i t ( ) ⇒ s e q u e n c e \bold{img.split()\Rightarrow sequence} img.split()sequence

将一个多通道的图像划分成多幅单通道图像,每个图像对应一个通道,例如一个 R G B RGB RGB 的图像,返回的是三个图像的序列: R , G , B R,G,B R,G,B

convert

i m g . c o n v e r t ( m o d e ) ⇒ i m a g e \bold{img.convert(mode) ⇒ image} img.convert(mode)image

将图像转换为另外的颜色模式(见 mode),如果没有给出,则会选择一个不需要调色板的颜色模式

When converting from a colour image to black and white, the library uses the ITU-R 601-2 luma transform:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

When converting to a bilevel image (mode “1”), the source image is first converted to black and white. Resulting values larger than 127 are then set to white, and the image is dithered. To use other thresholds, use the point method. To disable dithering, use the dither= option (see below).

也就是说,将彩色图片转换为灰度图片采用的是我们熟悉的比例: R : 0.299 , G : 0.587 , B : 0.114 R:0.299,G:0.587,B:0.114 R:0.299,G:0.587,B:0.114
而将图片转换为一位图像(黑白图)的话,则会先转换为灰度图,然后再根据像素值是否大于 127 127 127 将图片转换为黑白图像

i m g . c o n v e r t ( “ P ” , ∗ ∗ o p t i o n s ) ⇒ i m a g e \bold{img.convert(“P”, **options) ⇒ image} img.convert(P,options)image

作用相同,但是为彩色图转化为8位彩色图提供了更多的控制选项:

  • dither:是否应用抖动
  • palette:即决定生成的调色板,默认为 WEB,即标准 216 216 216 色调色板。选择 ADAPTIVE 可以获得更好的调色板
  • colors:当选择 ADAPTIVE 时调色板的颜色数量,默认为最大值,即 256 256 256

copy

i m g . c o p y ( ) ⇒ i m a g e \bold{img.copy() ⇒ image} img.copy()image

复制一份该图像并返回

crop

i m g . c r o p ( b o x ) ⇒ i m a g e \bold{img.crop(box)\Rightarrow image} img.crop(box)image

获得原图中指定区域的图像,其中:

  • box:一个四元组,指定区域的上下左右边界

This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To get a separate copy, call the load method on the cropped copy.

也就是说这是一个lazy的操作,如果没有在调用这个函数之后使用load方法的话,那么对原图进行的修改(在裁剪区域内的话)就会影响到我们获得的图像,调用load之后,则不会有影响

paste

i m g . p a s t e ( i m a g e , b o x ) \bold{img.paste(image, box)} img.paste(image,box)

将所给图像填充到所给的区域中,这个 box 参数可以是:

  • 一个二元组,即指定区域左上角的位置,这时图像的大小与填充区域的大小无关
  • 一个四元组,即指定了区域上下左右的边界,图像大小必须与区域大小吻合
  • None,相当于二元组 (0,0)

If the modes don’t match, the pasted image is converted to the mode of this image

注意如果颜色模式不匹配的话,那么填充图像的颜色模式将会转换成被填充图像的颜色模式(convert

i m g . p a s t e ( c o l o u r , b o x ) \bold{img.paste(colour, box)} img.paste(colour,box)

即用纯色填充所给区域

The colour is given as a single numerical value for single-band images, and a tuple for multi-band images.

如果是单通道图片,则只需给出一个值;如果是多通道图片,则需要给出一个相应的元组

i m g . p a s t e ( i m a g e , b o x , m a s k ) \bold{img.paste(image, box, mask)} img.paste(image,box,mask)

同样将所给图像填充进所给区域,但是取决于mask 的值,即只填充mask中指定的区域,其中:

  • mask[x,y]=255:填充
  • mask[x,y]=0:保留原图颜色

Note that if you paste an “RGBA” image, the alpha band is ignored. You can work around this by using the same image as both source image and mask.

注意:如果你使用的是一张 R G B A RGBA RGBA 的图像的话,alpha通道会被忽略掉。不过,可以将alpha通道作为mask来解决这个问题。(即具有透明度的图像的paste问题)

i m g . p a s t e ( c o l o u r , b o x , m a s k ) \bold{img.paste(colour, box, mask)} img.paste(colour,box,mask)

同上,填充纯色

filter

i m g . f i l t e r ( f i l t e r ) ⇒ i m a g e \bold{img.filter(filter) ⇒ image} img.filter(filter)image

返回用对应滤波器进行图像滤波的结果,在ImageFilter模块中提供了许多内建的滤波器

thumbnail

i m g . t h u m b n a i l ( s i z e ) \bold{img.thumbnail(size)} img.thumbnail(size)
i m g . t h u m b n a i l ( s i z e , f i l t e r ) \bold{img.thumbnail(size, filter)} img.thumbnail(size,filter)

将原图转化为缩略图,如果需要保持原图,则需要将原图拷贝之后再获得其缩略图,其中:

  • size:一个二元组 (width,height) ,缩略图的大小
  • filter:缩略图的插值方式,见 Filter,其中 ANTIALIAS 方式得到的缩略图的质量最好,但是效率较低,当且仅当效率要求大于质量要求的时候建议使用其他方法。默认为 NEAREST

Attribute

Image 类中的属性


format

i m g . f o r m a t ⇒ s t r i n g   o r   N o n e \bold{img.format ⇒ string\ or\ None} img.formatstring or None

原图的文件格式,通常是一个字符串,例如:'BMP'如果是在程序中使用库函数创建的图像的话,这个属性为 None

mode

i m g . m o d e ⇒ s t r i n g \bold{img.mode ⇒ string} img.modestring

图像的颜色模式:

  • 1 (1-bit pixels, black and white, stored with one pixel per byte)
  • L (8-bit pixels, black and white),8位灰度图
  • P (8-bit pixels, mapped to any other mode using a colour palette),8位彩色图,含调色板
  • RGB (3x8-bit pixels, true colour),24位彩色图
  • RGBA (4x8-bit pixels, true colour with transparency mask),32位彩色图,含alpha通道
  • CMYK (4x8-bit pixels, colour separation),32位彩色,即印刷四色模式,印刷颜色模型
  • YCbCr (3x8-bit pixels, colour video format),24位彩色,视频颜色格式
  • I (32-bit signed integer pixels),32位有符号整型
  • F (32-bit floating point pixels),32位浮点数

size

i m g . s i z e ⇒ ( w i d t h , h e i g h t ) \bold{img.size\Rightarrow (width,height)} img.size(width,height)

一个二元组 (width, height)

palette

i m g . p a l e t t e ⇒ p a l e t t e o r N o n e \bold{img.palette ⇒ palette or None} img.palettepaletteorNone
mode='P' 的图像,即8位彩色图的调色板

info

i m g . i n f o ⇒ d i c t i o n a r y \bold{img.info ⇒ dictionary} img.infodictionary

一个字典对象,包含图像的一些相关信息

example

>>> from PIL import Image
>>> img = Image.open('timg.jfif')
>>> img.mode
'RGB
>>> img.format
'JPEG'
>>> img.size
(1916, 1078)
>>> img.palette
None
>>> img.info
{'jfif': 257, 'jfif_version': (1, 1), 'dpi': (96, 96), 'jfif_unit': 1, 'jfif_density': (96, 96)}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值