文章目录
Pillow(PIL Fork) Image模块
Related Links
Pillow(PIL)入门教程(非常详细) (biancheng.net)
Pillow (PIL Fork) 10.3.0.dev0 documentation
Image class
PIL中最重要的类,表示一张图片对象,在Image模块中定义。有多种方法实例化这个类,例如通过加载文件,处理其他图片和生成图片。
加载图像
from PIL import Image
im = Image.open("logo.png")
'''
下述方法等价
with Image.open("logo.png") as im
...
with open("logo.png", "rb") as f:
im3 = Image.open(f)
...
with open("logo.png", "rb") as f:
im4 = Image.open(io.BytesIO(f.read()))
'''
创建图像
包含PIL.Image.new(),PIL.Image.fromarray(), PIL.Image.frombytes(),PIL.Image.frombuffer()等
具体参照Image Module - Pillow (PIL Fork) 10.3.0.dev0 documentation
新图像
PIL.Image.new(mode, size, color=0)
参数:
- mode 新图像的mode
- size 宽高
- coloe 图片的颜色,默认为黑色,为None,图片不会被初始化
从矩阵中创建
PIL.Image.fromarray(obj, mode=None)
参数:
- obj 矩阵接口的对象
- mode 可选读取对象使用的模式
from PIL import Image
import numpy as np
a = np.zeros((5, 5))
im = Image.fromarray(a)
对于NumPy,Pillow的模式并不总是匹配,Pillow只提供1-bit,8-bit,32-bit符号整型和32-bit浮点像素。
mode不会转换数据,而是改变数据读取的方式
from PIL import Image
import numpy as np
a = np.full((1, 1), 300)
im = Image.fromarray(a, mode="L")
im.getpixel((0, 0)) # 44
im = Image.fromarray(a, mode="RGB")
im.getpixel((0, 0)) # (44, 1, 0)
Pillow图像同样可以转换为矩阵
from PIL import Image
import numpy as np
im = Image.open("hopper.jpg")
a = np.asarray(im)
生成图像
生成中心为128的高斯噪声
PIL.Image.effect_noise(size, sigma)
参数:
- size 尺寸
- sigma 标准差
from PIL import Image
import numpy as np
a = np.full((1, 1), 300)
im = Image.fromarray(a, mode="F")
im = Image.effect_noise((128,128),20)
im.show()
方法
显示
Image.show(title=None)
保存
Image.save(fp, format=None, **params)
参数:
- fp 文件名
- format 可选格式覆盖,如果省略将使用文件名的扩展名
- params 给图像writer的额外数据
格式转换
Image.convert(mode=None, matrix=None, dither=None, palette=Palette.WEB, colors=256)
参数:
- mode 目标图像的mode
- matrix 可选项 转换矩阵
- dither 当模式从"RGB"转换到"P"或"L"到“1”使用
- plette 当模式从"RGB"转换到"P"使用
- colors Palette.ADAPTIVE 调色板使用的颜色数目
from PIL import Image
im = Image.open("logo.png")
#此时返回一个新的image对象,转换图片模式
image=im.convert('RGB')
复制
Image.copy()
from PIL import Image
im = Image.open("logo.png")
im_copy = im.copy()
im_copy.show()
裁剪
Image.crop(box=None)
参数:
- box 裁剪的矩形 (left, upper, right, lower)元组,可以看作左上点(left, upper)和右下点(right, lower),两点确定一个矩形框
from PIL import Image
with Image.open("hopper.jpg") as im:
# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (20, 20, 100, 100)
# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))
draft
Image.draft(mode, size)
返回一个尽可能接近给定mode和尺寸的图像,可以用来做彩色Jpeg到灰度图转换,但这种方式是原地的。
参数
-
mode 目标mode
-
size 目标尺寸
图像缩放
Image.resize(size, resample=None, box=None, reducing_gap=None)
返回一个缩放副本
参数:
- size 目标尺寸
- resample 可选重采样filter
- box 可选原图片的缩放区域
- reducing_gap 可选优化图片的缩放效果
from PIL import Image
with Image.open("hopper.jpg") as im:
# Provide the target width and height of the image
(width, height) = (im.width // 2, im.height // 2)
im_resized = im.resize((width, height))
图像旋转
Image.rotate(angle, resample=Resampling.NEAREST, expand=0, center=None, translate=None, fillcolor=None)
参数:
- angle 顺时针角度
- resample 重采样滤波器
- expand 可选表示是否对图像扩展
- center 可选旋转中心
- translate 可选旋转后的图像平移
- fillcolor 可选旋转后图像之外的颜色填充
from PIL import Image
im = Image.open("logo.png")
im_out=im.rotate(45)
im_out.show()
图像翻转
Image.transpose(method)
参数:
- method Transpose.FLIP_LEFT_RIGHT(水平翻转) Transpose.FLIP_TOP_BOTTOM(上下翻转), Transpose.ROTATE_90, Transpose.ROTATE_180, Transpose.ROTATE_270, Transpose.TRANSPOSE or Transpose.TRANSVERSE.
对象属性
参照Pillow Image对象属性 (biancheng.net)
图像模式
1
1x1位像素L
1x8位像素 灰度P
1x8位像素 使用调色板映射到任何其他模式RGB
(3x8位 真彩色)RGBA
(4x8位 真彩色 + 透明通道)CMYK
(4x8位 印刷色彩 )YCbCr
(3x8位 彩色视频格式)LAB
(3x8位 Lab色彩空间)HSV
(3x8位 色相 饱和度 色明度 )I
(1x32 位有符号整数)F
(1x32 位浮点像素)
图像生命周期
-
Image.open() 会读取打开文件的Metadata,文件保持打开状态以供进一步处理
-
Image.Image.load() 当需要像素数据时,会调用load(),当前帧读取到内存中,任何基于其他图像创建的图像会内部调用原图像load()然后读取数据。如果是单帧图像,在当前帧读取后需要关闭文件,多帧图像(TIFF,或GIF)保持打开,正常情况下不需要调用,图像类会自动加载。
-
Image.Image.close()会关闭文件并摧毁core图像对象,Pillow上下文控制器也会关闭文件,但是不会摧毁图像对象。当文件关闭后,需要文件访问的操作会失败。
with Image.open("test.jpg") as img:
img.load()
assert img.fp is None
img.save("test.png")
with open("test.jpg", "rb") as f:
im5 = Image.open(f)
im5.load() # FAILS, closed file
with Image.open("test.jpg") as im6:
pass
im6.load() # FAILS, closed file