Python的PIL库

Python图像库PIL(Python Image Library)是python的第三方图像处理库;

图像类Image class

    Image类是PIL中的核心类,比如从文件中加载一张图像,处理其他形式的图像,或者是从头创造一张图像等。Image模块操作的基本方法都包含于此模块内。如open、save、conver、show…等方法。下面是PIL的 Image类中常用的方法和属性:

1. open方法
  Image.open(file)
  Image.open(file, mode)
  要从文件加载图像,使用 open() 函数, 在 Image 模块(类):

from PIL import Image             ##调用库,包含图像类
    im = Image.open("3d.jpg")  ##文件存在的路径,如果没有路径就是当前目录下文件
    im.show()

2. save方法
  im.save(outfile,options…)
  im.save(outfile, format, options…)
      用 Image 类的 save() 方法保存文件的文件,使用给定的文件名保存图像。如果变量format缺省,如果可能的话,则从文件名称的扩展名判断文件的格式。该方法返回为空。关键字options为文件编写器提供一些额外的指令。
比如:jpg 转换成png

from PIL import Image
im = Image.open("3d.jpg")
print(im)
im.save("3d.png")     ## 将"3d.jpg"保存为3d.png"
im = Image.open("3d.png")  ##打开新的png图片
print(im.format, im.size, im.mode)

3. format属性

im.format ⇒ string or None

这个属性标识了图像来源,如果图像不是从文件读取它的值就是None。

from PIL import Image
im = Image.open("3d.jpg")
print(im.format) ## 打印出格式信息
im.show()

 

4. mode属性

im.mode ⇒ string

图像的模式,常见的mode 有 “L” (luminance) 表示灰度图像,“RGB”表示真彩色图像,和 “CMYK” 表示出版图像,表明图像所使用像素格式。如下表为常见的nodes描述:
Python的PIL库_第1张图片

from PIL import Image
im = Image.open("3d.jpg")
print(im.mode) ## 打印模式属性
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()

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模块讲了。其实,该模块就是提供滤波器。自带的滤波器有:BLUR、CONTOUR、DETAIL、EDGE_ENHANCE、EDGE_ENHANCE_MORE、EMBOSS、FIND_EDGES、SMOOTH、SMOOTH_MORE、SHARPEN。其中BLUR就是均值滤波,CONTOUR找轮廓,FIND_EDGES边缘检测,使用该模块时,需先导入。

使用中值滤波:

# -*- 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,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0, 16, 31, 20, 8, 6, 0, 35, 4, 0, 73463, 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, 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]


原文链接:https://blog.csdn.net/louishao/article/details/69879981

你可能感兴趣的:(python,python,图像处理,计算机视觉)