31

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?

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
carrier
  • 32,209
  • 23
  • 76
  • 99

8 Answers8

78

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

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(xy=[(50, 0), (100, 100), (0, 100)], fill=(255, 0, 0, 125))
drw.polygon(xy=[(50, 100), (100, 0), (0, 0)], fill=(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.

David Parks
  • 30,789
  • 47
  • 185
  • 328
Matt
  • 781
  • 5
  • 4
6

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.

akdom
  • 32,264
  • 27
  • 73
  • 79
3

PIL's Image module provides a blend method.

Create a second image the same size as your first, with a black background. Draw your polygon on it (with full colour). Then call Image.blend passing the two images and an alpha level. It returns a third image, which should have a semi-transparent polygon on it.

I haven't measured the performance (hey, I haven't even tried it!) so I cannot comment on it's suitability. I suggest you work out your performance budget, and then measure it to see if it is fast enough for your purposes.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121
2

I'm using cairo + pycairo for this, and it works well. And you can share image data between PIL and cairo, using python buffer interface, if there is operation in pil that can't be done in cairo.

2

To do that you can use Shapely and OpenCV like this:

import cv2
import numpy as np
from shapely.geometry import Polygon

alpha = 0.5 # that's your transparency factor
path = 'path_to_image.jpg'
image = cv2.imread(path)
(H, W) = image.shape[:2]

xmin = 0
ymin = 0 
xmax = int(W / 2)
ymax = int(H / 2)

polygon = Polygon([(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)])
int_coords = lambda x: np.array(x).round().astype(np.int32)
exterior = [int_coords(polygon.exterior.coords)]

overlay = image.copy()
cv2.fillPoly(overlay, exterior, color=(255, 255, 0))
cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
cv2.imshow("Polygon", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
1

From what I have found it can't be done directly with PIL. Here is a solution with PyCairo. Cairo is also used by Mozilla, GTX+, Mono, Inkscape, and WebKit so I think it is a safe to use in terms of future support. It can also be done with aggdraw, an optional add-on for PIL. See my listed source for more details. Python version 2.7.3 is used.

Source: http://livingcode.org/2008/12/14/drawing-with-opacity.1.html

Helper file: random_polys_util.py

    MIN_ALPHA = 50
    MAX_ALPHA = 100

    WIDTH = 500
    HEIGHT = 250

    #
    #   Utilities
    #
    def hex2tuple(hex_color):
        return tuple([int(hex_color[i:i+2], 16) for i in range(1,9,2)])

    def tuple2hex(tuple_color):
        return "#%0.2X%0.2X%0.2X%0.2X" % tuple_color

    def ints2floats(tuple_color):
        return tuple([c / 255.0 for c in tuple_color])

    def inc_point(p, dp):
        return (p[0] + dp[0]) % WIDTH, (p[1] + dp[1]) % HEIGHT

    def inc_triangle(t, dt):
        return tuple([inc_point(t[i], dt[i]) for i in range(3)])

    def inc_color(c, dc):
        new_c = [(c[i] + dc[i]) % 256 for i in range(3)]
        new_a = (c[3] + dc[3]) % MAX_ALPHA
        if new_a < MIN_ALPHA: new_a += MIN_ALPHA
        new_c.append(new_a)
        return tuple(new_c)

    def draw_all(draw_fn):
        triangle = start_t
        color = start_c
        for i in range(50):
            triangle = inc_triangle(triangle, dt)
            color = inc_color(color, dc)
            draw_fn(triangle, color)

    #
    #   Starting and incrementing values
    #
    start_c = hex2tuple('E6A20644')
    start_t = (127, 132), (341, 171), (434, 125)
    dt = (107, 23), (47, 73), (13, 97)
    dc = 61, 113, 109, 41

Main file: random_polys.py

from random_polys_util import *

def cairo_poly(pts, clr):
    ctx.set_source_rgba(*ints2floats(clr))
    ctx.move_to(*pts[-1])
    for pt in pts:
        ctx.line_to(*pt)
    ctx.close_path()
    ctx.fill()

def cairo_main():
    # Setup Cairo
    import cairo
    global ctx
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)
    # fill background white
    cairo_poly(((0,0),(WIDTH,0),(WIDTH,HEIGHT),(0,HEIGHT)),(255,255,255,255))
    draw_all(cairo_poly)
    surface.write_to_png('cairo_example.png')

def main():
    cairo_main()

if __name__ == "__main__":
    main()
Matt Campbell
  • 289
  • 2
  • 9
0

I had to draw an outside polygon with an outline, and subtract inner polygons (a common operation in GIS). Works like a charm using color (255,255,255,0).

image = Image.new("RGBA", (100,100))
drawing = ImageDraw.Draw(i)
for index, p in enumerate(polygons):
    if index == 0:
        options = { 'fill': "#AA5544",
                    'outline': "#993300"}
    else:
        options = {'fill': (255,255,255,0)}
    drawing.polygon( p, **options )

buf= StringIO.StringIO()
i.save(buf, format= 'PNG')
# do something with buf
ivy
  • 5,539
  • 1
  • 34
  • 48
-1

The answer with the highest score will not save real transparent png files. We need to first create an image with RGBA mode filled with zeros including the alpha channel. Then we can draw anything we want with the alpha channel.

from PIL import Image, ImageDraw

img = Image.new('RGBA', (100, 100), (0, 0, 0, 0))
drw = ImageDraw.Draw(img, 'RGBA')
drw.polygon(xy=[(50, 0), (100, 100), (0, 100)], fill=(255, 0, 0, 125))
drw.polygon(xy=[(50, 100), (100, 0), (0, 0)], fill=(0, 255, 0, 125))

img.save('out.png', 'PNG')
T.X. Xie
  • 1
  • 1