Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用PIL绘制箭头?_Python_Python 3.x_Python Imaging Library - Fatal编程技术网

Python 如何使用PIL绘制箭头?

Python 如何使用PIL绘制箭头?,python,python-3.x,python-imaging-library,Python,Python 3.x,Python Imaging Library,我需要画一支箭。用户发送箭头的开始坐标和结束坐标。我没有任何想法,怎么做。请帮帮我吧?PIL无法提供一种简单的箭头绘制方法,因此我建议将您的PIL图像转换为Numpy数组,并使用OpenCV、或matplotlib、或Cairo或魔杖,要在其上绘制箭头,然后转换回PIL图像: #!/usr/bin/env python3 import cv2 import numpy as np from PIL import Image # Create an empty solid blue i

我需要画一支箭。用户发送箭头的开始坐标和结束坐标。我没有任何想法,怎么做。请帮帮我吧?

PIL无法提供一种简单的箭头绘制方法,因此我建议将您的PIL图像转换为
Numpy
数组,并使用OpenCV、或matplotlib、或Cairo魔杖,要在其上绘制箭头,然后转换回PIL
图像

#!/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')

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


如果您真的非常想使用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')


关键词:Python、图像处理、PIL、枕头、箭头、箭头、箭头线、OpenCV。

PIL不提供绘制箭头的简单方法,因此我建议将您的PIL
图像
转换为
Numpy
数组,并使用OpenCVmatplotlib,或开罗,或魔杖,在其上画一个箭头,然后转换回PIL
图像

#!/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')

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


如果您真的非常想使用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')


关键词:Python、图像处理、PIL、枕头、箭头、箭头、箭头线、OpenCV。

我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案-点击空心蜱/支票旁边的选票计数。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案-点击空心蜱/支票旁边的选票计数。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢我如何在没有任何背景色的情况下绘制此箭头?我只需要有透明背景的箭头。我如何在没有任何背景颜色的情况下绘制此箭头?我只需要有透明背景的箭头。