亲宝软件园·资讯

展开

基于Python的PIL库学习详解

人气:0

摘要

对于图像识别,大量的工作在于图像的处理,处理效果好,那么才能很好地识别,因此,良好的图像处理是识别的基础。在Python中,有一个优秀的图像处理框架,就是PIL库,本博文会分模块,介绍PIL库中的各种方法,并列举相关例子。

参考:http://pillow-cn.readthedocs.io/zh_CN/latest/reference/index.html

网站上列举了PIL库中所有的模块和方法,但是没有相关的例子,博文中会尽量给出相关的例子和进行简单的讲解。

基于的环境:Win10,Python2.7,PIL 1.1.7。

Image模块

开篇的例子

首先,给出Image模块中的一个简单的例子。例子实现的功能是:读取图片,并进行45°旋转,然后进行可视化。

# -*- coding:utf-8 -*-
# Image模块开篇例子
from PIL import Image
im = Image.open('test.bmp') # 读取图片
im.rotate(45).show() # 将图片旋转,并用系统自带的图片工具显示图片

创建缩略图

# -*- coding:utf-8 -*-
# PIL中创建缩略图(create thumbnails)
from PIL import Image
import glob,os

size = 128,128

for infile in glob.glob("*.jpg"): # glob的作用是文件搜索,返回的是一个列表
  file,ext = os.path.splitext(infile) # 将文件的文件名和拓展名分开,用于之后的保存重命名
  im = Image.open(infile)
  im.thumbnail(size,Image.ANTIALIAS) # 等比例缩放
  im.save(file+".thumbnail","JPEG")
  #im.show() # 显示缩略图
  #print im.size,im.mode
 

缩略图不能直接双击打开,而可以使用PIL.image的open读取,然后使用show()方法进行显示。

图像处理

PIL.image.alpha_composite(im1,im2)

PIL.image.blend(im1,im2,alpha)

PIL.Image.composite(im1,im2,mask)

这三个方法都属于图片的合成或者融合。都要求im1和im2的mode和size要一致,alpha代表图片占比的意思,而mask是mode可以为”1”,”L”或者”RGBA”的size和im1、im2一致的。

# coding:utf-8 -*-
from PIL import Image
# 图片合成

# PIL的alpha_composite(im1,im2) 图像通道融合
# im2要和im1的size和mode一致,且要求格式为RGBA
im1 = Image.open("test.png")
im2 = Image.open("test2.png")

newim1 = Image.alpha_composite(im1,im2) # 将im2合成到im1中,如果其一是透明的,
# 才能看到结果,不然最终结果只会显示出im2
newim1.show()
#print(im1.mode)

# -----------------------------------------
# image.blend(im1,im2,alpha)
# alpha为透明度
newim2 = Image.blend(im1,im2,0.5)
newim2.show()

# -----------------------------------------
mask = Image.open("mask.png")
newim3 = Image.composite(im2,im1,mask)
newim3.show()

PIL.image.eval(image,*args)

程序:

# -*- coding:utf-8 -*-
from PIL import Image
im = Image.open("test.png")

imnew = Image.eval(im,lambda i:i*2) # 将原图片的像素点,都乘2,返回的是一个Image对象
#print imnew.mode
imnew.show()
im.show()

创建图像

(1) PIL.image.new(mode,size,color=0)

使用模式和大小,创建一个新的图像。其中,mode可以是”L”,”RGB”,”RGBA”;而size则是一个tuple(元组),color应该和mode相对应。

下面例子,分别创建”L”、”RGB”和”RGBA”的图片。

# -*- coding:utf-8 -*-
from PIL import Image
# 创建图像
# 创建一个灰度图像

newL = Image.new("L",(28,28),255)
newL.show()

# 创建一个RGb图像
newrgb = Image.new("RGB",(28,28),(20,200,45))
newrgb.show()

newrgba = Image.new("RGBA",(28,28),(20,200,45,255))
newrgba.show()
print "The frist image:",newL.size,newL.mode
print "The second image:",newrgb.size,newrgb.mode
print "The third image:",newrgba.size,newrgba.mode

(2)以其他形式创建图像

a. 以数组的形式创建图像,PIL.image.fromarray(obj,mode=None)

obj - 图像的数组,类型可以是numpy.array()

mode - 如果不给出,会自动判断

本人觉得这个功能还是挺实用的,可以将一个数组(具体一点就是像素数组)转换为图像,从图像的本质去处理图像。

下面一段程序,就是用fromarray()函数实现图像的灰度化(使用了两种方法)。

