Python1-Pillow库简单使用

Python1-Pillow库简单使用

    • 1.安装pillow
    • 2.打开和显示图像
    • 3.图像基本操作
    • 4.批量图像格式转换 save
    • 5.批量创建缩略图 thumbnail
    • 6.批量图像加文字、图片水印 blend composite
    • 7.批量调整图像大小resize


1.安装pillow

# pip安装或者conda安装
conda install pillow

2.打开和显示图像

PIL:Python Image Library

import PIL
from PIL import Image

img = PIL.Image.open("lena.png")
# img.show()

print(img.format, img.size, img.mode) # PNG (512, 512) RGBA
img = PIL.Image.open("lena_c.bmp")
print(img.format, img.size, img.mode) # BMP (512, 512) RGB

3.图像基本操作

copy()方法用于拷贝图像、crop()方法用于裁剪图像、paste()方法用于将一个图像粘贴(覆盖)在另一个图像上面、resize()用于调整图像大小、rotate()用于旋转和翻转、filter()用于图像过滤。

更多文档参考:pillow.readthedocs.org

使用PIL.Image模块中的new()可以创建一个给定模式和大小的新图像对象。例如创建一个新的大小为800x600的RGB图像:

img = PIL.Image.new('RGB', (800, 600))

下面图像基本操作的实例:把一幅图像的4个副本排成2x2网格,左上角是原始图像,右上、左下、右下方分别使用模块PIL.ImageFilter种定义的内置过滤器CONTOUR、EMBOSS、FIND_EDGES进行过滤。

import PIL
from PIL import Image
from PIL import ImageFilter


img = PIL.Image.open("lena_c.bmp")
print(img.format, img.size, img.mode) # BMP (512, 512) RGB
width, height = img.size

# 创建新图像,大小为原始图像的4倍
res = PIL.Image.new(img.mode, (2*width, 2*height))

# 把原始图像放在左上
res.paste(img, (0, 0, width, height))

# 把轮廓过滤CONTOUR的图像放置在右上
contour = img.filter(PIL.ImageFilter.CONTOUR)
res.paste(contour, (width, 0, 2*width, height))

# 把浮雕过滤EMBOSS的图像放置在坐下
emboss = img.filter(PIL.ImageFilter.EMBOSS)
res.paste(emboss, (0, height, width, 2*height))

# 把边缘过滤FIND_EDGES的图像放置在右下角
edges = img.filter(PIL.ImageFilter.FIND_EDGES)
res.paste(edges, (width, height, 2*width, 2*height))

# 显示结果图像
res.show()

4.批量图像格式转换 save

使用PIL.Image模块的open()函数打开磁盘图像文件时,会根据文件内容自动确定文件格式。使用Image对象的save()方法保存图像时,可以指定格式,从而实现格式转换。

glob模块可以使用通配符匹配文件名。例如glob.glob("c:\tmp\*.jpg"),可以返回指定目录下所有后缀为jpg的文件列表。

os.path.splitext()可以拆分文件名和后缀。

import sys
import glob
import os
import PIL.Image

img_path = sys.argv[1] + "/*." + sys.argv[2]
for infile in glob.glob(img_path):
    f, e = os.path.splitext(infile)
    outfile = f + "." + sys.argv[3]
    PIL.Image.open(infile).convert("RGB").save(outfile, sys.argv[3])
# python main.py . png jpeg

先转换为RGB,然后由RGB转换为指定的格式

注意jpg后缀的文件格式全名为JPEG。

5.批量创建缩略图 thumbnail

缩略图是网络开发或图像软件预览常用的一种基本技术,使用Image对象的thumbnail()方法,可以很方便地建立缩略图。

import sys
import glob
import os
import PIL.Image

img_path = sys.argv[1] + "/*." + sys.argv[2]
size=(128,128)
for infile in glob.glob(img_path):
    f, e = os.path.splitext(infile)
    outfile = f + "_s." + sys.argv[2]
    img = PIL.Image.open(infile)
    img.thumbnail(size)
    img.save(outfile)
# python main.py . jpg

thumbnail()函数是Pillow库中的一个方法,用于调整图像的大小。该函数可以根据指定的尺寸或比例自动调整图像的大小,同时保持图像的宽高比。

thumbnail(size, resample=None)
'''
size:一个元组或整数,指定缩略图的目标尺寸。如果size是一个整数,则图像将按照宽度和高度的相同比例进行缩放。如果size是一个元组,可以指定宽度和高度的具体值,或者使用其中一个维度值为None来自动计算缩放比例。

resample(可选):指定缩放算法。默认为None,表示使用默认的缩放算法。常用的选项包括Image.NEAREST(最近邻插值)、Image.BILINEAR(双线性插值)和Image.BICUBIC(双三次插值)。如果需要更高质量的缩放结果,可以选择Image.LANCZOS(Lanczos插值)
'''

6.批量图像加文字、图片水印 blend composite

使用Image模块的new函数可以创建水印图像对象,使用ImageDraw模块在水印图像上绘制文字,最后通过Image模块的composite函数合成水印图像和原图像。

# 把指定路径下所有*.jpg文件加上"Python"水印并另存为*_w.jpg
# python main.py . jpg Python
import sys
import glob
import os
from PIL import Image, ImageDraw, ImageFont

img_path = sys.argv[1] + "/*." + sys.argv[2]
img_suffix = sys.argv[2]
text = sys.argv[3]
# 水印字体文件
fnt = ImageFont.truetype('arial.ttf', 36)

for infile in glob.glob(img_path):
    f, e = os.path.splitext(infile)
    outfile = f + "_w." + sys.argv[2]
    img = Image.open(infile)

    # 创建水印图层
    wartermark = Image.new(img.mode, img.size)
    print(wartermark.size, wartermark.format,wartermark.mode)

    # 创建绘图对象 在水印图层上绘制水印文字
    draw = ImageDraw.Draw(wartermark)
    draw.text((50,60), text, font=fnt)

    # 将水印图层混合到原始图像上
    img_out = Image.blend(img, wartermark, 0.5)
    # img.save(outfile, "JPEG")
    img_out.show()

alpha_composite还有composite不成功。是因为要有alpha通道。下面加图片水印:使用png格式。

# python main.py . png Python
# 替换下面代码
r, g, b, alpha = img.split()
alpha = alpha.point(lambda i: i > 0 and 204)

# 将水印图层混合到原始图像上
img_out = Image.composite(img, wartermark, alpha)

7.批量调整图像大小resize

resize()函数是Pillow库中的一个方法,用于调整图像的尺寸。该函数可以根据指定的尺寸进行缩放或裁剪图像。

resize(size, resample=None, box=None)
from PIL import Image

# 打开图像
image = Image.open('image.jpg')

# 按照比例缩小图像
scale_factor = 0.5
new_width = int(image.width * scale_factor)
new_height = int(image.height * scale_factor)
resized_image = image.resize((new_width, new_height))

# 保存调整后的图像
resized_image.save('resized_image.jpg')
# python main.py . png 200 200 将当前目录下所有png格式调整到200x200
import sys
import glob
import os
from PIL import Image

img_path = sys.argv[1] + "/*." + sys.argv[2]
text = sys.argv[3]
img_size_width = int(sys.argv[3])
img_size_height = int(sys.argv[4])

for infile in glob.glob(img_path):
    f, e = os.path.splitext(infile)
    outfile = f + "_w." + sys.argv[2]
    img = Image.open(infile)
    img_out = img.resize((img_size_width, img_size_height))
    # img.save(outfile, "JPEG")
    img_out.show()

你可能感兴趣的:(Python,pillow,计算机视觉,python)