PIL: Python Image Library, python平台的图像处理库,要使用Image首先要从PIL库导入Image:

from PIL import Image

如果没有安装PIL的包,导入会出错。所以要先安装包

PIL:Python Imaging Library(仅支持到python2.7)

Pillow:支持最新的Python 3.*

Python3.*的版本直接安装Pillow:pip install Pillow

Image是PIL下的一个类,具体的使用如下:

from PIL import Image

# 读取图片

im = Image.open(r''D:\kolor.jpg'')

#查看图片信息

im.format, im.size, im.mode

# 显示图片

im.show()

# 保存图片, 参数:保存的地址和名称,图片格式

im.save(r'D:\kolor.jpg', 'JPEG')

#创建新图片

im=Image.new(mode, size) # color的默认值是黑色

im=Image.new(mode, size, color)

如: im=Image.new('RGB', (450, 600), (255,255,255))  # 'RGB'是红(RED)绿(GREEN)蓝(BLUE)模式

#两张图片相加

Image.blend(im1, im2, alpha)  # alpha是im1和im2的比例参数

#点操作

im.point(function) # function接收一个参数,并对图片的每个点执行这个函数

如:im.point(lambad i: i*1.5) #对每个点进行50%的加强

#图片裁剪

box=(100,100,500,500)  # 设置要裁剪的区域

region=im.crop(box)

#图片黏贴

im.paste(im1, box) #把im1的box区域黏贴到im中去

#通道分离

r,g,b=im.split() #分割成三个通道,此时r,g,b分别是三个图像对象

#通道合并

im=Image.merge('RGB', (b,g,r)) #将b, r两个通道进行反转

#改变图片的大小

im.resize((128,128))

#缩小图片

im.thumbnail(())