# -*- coding:utf-8 -*-
from PIL import Image
import numpy as np
a = Image.open("fromimg.png")
a.show()
b = a.resize((28,28))
datab = list(b.getdata())
#print type(datab)
obj1 = []
obj2 = []
for i in range(len(datab)):
  obj1.append([sum(datab[i])/3]) # 灰度化方法1:RGB三个分量的均值
  obj2.append([0.3*datab[i][0]+0.59*datab[i][1]+0.11*datab[i][2]])
  #灰度化方法2:根据亮度与RGB三个分量的对应关系:Y=0.3*R+0.59*G+0.11*B

obj1 = np.array(obj1).reshape((28,28))
obj2 = np.array(obj2).reshape((28,28))
print obj1
print obj2

arrayimg1 = Image.fromarray(obj1)
arrayimg2 = Image.fromarray(obj2)
arrayimg1.show()
arrayimg2.show()

显然,两种方法都能成功灰度化。

还有:

PIL.Image.frombytes(mode,size,data,decoder_name='raw',*args)

PIL.Image.fromstring(*args,**kw)

PIL.Image.frombuffer(mode,size,data,decoder_name='raw',*args)

感觉不常用,没有仔细研究。

Image模块下的Image类

下面的Image是一个图像对象,而不是模块!

(1) Image.convert(mode=None,matrix=None,dither=None,palette=0,color=256)

该方法,同样可以实现上面的灰度化处理。

# -*- coding:utf-8 -*-
from PIL import Image
img = Image.open("test.png")
# 灰度化:将RGB/RGBA -> L
img = img.convert("L")
img.show()

(2) Image.copy()

将读取的图片复制一份。

# -*- coding:utf-8 -*-
from PIL import Image
img = Image.open("test.png")
# 灰度化:将RGB/RGBA -> L
img = img.convert("L")
#img.show()

# ------ copy()----------
img1 = img.copy()
img1.show()

将灰度化的图片复制一份,因此该程序的运行结果和之前的一致。

(3) Image.filter(filter)

该函数是用于图像滤波的,PIL中自带了很多的滤波器,就是括号中的filter的参数。filter应该是一个ImaageFilter模块下的对象。这里把ImageFilter模块讲了。其实,该模块就是提供滤波器。自带的滤波器有:

使用中值滤波:

# -*- coding:utf-8 -*-
from PIL import Image
from PIL import ImageFilter

# BLUR - 模糊处理
# CONTOUR - 轮廓处理
# DETAIL - 增强
# EDGE_ENHANCE - 将图像的边缘描绘得更清楚
# EDGE_ENHANCE_NORE - 程度比EDGE_ENHANCE更强
# EMBOSS - 产生浮雕效果
# SMOOTH - 效果与EDGE_ENHANCE相反,将轮廓柔和
# SMOOTH_MORE - 更柔和
# SHARPEN - 效果有点像DETAIL
testimg = Image.open("filter1.png")
testimg.show()
filterimg = testimg.filter(ImageFilter.MedianFilter)
filterimg.show()

(4) 使用各种方法/函数获取图片的基本信息

Image.getbands()

Image.geebbox()

Image.getcolors(maxcolor=256)

Image.getdata(band=None)(一般和list()结合使用)

Image.getextrema()

Image.getpixel((x,y))

Image.histogram(mask=None,extrema=None)

# -*- coding:utf-8 -*-
from PIL import Image
img1 = Image.open("test.png")
img1.show()

# getbands() - 显示该图像的所有通道,返回一个tuple
bands = img1.getbands()
print bands

# getbbox() - 返回一个像素坐标,4个元素的tuple
bboxs = img1.getbbox()
print bboxs

# getcolors() - 返回像素信息,是一个含有元素的列表[(该种像素的数量,(该种像素)),(...),...]
colors = img1.getcolors()
print colors

# getdata() - 返回图片所有的像素值,要使用list()才能显示出具体数值
#data = list(img1.getdata())
#print data

# getextrema() - 获取图像中每个通道的像素最小和最大值,是一个tuple类型
extremas = img1.getextrema()
print extremas

# getpixel() - 获取该坐标
pixels = img1.getpixel((87,180))
print pixels

# histogram() - 返回图片的像素直方图
print(img1.histogram())

运行结果:

