开发者

Draw lines with settings for line cap and line join

开发者 https://www.devze.com 2023-03-20 00:55 出处:网络
I need a library in Python which can draw lines with an option to choose line cap (e.g. round) and line j开发者_如何学Gooin (e.g. round).

I need a library in Python which can draw lines with an option to choose line cap (e.g. round) and line j开发者_如何学Gooin (e.g. round).

I am looking for something what works like HTML5 Canvas Drawing capabilities.

I looked at PIL, AggDraw and did not find the line cap and join options.

Thank you.

EDIT

I found Cairo library, which I decided to use. You can read more about Cairo here and about PyCairo here


With PyQt you can easily set those attributes and the application can be without a gui (e.g. just creating an image and saving for example to a file).

Just check the QPainter documentation about setting antialiasing (with setRenderHints) and the documentation about the QPen class to set cap and join style.

from PyQt4.Qt import *
import math

app = QApplication([])

img = QImage(256, 256, QImage.Format_RGB32)
dc = QPainter(img)
dc.fillRect(0, 0, 256, 256, QColor(192, 192, 192))
dc.setRenderHints(QPainter.Antialiasing)
dc.setPen(QPen(QColor(128, 128, 192),
               12.0,
               Qt.SolidLine,
               Qt.RoundCap,
               Qt.RoundJoin))
pts = []
for i in xrange(7):
    t = i * 2 * 3 * math.pi / 7
    pts.append(QPointF(128 + 100 * math.cos(t),
                       128 + 100 * math.sin(t)))
dc.drawPolyline(QPolygonF(pts))
dc = None
img.save("output.png")

Draw lines with settings for line cap and line join


Since you're asking about HTML5 canvas, I assume you're outputting this to a web page. In that case, would you consider SVG graphics rather than Canvas? (personally I'd prefer SVG over Canvas, especially for drawing lines)

If you're okay with SVG, then you might want to look at pySVG - it seems to meet your needs in terms of polygons and polylines.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消