python图像处理库pil_python-图像处理之PIL库的使用

1.先看一段英文代码The Python Imaging Library (PIL) adds image processing capabilities to your Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities,意思是说PIL给Python增加了图像处理功能,这个库可以处理多种文件格式图像,提供了强大的图像处理和图形处理能力。

2.安装PIL,目前一般的PIL版本只支持python2.x版本,如果想用3.0以后的,先下载anaconda这个平台,使用起来分方便,自带spyder和qtpython插件,下载地址见https://repo.continuum.io/archive/,里面有各种平台的安装包,根据你的平台具体下载,这个平台安装好后可以很方便下载很多python支持库,本人用的Anaconda3这个版本系列。

3.这个PIL使用起来很精单首先导入模块:from PIL import Image,并查看模块内容,里面有好多组件,Image就是一个常用组件

from PIL import Image

dir(PIL)

Out[17]:

['BmpImagePlugin','GifImagePlugin','GimpGradientFile','GimpPaletteFile','Image','ImageChops','ImageColor','ImageEnhance','ImageFile','ImageFilter','ImageMode','ImagePalette','ImageSequence','ImageStat','JpegImagePlugin','JpegPresets','PILLOW_VERSION','PaletteFile','PngImagePlugin','PpmImagePlugin','TiffImagePlugin','TiffTags','VERSION','__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__','__path__','__spec__','__version__','_binary','_imaging','_plugins','_util','version']

3.打开一幅图像:open(fp, mode="r")第一个参数为文件路径,第二个为打开模式一般默认只读

img = Image.open('girl1.jpg')

4.将打开的图像显示出来调用:show(self, title=None, command=None), On Windows, it saves the image to a temporary BMP file, and usesthe standard BMP display utility to show it (usually Paint).:param title: Optional title to use for the image window,where possible.:param command: command used to show the image,一般会产生一幅临时图像,然后调用默认的图像查看器显示图像

img.show()

1069003-20181011151843472-1797111271.png

5.查看图像尺寸img.size

w,h = img.size

w

Out[27]: 1920

h

Out[28]: 1080

6.缩放图像img:thumbnail(self, size, resample=BICUBIC),第二个参数是缩放尺寸,第三个参数可以指定缩放模式,有`PIL.Image.NEAREST`, `PIL.Image.BILINEAR`,`PIL.Image.BICUBIC`, `PIL.Image.LANCZOS`.这四种模式

In:img.thumbnail((w/2,h/2))

In:img.size

Out[33]: (960, 540)

1069003-20181011153050863-1592230204.png

7.保存图像save(self, fp, format=None, **params)

img.save('D:\\girl.jpeg','jpeg')

8.根据所给的模式和尺寸创建一幅新图像new(mode, size, color=0),模式,尺寸,颜色

1069003-20181011155550192-521628203.png

9.选中图片一部分进行复制crop(self, box=None) The crop rectangle, as a (left, upper, right, lower)-第二个参数为一个矩形也就是一个四个元素的元组

1069003-20181011160212425-595597039.png

10.粘贴图像paste(self, im, box=None, mask=None)

1069003-20181011161936768-1410584479.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值