- 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/
在 python 解释器中: >>> import PIL >>> PIL.Image Traceback (most recent call last): File "", line 1, in
我在 Pillow 中遇到过这个奇怪的错误,其中导入名称 PIL 需要全部小写而不是全部大写,但我从未见过 pil在任何地方都使用小写字母。这意味着我使用的导入 PIL 的所有 python 包和文件
有没有办法使用 PIL 截取驻留在我的服务器上的指定 HTML/Javascript 页面的屏幕截图? 我想编写一个脚本来更改该 HTML 页面上的一些参数,然后让 PIL 对其进行屏幕截图。 有什么
这是我所做的描述。 我正在尝试使用 PIL 编写程序,但是在尝试导入它时(如下所示),出现错误(也如下所示)。 from PIL import Image Here is the error. Tra
我正在尝试对图像进行简单的裁剪。这是代码 from PIL.Image import Image def get_image_half(image, half="upper"): if hal
我是一名新的Python用户,也是“Stack Overflow”中的新用户,当我尝试编译 tensorflow 代码时,我遇到了一些问题,并且我无法从网站上找到答案,所以我想从这里获得一些帮助,先谢
我知道 stackoverflow 上已经有很多与此问题相关的问题,我已经阅读了所有问题,但我仍然没有成功解决此问题。我希望有人能帮我解决这个问题。 我已经安装并重新安装了 Pillow 10 次。我
我得到错误: --------------------------------------------------------------------------- ImportError
我是机器学习的初学者,所以我试图创建一个模型来识别 Keras 博客中引用的图像。我已经在 Windows 10 上安装了 Anaconda 3 以及所有软件包,如tensorflow、keras、s
我正在尝试使用过滤器 FIND_EDGES 从图片中获取边缘,它在我的 Windows PC 上工作,但是当我在我的 Raspberry Pi 上运行相同的代码时,它给出了图像模式错误的错误。 最佳答
这个问题在 Python 环境中有一些答案,但这些解决方案不适用于我的 RStudio 环境。这是我的代码: library(keras) library(tensorflow) use_condae
我正在使用 Mac OS x 10.10.3 Yosemite 和 Python 2.7.9 |Anaconda 2.2.0 (x86_64) 来处理很多 python 的东西。我正在使用 Eclip
我正在遵循这个 SageMaker 指南并使用 1.12 cpu docker 文件。 https://github.com/aws/sagemaker-tensorflow-serving-cont
`from PIL import Image, ImageDraw, ImageFont image = Image.new('RGB', (950, 250), color=(255, 255, 2
我知道如何从图片中找到边缘。但我希望轮廓边缘更厚,例如宽度 9。 from PIL import Image, ImageFilter image = Image.open('your
我有多个白色背景的 PNG 图像,并且图像的某些部分充满了图案(可能是不同的颜色,黑色、蓝色、红色、黄色等)。 如何使用 Python PIL 库将所有这些图像合并为一张图像,以便所有非白色部分出
目前我正在尝试裁剪以下地址下文件夹内的所有图像:C:\\Users\\xie\\Desktop\\tiff\\Bmp然后将它们重新保存到同一个文件夹中。下面是我试图试验的代码,两者都运行没有错误但什么
虽然它们非常相似,但我确信 Pearson 相关相似度和调整余弦相似度之间存在一些差异,因为所有的论文和网页都将它们分为两种不同的类型。 然而,它们都没有提供明确的定义。 Here是其中一页。 谁能说
这是我的forms.py: class UploadImageForm(forms.ModelForm): class Meta: model = UserImages
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我是一名优秀的程序员,十分优秀!