- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用PIL绘制一个虚线矩形。
我知道我可以绘制 4 条不同的虚线,使其看起来像一个矩形:
for x in range(0, Width_of_image, 5):
d.line([(x, 30), (x + 2, 30)],fill ="black", width =2)
但是有没有办法使用draw.rectangle
绘制一个矩形,即不绘制4条不同的线?
最佳答案
正如其他用户所说,PIL 库(特别是 PIL.ImageDraw.ImageDraw
)不提供绘制虚线的功能。我查看了StackOverflow问题(以及其他地方)有关PIL中虚线的问题,但我还没有找到在PIL中绘制虚线的令人满意的方法。我找到的答案都没有以预期的方式绘制虚线(其他 Python 库提供虚线的方式,例如使用 Tkinter canvas )。
所以我编写了类DashedImageDraw
(它扩展了PIL.ImageDraw.ImageDraw
),它具有绘制虚线和虚线矩形的功能。以下是代码和如何使用它的示例:
from PIL import Image, ImageDraw
import math
class DashedImageDraw(ImageDraw.ImageDraw):
def thick_line(self, xy, direction, fill=None, width=0):
#xy – Sequence of 2-tuples like [(x, y), (x, y), ...]
#direction – Sequence of 2-tuples like [(x, y), (x, y), ...]
if xy[0] != xy[1]:
self.line(xy, fill = fill, width = width)
else:
x1, y1 = xy[0]
dx1, dy1 = direction[0]
dx2, dy2 = direction[1]
if dy2 - dy1 < 0:
x1 -= 1
if dx2 - dx1 < 0:
y1 -= 1
if dy2 - dy1 != 0:
if dx2 - dx1 != 0:
k = - (dx2 - dx1)/(dy2 - dy1)
a = 1/math.sqrt(1 + k**2)
b = (width*a - 1) /2
else:
k = 0
b = (width - 1)/2
x3 = x1 - math.floor(b)
y3 = y1 - int(k*b)
x4 = x1 + math.ceil(b)
y4 = y1 + int(k*b)
else:
x3 = x1
y3 = y1 - math.floor((width - 1)/2)
x4 = x1
y4 = y1 + math.ceil((width - 1)/2)
self.line([(x3, y3), (x4, y4)], fill = fill, width = 1)
return
def dashed_line(self, xy, dash=(2,2), fill=None, width=0):
#xy – Sequence of 2-tuples like [(x, y), (x, y), ...]
for i in range(len(xy) - 1):
x1, y1 = xy[i]
x2, y2 = xy[i + 1]
x_length = x2 - x1
y_length = y2 - y1
length = math.sqrt(x_length**2 + y_length**2)
dash_enabled = True
postion = 0
while postion <= length:
for dash_step in dash:
if postion > length:
break
if dash_enabled:
start = postion/length
end = min((postion + dash_step - 1) / length, 1)
self.thick_line([(round(x1 + start*x_length),
round(y1 + start*y_length)),
(round(x1 + end*x_length),
round(y1 + end*y_length))],
xy, fill, width)
dash_enabled = not dash_enabled
postion += dash_step
return
def dashed_rectangle(self, xy, dash=(2,2), outline=None, width=0):
#xy - Sequence of [(x1, y1), (x2, y2)] where (x1, y1) is top left corner and (x2, y2) is bottom right corner
x1, y1 = xy[0]
x2, y2 = xy[1]
halfwidth1 = math.floor((width - 1)/2)
halfwidth2 = math.ceil((width - 1)/2)
min_dash_gap = min(dash[1::2])
end_change1 = halfwidth1 + min_dash_gap + 1
end_change2 = halfwidth2 + min_dash_gap + 1
odd_width_change = (width - 1)%2
self.dashed_line([(x1 - halfwidth1, y1), (x2 - end_change1, y1)],
dash, outline, width)
self.dashed_line([(x2, y1 - halfwidth1), (x2, y2 - end_change1)],
dash, outline, width)
self.dashed_line([(x2 + halfwidth2, y2 + odd_width_change),
(x1 + end_change2, y2 + odd_width_change)],
dash, outline, width)
self.dashed_line([(x1 + odd_width_change, y2 + halfwidth2),
(x1 + odd_width_change, y1 + end_change2)],
dash, outline, width)
return
image = Image.new('RGB', (300, 200), (255, 255, 255))
d = DashedImageDraw(image)
d.dashed_rectangle([(20, 20), (280, 180)],
dash = (5, 3), outline = 'black', width = 2)
image.save("image.png", "PNG")
这条线绘制了矩形:
d.dashed_rectangle([(20, 20), (280, 180)], dash = (5, 3), outline = 'black', width = 2)
(20, 20)
是左上角,(280, 180)
是右下角,outline
是颜色线的宽度,width
是线的宽度,dash 是表示虚线模式的元组。例如:
dash = (5, 3) 将画线:
----- ----- ----- ----- ----- -----
dash = (4, 2, 3, 1) 将绘制线条:
---- --- ---- --- ---- --- ---- --- ----
这是上面代码生成的虚线矩形的图像:
关于python - 从 PIL 中绘制点线或虚线矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64276513/
最近,我在各种俄罗斯Web 2.0网站中看到了点缀和虚线超链接的广泛使用。通常,此类链接(在其下方具有虚线或虚线,而不是普通的实线)不会将用户引导至另一页面,而是在同一页面上执行操作而不重新加载该页面
Google 没有帮助我找到问题的解决方案。 (据我所知...)。 我想知道是否可以在 Android 上画一 strip 有 9 个补丁功能的虚线。 我的目标是从一个布局到另一个布局画一条虚线。 我
有没有办法将多条线段视为一条线? IE:我将鼠标悬停在一个上,两个都突出显示,切换图例中的可见性将隐藏两个部分。 http://jsfiddle.net/rayholland/HSvBj/2/ ser
我在我的 C# WinForms 应用程序中使用 .net 4.0 的图表控件。我有两个系列的数据显示为折线图。 我绘制的基本上是作为时间函数的供求关系图。我希望需求是某种颜色的实线,供应是相同颜色的
如何在窗口调整大小期间避免线划线调整大小?考虑下图: http://leprastuff.ru/data/img/20130315/b83eea4a7a3f07ca53a0e118ddbb9230.G
我正在尝试使用纯 webgl 创建虚线。我知道这已经有一个问题,也许我很笨,但我不知道如何让它发挥作用。我理解这个概念,但我不知道如何在着色器中获取沿路径的距离。以前的答案有以下行: varying
我们应该在序列图中的哪些步骤中使用返回线? )没有返回箭头,因为它们隐式返回实例化对象 编辑 2 针对您更新的问题: 我不会有用户操作的返回箭头,例如 login(),因为结果不会以与对象相同的方式
我正在尝试使用 gnuplot v4.4 绘制一个包含虚线和连续线的图形。代码是: set term postscript eps enhanced color set style line 1 li
我正在尝试创建一个 React nivo 折线图,其中包含虚线而不是实线。我研究过图案,但不知道如何制作图案。感谢您的帮助。 最佳答案 nivo 在库中提供了自定义图层功能,您可以使用它来自定义从实线
我正在尝试创建一个 React nivo 折线图,其中包含虚线而不是实线。我研究过图案,但不知道如何制作图案。感谢您的帮助。 最佳答案 nivo 在库中提供了自定义图层功能,您可以使用它来自定义从实线
我使用一些通过的点创建虚线 UIBezierPath。 let pathLayer = CAShapeLayer() pathLayer.strokeColor = UIColor.blue.cgCo
我有以下问题。我必须从很多距离太近的点画一个圆。您可以在下面的 jsfiddle 链接中看到它: http://jsfiddle.net/L6e5oz3L/
我是 CSS 的新手,正在网上寻找自动重新压缩图像的代码,我发现的代码可以完美地做到这一点。但正确的是,它重新调整图像周围的一些红色虚线,当我尝试编辑时,整个图像消失了。可以请有人帮我解决图像显示时如
已将 Firebase Analytics 集成到我的 Android 项目中,它上个月运行正常,现在它只显示虚线并且用户数为 0。 不确定为什么会这样,我哪里错了?由于它之前工作正常,我相信我已经按
我正在使用 OpenGl ES 1.0 开发我的 Android 游戏,我想绘制虚线现在我使用这段代码来画我的线: gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // g
我正在尝试绘制 1 像素的虚线,但它总是绘制 2 像素的线。当前解决方案: let shapelayer = CAShapeLayer() let path = UIBezierPath() path
我这里有一个用 html 编码的菜单,但我需要一条虚线来跨越名称和价格,我将如何在这里做呢?我有点迷路了哈哈。 你可以在这里看到它。 http://mystycs.com/menu/menuifram
是否可以在 Chart.js 折线图上制作虚线?并使图表全宽?见附件样机。 这是我当前的代码(只是简单的例子): var ctx = document.getElementById("main
如何删除 JTabbedPane 上当前选定选项卡周围的虚线。 例如: 看到它周围的虚线了吗? 最佳答案 我刚刚找到了一个更好的方法: component.setFocusable(false); 关
我正在尝试使用 rgl 绘制一些 3d 形状,但我想使用虚线/虚线连接我的点。我怎样才能做到这一点?目前我有 require(rgl) p1 <- c(0,0,0) p2 <- c(1,0,0) p3
我是一名优秀的程序员,十分优秀!