[Image-SIG] Re: Image Draw, line width

Fredrik Lundh fredrik@pythonware.com
Mon, 5 May 2003 16:17:39 +0200


"VAN DER HEIJDEN Maarten" wrote:

> I would like to set the line width when using the
> method ImageDraw.Line()
>
> How is this possible ?

you cannot; the draw.line primitive only supports single-pixel
widths.

here's a stupid little hack that add a "width" option to the line
primitive, but only supports widths 0 (thin), 1 and 2.

    # file: ImageDraw2.py

    import ImageDraw, ImagePath

    class Draw(ImageDraw.Draw):

        def line(self, xy, fill=None, width=0):
            ink, fill = self._getink(fill)
            if ink is None:
                return
            if width <= 1:
                self.draw.draw_lines(xy, ink)
            else:
                # hackelihack!
                xy = ImagePath.Path(xy)
                self.draw.draw_lines(xy, ink)
                xy.transform((1, 0, -1, 0, 1, 0))
                self.draw.draw_lines(xy, ink)
                xy.transform((1, 0, 0, 0, 1, -1))
                self.draw.draw_lines(xy, ink)
                xy.transform((1, 0, 1, 0, 1, 0))
                self.draw.draw_lines(xy, ink)

usage:

    import Image, ImageDraw2, random

    im = Image.new("L", (600, 600), "white")
    d = ImageDraw2.Draw(im)

    xy0 = None
    for i in range(20):
        xy1 = random.randrange(0, 600), random.randrange(0, 600)
        if xy0: d.line(xy0 + xy1, fill="black", width=2)
        xy0 = xy1

    im.show()

to draw arbitrarily wide lines, the correct approach would be to
convert the line to a polygon, and use the polygon primitive to
render it.  contributions are welcome.

</F>