首页 > 编程 > Python > 正文

python pillow模块使用方法详解

2019-11-25 11:48:41
字体:
来源:转载
供稿:网友

pillow

Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。pillow可以说已经取代了PIL,将其封装成python的库(pip即可安装),且支持python2和python3,目前最新版本是3.0.0。

Pillow的Github主页:https://github.com/python-pillow/Pillow
Pillow的文档(对应版本v3.0.0):

https://pillow.readthedocs.org/en/latest/handbook/index.html

安装它很简单 pip install pillow

使用方式:

#python2 import Image #python3(因为是派生的PIL库,所以要导入PIL中的Image) from PIL import Image

以python3为例,

open

from PIL import Imageim = Image.open("1.png")im.show()

format

format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为None;size属性是一个tuple,表示图像的宽和高(单位为像素);mode属性为表示图像的模式,常用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。如果文件不能打开,则抛出IOError异常。

print(im.format, im.size, im.mode)

save

im.save("c://")

convert()

convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值可以是如下几种:
・ 1 (1-bit pixels, black and white, stored with one pixel per byte)
・ L (8-bit pixels, black and white)
・ P (8-bit pixels, mapped to any other mode using a colour palette)
・ RGB (3x8-bit pixels, true colour)
・ RGBA (4x8-bit pixels, true colour with transparency mask)
・ CMYK (4x8-bit pixels, colour separation)
・ YCbCr (3x8-bit pixels, colour video format)
・ I (32-bit signed integer pixels)
・ F (32-bit floating point pixels)

im = Image.open('1.png').convert('L')

Filter

from PIL import Image, ImageFilter im = Image.open(‘1.png') # 高斯模糊 im.filter(ImageFilter.GaussianBlur) # 普通模糊 im.filter(ImageFilter.BLUR) # 边缘增强 im.filter(ImageFilter.EDGE_ENHANCE) # 找到边缘 im.filter(ImageFilter.FIND_EDGES) # 浮雕 im.filter(ImageFilter.EMBOSS) # 轮廓 im.filter(ImageFilter.CONTOUR) # 锐化 im.filter(ImageFilter.SHARPEN) # 平滑 im.filter(ImageFilter.SMOOTH) # 细节 im.filter(ImageFilter.DETAIL)

查看图像直方图

im.histogram()

转换图像文件格式

def img2jpg(imgFile):     if type(imgFile)==str and imgFile.endswith(('.bmp', '.gif', '.png')):     with Image.open(imgFile) as im:       im.convert('RGB').save(imgFile[:-3]+'jpg')   img2jpg('1.gif') img2jpg('1.bmp') img2jpg('1.png')

屏幕截图

from PIL import ImageGrab im = ImageGrab.grab((0,0,800,200)) #截取屏幕指定区域的图像 im = ImageGrab.grab() #不带参数表示全屏幕截图

图像裁剪与粘贴

box = (120, 194, 220, 294) #定义裁剪区域 region = im.crop(box) #裁剪 region = region.transpose(Image.ROTATE_180) im.paste(region,box) #粘贴

图像缩放

im = im.resize((100,100)) #参数表示图像的新尺寸,分别表示宽度和高度

图像对比度增强

from PIL import Image from PIL import ImageEnhance #原始图像 image = Image.open('lena.jpg') image.show() #亮度增强 enh_bri = ImageEnhance.Brightness(image) brightness = 1.5 image_brightened = enh_bri.enhance(brightness) image_brightened.show() #色度增强 enh_col = ImageEnhance.Color(image) color = 1.5 image_colored = enh_col.enhance(color) image_colored.show() #对比度增强 enh_con = ImageEnhance.Contrast(image) contrast = 1.5 image_contrasted = enh_con.enhance(contrast) image_contrasted.show() #锐度增强 enh_sha = ImageEnhance.Sharpness(image) sharpness = 3.0 image_sharped = enh_sha.enhance(sharpness) image_sharped.show()

Image模块用法介绍

1. 简介。

图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴。PIL (Python Imaging Library)是 Python 中最常用的图像处理库,目前版本为 1.1.7,我们可以 在这里 下载学习和查找资料。

Image 类是 PIL 库中一个非常重要的类,通过这个类来创建实例可以有直接载入图像文件,读取处理过的图像和通过抓取的方法得到的图像这三种方法。

2. 使用。

导入 Image 模块。然后通过 Image 类中的 open 方法即可载入一个图像文件。如果载入文件失败,则会引起一个 IOError ;若无返回错误,则 open 函数返回一个 Image 对象。现在,我们可以通过一些对象属性来检查文件内容,即:

>>> import Image>>> im = Image.open("j.jpg")>>> im.show()>>> print im.format, im.size, im.modeJPEG (440, 330) RGB

这里有三个属性,我们逐一了解。

format : 识别图像的源格式,如果该文件不是从文件中读取的,则被置为 None 值。

size : 返回的一个元组,有两个元素,其值为象素意义上的宽和高。

mode : RGB(true color image),此外还有,L(luminance),CMTK(pre-press image)。

from PIL import Imagefrom PIL import ImageEnhanceimport pytesseractimport repytesseract.pytesseract.tesseract_cmd = 'D://Program Files//Tesseract-OCR//tesseract.exe'tessdata_dir_config = '--tessdata-dir "D://Program Files//Tesseract-OCR//tessdata"'im=Image.open("./img/10.jpg")im=im.convert('L')im.show()im=ImageEnhance.Contrast(im)im=im.enhance(1)#im = im.resize((300, 90))ltext = pytesseract.image_to_string(im)ltext = re.sub("/W", "", ltext)im.show()print(ltext)#print(pytesseract.image_to_string(im))#print(pytesseract.image_to_boxes(im))#print(im.format, im.size, im.mode)

convert() : 该函数可以用来将图像转换为不同色彩模式。

ImageEnhance.Contrast(im):使用ImageEnhance可以增强图片的识别率

其他

简单的几何变换。

>>>out = im.resize((128, 128))           #调整图片大小>>>out = im.rotate(45)               #逆时针旋转 45 度角。>>>out = im.transpose(Image.FLIP_LEFT_RIGHT)    #左右对换。>>>out = im.transpose(Image.FLIP_TOP_BOTTOM)    #上下对换。>>>out = im.transpose(Image.ROTATE_90)       #旋转 90 度角。>>>out = im.transpose(Image.ROTATE_180)      #旋转 180 度角。>>>out = im.transpose(Image.ROTATE_270)      #旋转 270 度角。

序列图像。

即我们常见到的动态图,最常见的后缀为 .gif ,另外还有 FLI / FLC 。PIL 库对这种动画格式图也提供了一些基本的支持。当我们打开这类图像文件时,PIL 自动载入图像的第一帧。我们可以使用 seek 和 tell 方法在各帧之间移动。

import Imageim.seek(1)    # skip to the second frametry:  while 1:    im.seek( im.tell() + 1)    # do something to imexcept EOFError:  pass

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表