im.thumbnail((w//2, h//2))  # 缩小50%

#旋转图像

im.rotate(45) #逆时针旋转45度

im.transpose(Image.ROTATE_180)

im.transpose(Image.FLIP_LEFT_RIGHT) #左右兑换

im.transpose(Image.FLIP_TOP_BOTTOM) #上下对换

#图像mode转换

im.convert('RGBA') #图像的mode转换成RGBA类型

#写某个像素位置的值

im.putpixel((4,4), (255,0,0))

#加滤镜

im.filter(ImageFilter.BLUE)

以下是Image对象的全部方法:

save(f,format=None) 保存 如果f是一个file对象,必须指定format(format codes)
convert(mode) 转换mode  
copy()    
crop(bbox) 剪切 原图中bbox区域
filter(name) 滤镜 the name of predefined image enhancement filters
滤镜名字需要import ImageFilter
getbands() 通道的字符串序列 如RGB图返回('R', 'G', 'B')
getbbox() 包含非零区域的最小bbox  
getextrema() 最大最小像素点值 min&max pixel value
单通道图:返回元组(min,max)
多通道图:返回各个通道的元组组成的元组
getpixel(xy) 取像素点值 坐标xy处的pixel value or a sequence of pixel values
histogram(mask=None)

统计直方图

单通道图:返回列表[c0, c1, ...],ci是值为i的像素数

多通道图:a single sequence that is the concatenation of the sequences for all bands

mask参数:a same-sized mask image of mode "1" or "L"(include only those pixels correspond to nonzero pixels in the mask argument)

offset(dx,dy=None)

平移

Returns a new image the same size as the original, but with all pixels rotated dx in the +x direction,and dy in the +y direction.

If dy is omitted, it defaults to the same value as dx.

paste(i2,where,mask=None) 粘贴图片 where参数可以是
1 (x,y)坐标对:i2的像素点(0,0)对齐原图中的(x,y)粘贴,i2超过原图边界的部分被抛弃
2 bbox:i2必须和该bounding box大小一致
3 None:i2必须和原图大小一致
如果i2的mode和原图不一致,粘贴前会被转换。
mask参数:a same-sized mask image of mode "1","L" or “RGBA ”(control which pixels get replaced)
paste(color,box=None,mask=None) 填充颜色 如果box省略,整个图被填充为color色;mask参数同上
point(function) 改变像素点(函数) Returns a new image with each pixel modified.
point(table) 改变像素点(查表) To translate pixels using a table(a sequence of 256n values, where n is the number of bands in the image) lookup
putalpha(band)

改变alpha通道

The pixels of the band image(same-sized,"L" or "1") replace the alpha band(A) of the original image(RGBA) in place.

putpixel(xy, color) 改变单个像素点颜色 Note that this method is relatively slow. For more extensive changes, use paste or theImageDraw module instead.
resize(size,filter=None) 调整大小  
rotate(theta)

旋转(围绕图片中心)

Any pixels that are not covered by rotation of the original image are set to black.

show()

显示图片

On Unix systems, this method runs the xv image viewer to display the image. 
On Windows boxes,the image is saved in BMP format and can be viewed using Paint. 
This can be useful for debugging.

split()

分离通道

返回各个通道的灰度图组成的元组
Returns a tuple containing each band of the original image as an image of mode "L". 
For example, applying this method to an "RGB" image produces a tuple of three images, one each for the red, green, and blue bands.

thumbnail(size,filter=None) 缩略图 Modifies in-place,Preserves aspect ratio
transform(xs, ys, Image.EXTENT, (x0,y0,x1,y1))  

Returns a transformed copy of the image. In the transformed image, the point originally at (x0,y0) will appear at (0,0), and point (x1,y1) will appear at (xs, ys).

transform(xs, ys, Image.AFFINE, (a,b,c,d,e,f)) affine变换

The values a through f are the first two rows of an affine transform matrix.
Each pixel at (x,y) in the resulting image comes from position (ax+by+c,dx+ey+f) in the input
image, rounded to the nearest pixel.

transpose(method) 翻转旋转 ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)

python, Image的更多相关文章

  1. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  2. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  3. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  4. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  5. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  6. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  7. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  8. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  9. python开发编译器

    引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...

  10. 关于解决python线上问题的几种有效技术

    工作后好久没上博客园了,虽然不是很忙,但也没学生时代闲了.今天上博客园,发现好多的文章都是年终总结,想想是不是自己也应该总结下,不过现在还没想好,等想好了再写吧.今天写写自己在工作后用到的技术干货,争 ...

随机推荐

  1. CentOS7.5 Python3安装pip报错:ModuleNotFoundError: No module named '_ctypes' --Python3

    1.问题:pyhontModuleNotFoundError: No module named '_ctypes'  操作系统:CentOS7.5 安装完Pyhotn3后(如何安装Python3,安装 ...

  2. 活动代码页437--修改windows的系统编码

    1.首先查看系统编码 win+R打开运行,输入cmd回车,打开命令提示符窗口,输入chcp回车,会查询当前系统的活动代码页,它指明了当前系统使用的编码: 或者,打开cmd后,点击cmd窗口左上角图标, ...

  3. 中国历史人物传记数据库 CBDB 若干表简介

    ''' 推荐使用SQLite版本的CBDB数据库 推荐使用SQlite Studio进行数据库的操作 免费,可视化操作,轻量级应用,无需配置,学习扩展性好,非常适合广大历史系学生. ''' 一 人物基 ...

  4. QSplineSeries QChartView绘制曲线

    参考资料: https://www.qtdebug.com/qtbook-paint-smooth-curve-qchart/ https://blog.csdn.net/liang19890820/ ...

  5. 浅谈jQuery的promise

    jquery中的Promise,也就是我们所知道的Deferred对象. 举例1: var data=""; function runAsync(){ var def = $.De ...

  6. Linux c 从文件当中读取任意一行的数据

    代码如下 #include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/ty ...

  7. es6新增 set

    Set 基本用法 ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成Set数据结构. var s = new Set(); [2, ...

  8. Python IDE:pycharm

    ------------------------------------------------------安装pycharm------------------------------------- ...

  9. vue导出excel

    1.按装依赖 cnpm install -S file-saver xlsx cnpm install -D script-loader 2.引入Blob.js和expor2Excal.js 3.在m ...

  10. Java学习笔记36(jdbc快速入门)

    JDBC: Java DataBase Connectivity 是java程序连接存取数据库的应用程序接口 (是sun公司的程序员定义的一套操作数据库的规则,然后下面的各个公司如:mysql,sql ...