python Pillow画图总结

2022/4/13 17:12:44

本文主要是介绍python Pillow画图总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

官方介绍地址: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()



这篇关于python Pillow画图总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程