如何使用Python绘制透明多边形? [英] How do you draw transparent polygons with Python?

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

问题描述

我正在使用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/

如果要绘制相对于彼此透明的多边形,则基本Image必须是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.

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

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