如何绘制透明多边形? [英] How to draw transparent polygons?

查看:52
本文介绍了如何绘制透明多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PIL(Python 成像库).我想绘制透明多边形.似乎指定包含 alpha 级别的填充颜色不起作用.是他们的解决方法吗?

I'm using PIL (Python Imaging Library). I'd like to draw transparent polygons. It seems that specifying a fill color that includes alpha level does not work. Are their workarounds?

如果使用 PIL 无法完成,我愿意使用其他东西.

If it can't be done using PIL I'm willing to use something else.

如果有多个解决方案,则应考虑性能.绘图需要尽可能快.

If there is more than one solution, then performance should be factored in. The drawing needs to be as fast as possible.

推荐答案

这是针对 Pillow 的,它是 PIL 的一个更加维护的分支.http://pillow.readthedocs.org/

This is for Pillow, a more maintained fork of PIL. http://pillow.readthedocs.org/

如果要绘制相对于彼此透明的多边形,基础图像必须是 RGB 类型,而不是 RGBA,并且 ImageDraw 必须是 RGBA 类型.示例:

If you want to draw polygons that are transparent, relative to each other, the base Image has to be of type RGB, not RGBA, and the ImageDraw has to be of type RGBA. Example:

from PIL import Image, ImageDraw

img = Image.new('RGB', (100, 100))
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))
del drw

img.save('out.png', 'PNG')

这将绘制两个重叠的三角形,它们的两种颜色混合.这比必须为每个多边形合成多个层"要快得多.

This will draw two triangles overlapping with their two colors blending. This a lot faster than having to composite multiple 'layers' for each polygon.

这篇关于如何绘制透明多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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