('R', 'G', 'B', 'A')
(0, 0, 338, 238)
[(73463, (255, 255, 255, 255)), (32, (252, 249, 252, 255)), (1, (255, 189, 143, 255)), (12, (255, 199, 160, 255)), (22, (247, 239, 247, 255)), (3, (255, 242, 246, 255)), (9, (238, 221, 238, 255)), (9, (235, 215, 235, 255)), (5, (232, 209, 232, 255)), (1, (255, 228, 209, 255)), (2, (255, 210, 225, 255)), (1, (255, 202, 201, 255)), (3, (255, 158, 92, 255)), (22, (218, 181, 218, 255)), (1, (217, 181, 218, 255)), (2, (255, 232, 217, 255)), (16, (255, 195, 153, 255)), (22, (212, 169, 212, 255)), (3, (211, 169, 212, 255)), (1, (204, 153, 204, 255)), (1, (255, 229, 238, 255)), (53, (255, 131, 46, 255)), (9, (255, 203, 167, 255)), (1, (255, 157, 90, 255)), (3, (186, 119, 187, 255)), (2, (255, 217, 229, 255)), (6, (183, 113, 184, 255)), (1, (255, 212, 227, 255)), (14, (214, 175, 215, 255)), (2, (255, 182, 131, 255)), (12, (166, 79, 167, 255)), (2, (255, 180, 127, 255)), (4309, (255, 127, 39, 255)), (737, (163, 73, 164, 255)), (4, (255, 252, 253, 255)), (3, (255, 232, 216, 255)), (9, (255, 250, 233, 255)), (1, (255, 245, 248, 255)), (34, (255, 239, 228, 255)), (3, (255, 142, 64, 255)), (1, (255, 162, 98, 255)), (19, (255, 247, 241, 255)), (7, (255, 223, 201, 255)), (2, (255, 133, 49, 255)), (16, (255, 221, 232, 255)), (58, (255, 235, 221, 255)), (1, (255, 225, 204, 255)), (2, (255, 219, 194, 255)), (21, (255, 175, 120, 255)), (6, (255, 182, 206, 255)), (37, (255, 243, 235, 255)), (3, (255, 179, 127, 255)), (6, (255, 207, 223, 255)), (3, (255, 232, 240, 255)), (1, (255, 134, 51, 255)), (2, (255, 222, 233, 255)), (2, (255, 218, 192, 255)), (1, (255, 186, 186, 255)), (1, (255, 163, 99, 255)), (1, (255, 207, 173, 255)), (8, (255, 151, 80, 255)), (1, (255, 184, 201, 255)), (19, (255, 211, 180, 255)), (1, (255, 143, 65, 255)), (9, (255, 233, 158, 255)), (18, (255, 215, 187, 255)), (1, (255, 185, 136, 255)), (7, (255, 227, 237, 255)), (22, (255, 163, 100, 255)), (1, (255, 221, 198, 255)), (5, (255, 184, 208, 255)), (10, (255, 195, 215, 255)), (5, (255, 239, 182, 255)), (1, (255, 197, 157, 255)), (1, (255, 154, 85, 255)), (1, (255, 136, 55, 255)), (8, (255, 240, 190, 255)), (14, (255, 216, 229, 255)), (3, (255, 179, 204, 255)), (1, (255, 143, 67, 255)), (1, (255, 196, 155, 255)), (19, (255, 249, 227, 255)), (2, (255, 211, 181, 255)), (10, (255, 230, 142, 255)), (4, (255, 187, 140, 255)), (195, (255, 201, 14, 255)), (2, (255, 129, 42, 255)), (1, (255, 131, 47, 255)), (12, (255, 231, 214, 255)), (1, (255, 181, 151, 255)), (8, (249, 244, 249, 255)), (13, (246, 238, 246, 255)), (44, (244, 234, 244, 255)), (1, (243, 232, 244, 255)), (7, (240, 226, 241, 255)), (25, (255, 167, 107, 255)), (24, (255, 215, 229, 255)), (22, (230, 206, 230, 255)), (6, (229, 204, 229, 255)), (3, (255, 130, 45, 255)), (11, (227, 200, 228, 255)), (4, (226, 198, 226, 255)), (3, (255, 127, 40, 255)), (5, (223, 192, 223, 255)), (9, (220, 186, 221, 255)), (172, (255, 174, 201, 255)), (16, (255, 231, 239, 255)), (1, (255, 171, 113, 255)), (33, (209, 164, 209, 255)), (1, (255, 192, 213, 255)), (6, (255, 247, 250, 255)), (2, (255, 136, 54, 255)), (9, (255, 253, 247, 255)), (1, (255, 171, 114, 255)), (2, (255, 147, 73, 255)), (5, (255, 181, 130, 255)), (7, (189, 124, 190, 255)), (1, (255, 199, 161, 255)), (13, (255, 183, 134, 255)), (3, (255, 152, 82, 255)), (2, (255, 156, 88, 255)), (32, (255, 143, 66, 255)), (5, (178, 102, 178, 255)), (6, (175, 96, 176, 255)), (8, (255, 129, 43, 255)), (4, (172, 90, 173, 255)), (1, (255, 168, 109, 255)), (1, (255, 153, 83, 255)), (1, (255, 174, 118, 255)), (1, (255, 172, 115, 255)), (1, (255, 148, 75, 255)), (8, (255, 244, 248, 255)), (1, (255, 130, 43, 255)), (5, (255, 205, 222, 255)), (1, (255, 210, 177, 255)), (1, (255, 170, 110, 255)), (1, (255, 157, 89, 255)), (1, (255, 197, 134, 255)), (13, (255, 155, 86, 255)), (3, (255, 137, 56, 255)), (2, (255, 138, 57, 255)), (11, (255, 227, 208, 255)), (1, (255, 190, 145, 255)), (2, (255, 155, 87, 255)), (1, (169, 84, 170, 255)), (4, (255, 202, 220, 255)), (6, (255, 139, 59, 255)), (1, (255, 128, 42, 255)), (1, (255, 158, 91, 255)), (1, (255, 198, 158, 255)), (5, (255, 130, 44, 255)), (1, (255, 202, 165, 255)), (1, (255, 187, 154, 255)), (1, (255, 132, 48, 255)), (1, (255, 154, 84, 255)), (1, (255, 235, 241, 255)), (7, (255, 135, 53, 255)), (62, (255, 159, 93, 255)), (2, (255, 177, 124, 255)), (4, (255, 187, 210, 255)), (11, (255, 251, 248, 255)), (1, (255, 229, 211, 255)), (1, (255, 208, 176, 255)), (1, (255, 133, 50, 255)), (2, (255, 219, 231, 255)), (2, (255, 141, 63, 255)), (2, (255, 146, 71, 255)), (1, (255, 160, 95, 255)), (2, (255, 184, 135, 255)), (1, (255, 208, 175, 255)), (1, (255, 139, 61, 255)), (1, (255, 189, 211, 255)), (2, (255, 145, 69, 255)), (263, (255, 191, 147, 255)), (4, (255, 187, 141, 255)), (3, (255, 250, 252, 255)), (1, (255, 147, 72, 255)), (5, (255, 177, 203, 255)), (1, (255, 169, 109, 255)), (62, (255, 207, 174, 255))]
((163, 255), (73, 255), (14, 255), (255, 255))
(255, 127, 39, 255)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 737, 0, 0, 12, 0, 0, 1, 0, 0, 4, 0, 0, 6, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 3, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 33, 0, 3, 22, 0, 14, 0, 0, 1, 22, 0, 9, 0, 0, 5, 0, 0, 4, 11, 0, 6, 22, 0, 5, 0, 0, 9, 0, 0, 9, 0, 7, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80444]

