如何在不带轮廓的枕头中用Python绘制透明椭圆 [英] How to draw a transparent ellipse in Python with Pillow without outline

查看:107
本文介绍了如何在不带轮廓的枕头中用Python绘制透明椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Pillow绘制多个透明椭圆,我想绘制没有轮廓的椭圆.没有轮廓,我似乎无法使它工作.

I am trying to draw multiple transparent ellipses with Pillow, and I want to draw them without outlines. I can't seem to make it work without outlines.

以下是一些测试代码:

from PIL import Image, ImageDraw
w,h = 100,100
img = Image.new('RGB', (w,h),(255,255,255))
drw = ImageDraw.Draw(img,"RGBA")
drw.polygon([(50, 0), (100, 100), (0, 100)], (255, 0, 0, 125))
drw.polygon([(50,100), (100, 0), (0, 0)], (0, 255, 0, 125))
drw.ellipse([(40, 40), (w - 10, h - 10)], fill=(0,0,255,125), outline=None)
img.save('out.png', 'PNG')

(来自此处,进行了一些修改)

(from here, with some modifications)

输出

只有椭圆才有轮廓.为什么?如何避免这种情况?

Only the ellipse gets an outline. Why? How can I avoid this?

推荐答案

在研究了一些代码,查看了文档和一些源代码之后,我很确定,最有可能在 arc chord ellipse ,它们都在后台共享相同的代码.

After playing around with some codes, looking at the documentation and some source codes, I'm quite sure, that most likely there's some issue with the functions like arc, chord, ellipse, that all share the same code under the hood.

我创建了以下示例:

from matplotlib import pyplot as plt
from PIL import Image, ImageDraw


def example(outline_alpha=None, width=None):
    if outline_alpha is None:
        outline = None
    else:
        outline = (255, 255, 0, outline_alpha)
    if width is None:
        width = 0
    img = Image.new('RGB', (100, 100), (255, 255, 255))
    drw = ImageDraw.Draw(img, 'RGBA')
    drw.line([(0, 40), (100, 40)], (0, 0, 0, 255))
    drw.line([(50, 100), (100, 0)], (0, 0, 0, 255))
    drw.polygon([(50, 100), (100, 0), (0, 0)], (0, 255, 0, 128), outline)
    drw.ellipse([(40, 40), (90, 90)], (0, 0, 255, 128), outline, width)
    return img


plt.figure(1, figsize=(15, 10))
plt.subplot(2, 3, 1), plt.imshow(example()), plt.title('No outlines specified, width = 0')
plt.subplot(2, 3, 2), plt.imshow(example(255)), plt.title('Opaque outlines specified, width = 0')
plt.subplot(2, 3, 3), plt.imshow(example(128)), plt.title('Semi-transparent outlines specified, width = 0')
plt.subplot(2, 3, 4), plt.imshow(example(None, 5)), plt.title('No outlines specified, width = 5')
plt.subplot(2, 3, 5), plt.imshow(example(255, 5)), plt.title('Opaque outlines specified, width = 5')
plt.subplot(2, 3, 6), plt.imshow(example(20, 5)), plt.title('Semi-transparent outlines specified, width = 5')
plt.tight_layout()
plt.show()

输出如下:

查看多边形,如果未指定轮廓(左上图),我们将看到黑线可见,这是多边形的边界之一.指定不透明的轮廓(顶部中央图像),黑线将不再可见.设置一个半透明的轮廓(右上图)显示该轮廓与多边形的边界相同.

Looking at the polygon, if no outline is specified (top left image), we see that the black line is visible, which is one of the polygon's borders. Specifying an opaque outline (top center image), the black line's no longer visible. Setting a semi-transparent outline (top right image) reveals, that the outline is identical to the polygon's border.

现在,对于椭圆是相同的:如果未设置轮廓(左上),则仍显示轮廓,该轮廓很可能与fill参数使用的颜色相同,但未包含alpha值.设置不透明轮廓(位于顶部中心)会覆盖"意外存在的轮廓,但是当设置半透明轮廓时,我们会看到意外轮廓仍然存在.

Now, the same for the ellipse: If no outline is set (top left), an outline is shown nevertheless, most likely the same color as used for the fill parameter, but without incorporating an alpha value. Setting an opaque outline (top center) "overwrites" the unexpected existent outline, but when setting a semi-transparent outline, we see that the unexpected outline is still there.

当在ellipse中设置width > 1时,此效果变得更加明显,请参阅底行.意外的轮廓似乎仍然具有width = 1,而显式设置的轮廓具有width = 5.

This effect becomes even more obvious, when setting width > 1 in ellipse, see the bottom row. The unexpected outline still seems to have width = 1, whereas the explicitly set outline has width = 5.

再次,我很确定,这种行为不是故意的.我将在其GitHub问题跟踪器中打开一个问题. 编辑:我刚刚打开了此问题. 另一项编辑:已修复.

Again, I'm quite sure, that this behavior isn't intended – and I will open an issue in their GitHub issue tracker. I just opened this issue. ANOTHER It's fixed.

希望有帮助–不知何故...

Hope that helps – somehow...

这篇关于如何在不带轮廓的枕头中用Python绘制透明椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