官方介绍地址:https://pillow.readthedocs.io/en/stable/reference/index.html
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name: Pillow测试用例
# Author: yunhgu
# Date: 2022/4/13 9:02
# Description:
# -------------------------------------------------------------------------------
from PIL import ImageDraw, ImageFont, Image
img_file1 = "img_1.png"
img_file2 = "img_2.png"
# 读2取图片
img = Image.open(img_file1)
img2 = Image.open(img_file2)
# 旋转图片
img.rotate(45).show()
# 切割图片,左上右下坐标
img.crop((10, 10, 50, 50)).show()
# 缩略图
size = 120, 120
img.thumbnail(size)
# 阿尔法复合,将img2复合到img
img = img.convert("RGBA")
img2 = img2.convert("RGBA")
alpha_img = Image.alpha_composite(img, img2)
alpha_img.show()
# blend图
# out = image1 * (1.0 - alpha) + image2 * alpha
img = img.convert("RGBA")
img2 = img2.convert("RGBA")
blend_img = Image.blend(img, img2, 0.3)
blend_img.show()
# 合成图
img = img.convert("RGBA")
composite_img = Image.composite(img, img2, img)
composite_img.show()
# 将一组单波段图像合并为一个新的多波段图像
img = img.convert("RGBA")
r, g, b, a = img.split()
im1 = Image.merge('RGB', (r, g, r))
im1.show()
# 创建画板
draw = ImageDraw.Draw(img)
# 写文字
font = ImageFont.truetype("simkai.ttf", 15)
draw.text((10, 10), "hello,世界你好", font=font, fill="blue")
img.show()
# 画圆形轮廓的一部分
xy = [(10, 10), (500, 500)] # 边界框
draw.arc(xy, start=0, end=360, fill="red", width=2)
img.show()
xy = [(10, 10), (500, 500)] # 边界框
draw.chord(xy, start=0, end=360, outline="red", width=2)
img.show()
# 画圆
xy = [(10, 10), (500, 500)] # 边界框
draw.ellipse(xy, outline="red", width=1)
img.show()
# 画线
xy = [(0, 0), (10, 10), (100, 200), (200, 300)]
draw.line(xy, fill="blue", width=1)
img.show()
# 画点
xy = [(0, 0), (10, 10), (100, 200), (200, 300)]
draw.point(xy, fill="blue") # 无法控制大小,只能是1像素
img.show()
# 画点-可调大小
x, y, r = 100, 100, 5
draw.ellipse((x - r, y - r, x + r, y + r), fill='blue')
img.show()
# 多边形
xy = [(0, 0), (10, 10), (100, 200), (200, 300)]
draw.polygon(xy, outline="blue")
img.show()
# 正多边形,eg:三角形,四角形,五角形
bounding_circle = (100, 100, 100)
draw.regular_polygon(bounding_circle, n_sides=3, rotation=0)
img.show()
# 矩形
xy = [(10, 10), (100, 100)]
draw.rectangle(xy, outline="blue")
img.show()
# 圆角矩形
xy = [(10, 10), (100, 100)]
draw.rounded_rectangle(xy, radius=10, outline="blue")
img.show()