您的位置:首页 > 编程语言 > Python开发

Python Imaging Library: ImageOps Module(图像运算模块)

2017-10-30 20:13 901 查看
Python Imaging Library: ImageOps Module(图像运算模块)

(版本1.1.3) ImageOps模块包含许多“现成的”图像处理操作。这个模块是实验性的,大多数操作符只支持“L”和“RGB”。


函数


autocontrast


ImageOps.autocontrast(image, cutoff=0) ⇒ image

图像对比度最大化(规范化)。

该函数计算输入图像的直方图,根据截止百分比删除直方图中最亮和最暗的像素,

并对图像进行重排,使最黑的像素变为黑色(0),最亮的像素变为白色(255)。


colorize


ImageOps.colorize(image, black, white) ⇒ image

灰度图像彩色化。

黑、白参数应该是RGB数组或颜色名称。

该函数计算一个颜色楔形,将源图像中的所有黑像素映射到第一个颜色,将所有的白色像素映射到第二个颜色。


crop


ImageOps.crop(image, border=0) ⇒ image

从所有四边删除边框像素。这个函数适用于所有的图像模式。


deform


ImageOps.deform(image, deformer, filter=Image.BILINEAR) ⇒ image

使用给定的deobject对象来变形图像。

deobject对象应该提供一个getmesh方法,它返回一个适合于图像转换方法的网格(MESH)列表。


equalize


ImageOps.equalize(image) ⇒ image

直方图均衡化。

该函数将非线性映射应用于输入图像,以在输出图像中创建灰度值的均匀分布。


expand


ImageOps.expand(image, border=0, fill=0) ⇒ image

向图像的所有四个边添加边框像素。


fit


ImageOps.fit(image, size, method, bleed, centering) ⇒ image

返回一个指定大小的裁剪版图像。size参数输出图像的大小,以(宽度,高度)元组表示。

method参数是指重新采样的方法。缺省值是图像最近邻方法。

bleed参数允许您删除图像外部的边框(从所有的四个边)。它的值是一个小数(用0.01表示1%)。默认值为0(没有边框)。

centering参数用于控制剪切位置。(0.5, 0.5) 是剪切中心。

(0.0,0.0)将从左上角开始。

(1.0,0.0)将从左下角开始。


flip


ImageOps.flip(image) ⇒ image

垂直翻转图像(上到下)。


grayscale


ImageOps.grayscale(image) ⇒ image

将图像转换为灰度值图像。


invert


ImageOps.invert(image) ⇒ image

反转(取反)图像。


mirror


ImageOps.mirror(image) ⇒ image

水平翻转图像(从左到右)。


posterize


ImageOps.posterize(image, bits) ⇒ image

减少每个颜色通道的位数。


solarize


ImageOps.solarize(image, threshold=128) ⇒ image

将给定阈值之上所有像素值取反。


例程

# 图像对比度最大化(规范化)(autocontrast)

# ImageOps.autocontrast(image, cutoff=0) ⇒ image

PilImg_Autocontrast1 = Image.open('图像5.jpg')

PilImg_Autocontrast = ImageOps.autocontrast(PilImg_Autocontrast1,

                                            cutoff=0.9)

# PilImg_Autocontrast.show()

# 灰度图像彩色化(colorize)

# ImageOps.colorize(image, black, white) ⇒ image

PilImg_Colorize1 = Image.open('图像5.jpg').convert('L')

PilImg_Colorize = ImageOps.colorize(PilImg_Colorize1,

                                    black=(100, 126, 189),

                                    white=(56, 146, 231))

# PilImg_Colorize.show()

# 删除边框像素(crop)

# ImageOps.crop(image, border=0) ⇒ image

PilImg_Crop1 = Image.open('图像5.jpg').convert('L')

PilImg_Crop = ImageOps.crop(PilImg_Crop1, border=500)

# PilImg_Crop1.show()

# 直方图均衡化(equalize)

# ImageOps.equalize(image) ⇒ image

PilImg_Equalize1 = Image.open('图像5.jpg')

PilImg_Equalize = ImageOps.equalize(PilImg_Equalize1)

# PilImg_Equalize.show()

# 添加边框像素(expand)

# ImageOps.expand(image, border=0, fill=0) ⇒ image

PilImg_Expand1 = Image.open('图像5.jpg')

PilImg_Expand = ImageOps.expand(PilImg_Expand1, border=1, fill=1)

# PilImg_Expand.show()

# 垂直翻转图像(flip)

# ImageOps.flip(image) ⇒ image

PilImg_Flip1 = Image.open('图像5.jpg')

PilImg_Flip = ImageOps.flip(PilImg_Flip1)

# PilImg_Flip.show()

# 水平翻转图像(mirror)

# ImageOps.mirror(image) ⇒ image

PilImg_Mirror1 = Image.open('图像5.jpg')

PilImg_Mirror = ImageOps.mirror(PilImg_Mirror1)

# PilImg_Mirror.show()

# 将图像转换为灰度值图像(grayscale)

# ImageOps.grayscale(image) ⇒ image

PilImg_Grayscale1 = Image.open('图像5.jpg')

PilImg_Grayscale = ImageOps.grayscale(PilImg_Grayscale1)

# PilImg_Grayscale.show()

# 反转(取反)图像(invert)

# ImageOps.invert(image) ⇒ image

PilImg_Invert1 = Image.open('图像5.jpg')

PilImg_Invert = ImageOps.invert(PilImg_Invert1)

# PilImg_Invert.show()

# 减少每个颜色通道的位数(posterize)

# ImageOps.posterize(image, bits) ⇒ image

PilImg_Posterize1 = Image.open('图像5.jpg')

PilImg_Posterize = ImageOps.posterize(PilImg_Posterize1, bits=2)

# PilImg_Posterize.show()

# 将给定阈值之上所有像素值取反(solarize)

# ImageOps.solarize(image, threshold=128) ⇒ image

PilImg_Solarize1 = Image.open('图像5.jpg')

PilImg_Solarize = ImageOps.solarize(PilImg_Solarize1, threshold=250)

# PilImg_Solarize.show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python PIL ImageOps