(5) 图像粘贴操作(paste)

Image.paste(im,box=None,maske=None)

使用im粘贴到原图片中。注意:两个图片的mode和size要求一致,不一致可以使用convert()和resize()进行调整。

# -*- coding:utf-8 -*-
from PIL import Image
rawimg = Image.open("qqtou.png")
print rawimg.size

im = Image.open("number.png")
print im.size

# rawimg的size和im的size要相同,不然不能匹配
# paste(用来粘贴的图片,(位置坐标)),可以通过设置位置坐标来确定粘贴图片的位置
# 该方法没有返回值,直接作用于原图片
rawimg.paste(im,(75,-90))
rawimg.show()

(6) 各种put操作

Image.putalpha(alpha) - 添加多一层alpha层,没看出具体效果

Image.putdata(data,scale=1.0,offset=0.0) - 添加一个像素序列到原图像。

# -*- coding:utf-8 -*-
from PIL import Image

img = Image.open("qqtou.png")
img = img.convert("L")
img.show()

imgdata = list(img.getdata())
print imgdata

addlist = []
for i in range(len(imgdata)):
  if imgdata[i]>250:
    addlist.append(imgdata[i]-100)
  else:
    addlist.append(imgdata[i])


# putdata - 将一个序列添加进原图像,没有返回值,直接作用在原图像中
img.putdata(addlist)
img.show()

显然,原始图像(左图)已经发生了改变。

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论