python-PIL
来源:互联网 发布:nginx 禁止外网访问 编辑:程序博客网 时间:2024/05/29 02:16
PIL (Python Imaging Library)是 Python 中最常用的图像处理库
http://effbot.org/imagingbook/pil-index.htm
.http://pillow-cn.readthedocs.io/zh_CN/latest/
安装PIL
ubuntu:$ sudo apt-get install python-imaging
Mac:$ sudo easy_install PIL
Image
该类是 PIL 库中一个非常重要的类,通过这个类来创建实例可以有直接载入图像文件,读取处理过的图像和通过抓取的方法得到的图像这三种方法。
例子
import Imageimg=Image.open('a.jpg')print img.format,img.size,img.modewidth,hight=img.size#获得长宽img=img.resize((128,128))#resize图像大小img.thumbnail(width//2,hight//2)out=img.rotate(45)#逆时针旋转45out-img.transpose(Image.FLIP_LEFT_RIGHT)#左右对换img.transpose(Image.FLIP_TOP_BOTTOM)#上下对换img.show()img.crop(left,upper,right,lower)#左上右下,从图像中切割某个矩形大小的图像img.transpose(Image.ROTATE_180)#旋转变换img.transpose(Image.ROTATE_90)img.transpose(Image.ROTATE_270)img.convert()#转换色彩空间img.paste(img1,img2)#粘贴img.merge()img.save('b.jpg','jpeg')
Open, rotate, and display an image
from PIL import Imageim = Image.open("bride.jpg")im.rotate(45).show()
创建缩略图
from PIL import Imageimport glob, ossize = 128, 128for infile in glob.glob("*.jpg"): file, ext = os.path.splitext(infile) im = Image.open(infile) im.thumbnail(size, Image.ANTIALIAS) im.save(file + ".thumbnail", "JPEG")
Image函数:
1.
读取图片PIL.Image.open(filename, mode='r')[source]
2.
composite可以使用另外一个图片作为蒙板(mask),所有的这三张图片必须具备相同的尺寸,mask图片的模式可以为“1”,“L”,“RGBA” PIL.Image.alpha_composite(im1, im2)
Image.composite(img1, img2, mask)
im=Image.blend(img1,img2,0.5)
ImageFilter
模块中可以使用 filter 函数来使用模块中一系列预定义的增强滤镜
img.filter(ImageFilter.BLUR)img.filter(ImageFilter.DETAIL)#import ImageFilter Filters
ImageDraw
提供了一系列绘图方法,让我们可以直接绘图。比如要生成字母验证码图片:
import Image, ImageDraw, ImageFont, ImageFilterimport random# 随机字母:def rndChar(): return chr(random.randint(65, 90))# 随机颜色1:def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))# 随机颜色2:def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))# 240 x 60:width = 60 * 4height = 60image = Image.new('RGB', (width, height), (255, 255, 255))# 创建Font对象:font = ImageFont.truetype('Arial.ttf', 36)# 创建Draw对象:draw = ImageDraw.Draw(image)# 填充每个像素:for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor())# 输出文字:for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())# 模糊:image = image.filter(ImageFilter.BLUR)image.save('code.jpg', 'jpeg');
阅读全文
0 0
- python PIL
- python PIL
- python,PIL
- python-PIL
- python-PIL
- python PIL
- PIL Python Imaging Library (PIL)
- python PIL 图形操作
- python-PIL显示中文
- python PIL库安装
- Python PIL学习笔记
- Python PIL模块整理
- python 安装PIL
- python 安装PIL模块
- python PIL PILLOW
- Python - screenshot with PIL
- python pip安装 PIL
- python-PIL学习笔记
- Cocos2d-lua 初识shader之四:描边
- 各种重启
- Matlab中查看内存环境的方法
- 元素出栈、入栈顺序的合法性
- 多线程
- python-PIL
- java POI excel导出,并合并单元格设置宽度高度
- 《将博客搬至CSDN》
- x264学习笔记(一)
- retrofit + RXJava上传 JsonBean、List<T>数据
- hdu 6214 割边最少的最小割
- Android:最全面的 Webview 详解
- 迪杰斯特拉算法
- 基于用户投票的六大排名算法研究