且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绘制透明多边形?

更新时间:2023-01-06 08:01:10

这是针对 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.