pip install pillow
Image
对象 r
from PIL import Image
im = Image.open(path)
Image
对象 Image
对象1Image
对象2from PIL import Image
im1 = Image.open(path1)
im2 = Image.open(path2)
im3 = Image.alpha_composite(im1,im2)
Image
对象 Image
对象1Image
对象2如果alpha为0.0,则返回第一个图像的副本。如果alpha为1.0,则返回第二个图像的副本,基本的算法如下:
out = image1 * (1.0 - alpha ) + image2 * alpha
Image
对象 Image
对象from PIL import Image
def func(a):
return a
im1 = Image.open(path1)
img = Image.eval(img1,func,1)
Image
对象 Pillow支持的模式表
Image
对象。烦会一个Image
对象 Pillow支持的模式表
ImageColor
对象from PIL import Image
# 单个整数值
img = Image.new("RGBA",(1024,768),215)
# 元组
img = Image.new("RGBA",(1024,768),(215,0,0)
# ImageColor
from PIL import ImageColor
color = ImageColor.getrgb("#FF0000")
img = Image.new("RGBA",(1024,768),color)
img.show()
从上面代码运行结果显示是一个红色,1024*768的图像
alpha_composite(im, dest=(0,0), source=(0,0)):在Image
对象中符合im
,效果与类方法alpha_composite
相似。无返回值
Image
对象copy():复制此图片
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
img_copy = img.copy()
Image
对象 from PIL import Image
img = Image.new("RGBA",(1024,768),215)
img_copy = img.crop(box=(0,0,500,500))
draft(mode, size):配置图像文件加载器,以便返回尽可能接近给定模式和大小的图像版本,无返回值
filter(filter):使用给定的过滤器过滤此图像,返回一个Image
对象
getbands():获取此图像中每个波段名称的元组。返回一个tuple
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.getbands() # ('R', 'G', 'B', 'A')
tuple
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.getbbox() # (0, 0, 1024, 768)
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.getcolors() # [(786432, (215, 0, 0, 0))]
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
for item in img.getdata():
print item
# 打印结果:
(215, 0, 0, 0)
(215, 0, 0, 0)
(215, 0, 0, 0)
...
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.getextrema() # ((215, 215), (0, 0), (0, 0), (0, 0))
list
对象。如果没有调色板,则返回Nonefrom PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.getpalette() # None
tuple
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.getpixel((500,500)) # (215, 0, 0, 0)
import os
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
img = Image.new("RGBA",(1024,768),215)
img1.paste(img)
img1.show()
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
img_c = Image.new("RGBA",(1024,768),-100)
img.putdata(img_c.getdata())
img.show()
P
或者L
。返回一个Image
对象 from PIL import Image
img = Image.new("P",(1024,768),215)
img_c = Image.new("P",(1024,768),-100)
img_c.putpalette(img.getpalette())
img_c.show()
P
模式图像,返回一个Image
对象 from PIL import Image
img = Image.new("RGBA",(1024,768),215)
img_q = img.quantize(colors=256,method=2)
print img_q # Image.Image image mode=P size=1024x768 at 0x2BF7E80>
Image
对象 Image.NEAREST
、Image.BOX
、Image.BILINEAR
、Image.HAMMING
、Image.BICUBIC
或者Image.LANCZOS
。如果省略,或者图像模式为1
或者P
,则设置Image.NEAREST
。from PIL import Image
img = Image.new("RGBA",(1024,768),215)
img_r = img.resize(size=(500,500))
print img_r # Image.Image image mode=RGBA size=500x500 at 0x37A6E80>
Image
对象 Image.NEAREST
、Image.BILINEAR
或者Image.BICUBIC
。如果省略,或者图像模式为1
或者P
,则设置Image.NEAREST
。import os
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
img_r = img1.rotate(45,Image.BICUBIC)
img_r.show()
可以看到,图像已经逆时针旋转了45度
import os
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
img_r = img1.rotate(45,Image.BICUBIC)
img_r.save(os.path.join(os.getcwd(),"rotate.png"))
seek(frame):在这个序列文件中寻找给定的帧。如果您在序列结束之外寻找方法,则会 引发EOFError异常。当序列文件被打开时,库会自动寻找0帧。无返回值
show(title=None, command=None):显示这个图像,此方法主要用于调试目的。无返回值
tuple
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
data = img1.split()
print data # (<PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC438>, <PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC860>, <PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC898>, <PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC8D0>)
Image
对象 from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
im = img1.getchannel(0)
或者:
im = img1.getchannel("R")
int
Image.NEAREST
、Image.BILINEAR
、Image.BICUBIC
或者Image.LANCZOS
。如果省略,则默认为Image.BICUBIC
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
img1.thumbnail(size=(500,500),resample=Image.BICUBIC)
print img1 #
1
的图像,返回一个str
from PIL import Image
img = Image.new("1",(1024,768),215)
data = img.tobitmap(name='abc')
print data
# 结果如下:
"""
#define abc_width 1024
#define abc_height 768
static char abc_bits[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
...
};
"""
str
对象Image
对象 Image.FLIP_LEFT_RIGHT
、Image.FLIP_TOP_BOTTOM
、Image.ROTATE_90
、Image.ROTATE_180
、Image.ROTATE_270
、Image.TRANSPOSE
或者Image.TRANSVERSE
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img1 = Image.open(path1)
im = img1.transpose(Image.FLIP_LEFT_RIGHT)
im.show()
可以看出图像已经翻转了
open
方法构建的图像对象才具有此属性import os
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img = Image.open(path1)
print img.filename # 、/aaa/bbb/ccc/23.png
None
import os
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img = Image.open(path1)
print img.format # PNG
img = Image.new("RGBA",(1024,768),215)
print img.format # None
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.mode # RGBA
tuple
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.size # (1024, 768)
int
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.width # 1024
int
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.height # 768
P
,这应该是ImagePalette
类的一个实例。否则为None
from PIL import Image
img = Image.new("RGBA",(1024,768),215)
print img.palette # None
img = Image.new("P",(1024,768),215)
print img.palette #
import os
from PIL import Image
path1 = os.path.join(os.getcwd(),"23.png")
img = Image.open(path1)
print img.info
# 结果如下:
'''
{
'chromaticity': (0.31269, 0.32899, 0.63999, 0.33001, 0.3, 0.6, 0.15, 0.05999),
'icc_profile': 'xxxx/...',
'dpi': (300, 300)
}
'''
img = Image.new("RGBA",(1024,768),215)
print img.info # {}
模式 | 说明 |
---|---|
1 | 1位像素,黑白,每字节一个像素存储 |
L | 8位像素,黑白 |
P | 8位像素,使用调色板映射到任何其他模式 |
RGB | 3x8位像素,真彩色 |
RGBA | 4×8位像素,带透明度掩模的真彩色 |
CMYK | 4x8位像素,分色 |
YCbCr | 3x8位像素,彩色视频格式 |
LAB | 3×8位像素,L * a * b颜色空间 |
HSV | 3x8位像素,色调,饱和度,值颜色空间 |
I | 32位有符号整数像素 |
F | 32位浮点像素 |
更多关于Image的操作:http://pillow.readthedocs.io/en/latest/reference/Image.html