开发者

Recommendations for a simple 2D graphics python library that can output to screen and pdf?

开发者 https://www.devze.com 2022-12-28 04:28 出处:网络
I\'m looking for an easy-to-use graphics lib for python that can output to screen as well as pdf. So, I would use code to draw some stuff (simple prims like ovals, rectangles, lines and points) to scr

I'm looking for an easy-to-use graphics lib for python that can output to screen as well as pdf. So, I would use code to draw some stuff (simple prims like ovals, rectangles, lines and points) to screen and then when things look good, have it 开发者_如何学运维output to pdf.


If you use Tkinter, you can draw on a Canvas widget, then use its .postscript method to save the contents as a PostScript file, which you can convert to PDF using ps2pdf.

postscript(self, cnf={}, **kw) Print the contents of the canvas to a postscript file. Valid options: colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagewidth, pagex, pagey, rotate, witdh, x, y.


Matplotlib should be able to do it. See event handling here: http://matplotlib.sourceforge.net/examples/event_handling/index.html


You can use the Python Imaging Library for drawing images which can easily be displayed in various UIs, e.g. by displaying a jpg. Then, use ReportLab. Here's an example which shows how to use ReportLab with an image.

I'm not sure what you mean by drawing to "screen", i.e. if you're working with a specific UI toolkit. But if it's acceptable to draw and display PDFs without using an intermediate image (jpg, etc), then you might consider the PyX library, which makes it quite simple to do graphics with PDFs.


You could look into matplotlib, which is mainly for plotting but you could probably do some basic drawing.

Then there is pygame. But I'm not so sure if it can generate a pdf, however you can do 2D graphics with it.

There is something called ReportLab that can generate pdf's. Here is a bunch of tutorials using it.


This is a tricky question, because there are so many libraries available - there is a trade-off between beauty/easiness.

What I've done and works great is to produce the Postscript directly, it is not difficult at all, and you can preview it using Ghostview; converting tyo PDF is trivial (ps2pdf). Learning how to tell Postscript to create lines and circles is extremely simple.

If you want more extensibility, then go to Matplotlib, but beware of the many times when it will "decide for you what looks best" even if you don't like it.

Good luck.


Creating PDFs is always a pain, it doesn't make sense if you do not aim to lose sanity.

With that said, you are aiming to do two completely different things: when you draw to screen you draw into a raster bitmap, while PDFs are mostly dynamic, like HTML. (unlike HTML they are more prone to be the same over different platforms, but that's beside the point)

If you really want to do that, the solution might be finding something that outputs PDFs, and then showing the generated PDF on screen at every step. I guess that's the only way to have WYSIWYG results.

0

精彩评论

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