部分填充 PIL ImageDraw Line [英] Fill PIL ImageDraw Line partially
本文介绍了部分填充 PIL ImageDraw Line的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用不同的颜色填充一条线,越来越像进度条.这是图片:
I'm trying to fill a line by a different color increasingly like a progress bar. This is the image:
它是用这个代码创建的
from PIL import Image, ImageDraw
image = Image.new("RGBA", (300, 300), color="black")
draw = ImageDraw.Draw(image)
width = 7
image_w, image_h = image.size
coord_a = image_w / 2, width
coord_b = width, image_h / 2
coord_c = image_w / 2, image_h - width
coord_d = image_w - width, image_h / 2
draw.line([coord_a, coord_b, coord_c, coord_d, coord_a], fill="red", width=width, joint="curve")
image.show()
image.save("test.png")
我正在尝试用不同的颜色填充它,如下所示:
我应该单独填写每一行并将它们全部合并吗?
I'm trying to fill it with different color like this:
Should I just fill each line separately and combine them all?
推荐答案
有趣的问题!想出实现此目的的方法可能会很有趣.
Interesting question! You could have lots of fun thinking up ways to do this.
- 按照您的建议,您可以将菱形绘制为四条单独的线.您必须使用 sin/cos 计算红色和蓝色部分相交的点,但这并不难.
- 您可以更简单地绘制它,因为正方形的四个边最初是水平和垂直的,然后在完成绘制后将其旋转 45 度到位.我想我会选择这个选项.
- 您可以绘制一条长水平的红线,然后以蓝色过度绘制正确的百分比.然后剪成四份,旋转粘贴到黑色方块背景上.
- 您可以使用 scikit-image
draw.polygon_perimeter()
获取菱形上所有点的坐标,如此处.然后将第一个着色为蓝色,其余为红色.您可以使用形态学扩张使线条更粗.
- You could get the coordinates of all the points on the rhombus using scikit-image
draw.polygon_perimeter()
as documented here. Then colour the first however many percent blue and the remainder in red. You could make the lines thicker using morphological dilation.
这篇关于部分填充 PIL ImageDraw Line的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文