How do you draw transparent polygons with Python?

后端 未结 7 1314
独厮守ぢ
独厮守ぢ 2020-12-05 10:28

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

7条回答
  •  误落风尘
    2020-12-05 10:59

    What I've had to do when using PIL to draw transparent images is create a color layer, an opacity layer with the polygon drawn on it, and composited them with the base layer as so:

    color_layer = Image.new('RGBA', base_layer.size, fill_rgb)
    alpha_mask = Image.new('L', base_layer.size, 0)
    alpha_mask_draw = ImageDraw.Draw(alpha_mask)
    alpha_mask_draw.polygon(self.outline, fill=fill_alpha)
    base_layer = Image.composite(color_layer, base_layer, alpha_mask)
    

    When using Image.Blend I had issues with strange outlining behaviors on the drawn polygons.

    The only issue with this approach is that the performance is abysmal when drawing a large number of reasonably sized polygons. A much faster solution would be something like "manually" drawing the polygon on a numpy array representation of the image.

提交回复
热议问题