python - 如何使用 PIL 绘制箭头?

标签 python python-3.x python-imaging-library

<分区>

我需要用 PIL 画一个箭头.用户发送箭头的开始坐标和结束坐标。我没有任何想法。我怎样才能做到?

最佳答案

PIL 不提供绘制箭头的简单方法,因此我建议转换您的 PIL ImageNumPy数组并使用 OpenCV ,或 Matplotlib ,或 Cairo,或 Wand,在其上绘制箭头,然后转换回 PIL Image :

#!/usr/bin/env python3

import cv2
import numpy as np
from PIL import Image

# Create an empty solid blue image
w, h = 640, 480
im = Image.new('RGB', (w,h), (0,0,255))

# Make into Numpy array so we can use OpenCV drawing functions
na = np.array(im)

# Draw arrowed line, from 10,20 to w-40,h-60 in black with thickness 8 pixels
na = cv2.arrowedLine(na, (10,20), (w-40, h-60), (0,0,0), 8)

# Revert back to PIL Image and save
Image.fromarray(na).save('result.png')

Enter image description here

请注意,OpenCV 使用 BGR 而不是 RGB 排序,因此如果您想要 PIL 中的红线,则需要在 OpenCV 中使用 (0, 0, 255)


如果您真的非常想使用PIL 绘制带箭头的线条,您可以绘制一条线条,然后使用draw.polygon() 在其末端添加一个三角形。像这样:

#!/usr/bin/env python3

import math
import random
from PIL import Image, ImageDraw

def arrowedLine(im, ptA, ptB, width=1, color=(0,255,0)):
    """Draw line from ptA to ptB with arrowhead at ptB"""
    # Get drawing context
    draw = ImageDraw.Draw(im)
    # Draw the line without arrows
    draw.line((ptA,ptB), width=width, fill=color)

    # Now work out the arrowhead
    # = it will be a triangle with one vertex at ptB
    # - it will start at 95% of the length of the line
    # - it will extend 8 pixels either side of the line
    x0, y0 = ptA
    x1, y1 = ptB
    # Now we can work out the x,y coordinates of the bottom of the arrowhead triangle
    xb = 0.95*(x1-x0)+x0
    yb = 0.95*(y1-y0)+y0

    # Work out the other two vertices of the triangle
    # Check if line is vertical
    if x0==x1:
       vtx0 = (xb-5, yb)
       vtx1 = (xb+5, yb)
    # Check if line is horizontal
    elif y0==y1:
       vtx0 = (xb, yb+5)
       vtx1 = (xb, yb-5)
    else:
       alpha = math.atan2(y1-y0,x1-x0)-90*math.pi/180
       a = 8*math.cos(alpha)
       b = 8*math.sin(alpha)
       vtx0 = (xb+a, yb+b)
       vtx1 = (xb-a, yb-b)

    #draw.point((xb,yb), fill=(255,0,0))    # DEBUG: draw point of base in red - comment out draw.polygon() below if using this line
    #im.save('DEBUG-base.png')              # DEBUG: save

    # Now draw the arrowhead triangle
    draw.polygon([vtx0, vtx1, ptB], fill=color)
    return im

# Create an empty solid blue image
w, h = 640, 480
im = Image.new('RGB', (w,h), (0,0,255))

# Get some controlled randomness
random.seed(58)

# Draw some random arrows
for _ in range(10):
    ptA = (random.randint(0,w), random.randint(0,h))
    ptB = (random.randint(0,w), random.randint(0,h))
    im = arrowedLine(im, ptA, ptB)

# Save
im.save('result.png')

enter image description here

关键词:Python、图像处理、PIL、Pillow、arrow、arrows、arrowed line、OpenCV。

关于python - 如何使用 PIL 绘制箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63671018/

相关文章:

python - 从 numpy 数组转换为图像时获得意外的颜色输出

python - 函数如何在 python 中保持状态?

python - Django-Graphene : On a model ChoiceField, graphene 需要一个类型但得到一个值

python - 为什么 yield 生成的生成器比 xrange 生成的生成器快?

Python-如何从多索引数据框中选择行

python - 动态生成一定文件大小的有效图片用于测试

python - 使用 argparse 传递一个目录(不需要类型检查)

python - 如何测试tensorflow cifar10 cnn教程模型

python - 如何更新 python pandas 中的交叉表值

Python PIL - 透明线和虚线?