Simple user-oriented graphics drawing and image manipulation.
PyAgg is a precompiled Python library for lightweight, easy, and convenient graphics rendering based on the aggdraw module.
There are several ways to create high quality 2D drawings and graphics in Python. The widely used Matplotlib library is a favorite for advanced scientific visualizations, but for simpler drawing uses, its size, dependencies, and steep learning curve can be a bit overkill. PyCairo is another library, but unfortunately it suffers some from slow drawing and a stateful API that leads to longer code. Finally, Aggdraw is simple and intuitive, but has slightly limited functionality, is no longer maintained, and precompiled binaries are hard to come by.
The current library, PyAgg, aims to solve some of these problems. By building on the lightweight aggdraw module and including the necessary pre-compiled binaries for multiple Python and architecture versions in the same package, PyAgg is ready to use out of the box, no installation or compiling required. It is very fast and produces high quality antialiased drawings. Most importantly, PyAgg wraps around and offers several convenience functions for easier no-brain drawing and data visualization, including flexible handling of coordinate systems and length units.
Here is to at least another 5 years of beautiful and lightweight AGG image drawing. Cheers! After that, Python 2x will no longer be maintained, and while people can still continue to use it, it is likely that most people will be using Python 3x. At that point someone will have to update the C++ Aggdraw wrapper so it can be compiled for Python 3x if Python users are to continue to enjoy the power of the Agg graphics library.
The main features of PyAgg include:
- A coordinate aware image. No need for the intricacies of affine matrix transformations, just define the coordinates that make up each corner of your image once, and watch it follow as you resize, crop, rotate, move, and paste the image. Zoom in and out of you coordinate system by boundox box, factor, or units, and lock the aspect ratio to avoid distortions.
- Oneliners to easily draw and style high quality graphical elements including polygons with holes, lines with or without smooth curves, pie slices, and point symbols like circles, squarea, and triangles with an optional flattening factor.
- Style your drawing sizes and distances in several types of units, including pixels, percentagea, cm, mm, inches, and font points, by specifying the real world size of your image.
- Smart support for text writing, including text anchoring, and automatic detection of available fonts.
- Instantly view your image in a Tkinter pop up window, or prepare it for use in a Tkinter application.
There is also support for common domain specific data visualization:
- Partial support for geographical plotting, including a lat-long coordinate system, and automatic drawing of GeoJSON features.
- Easily plot statistical data on graphs witn a syntax and functionality that is aimed more for data analysts and laymen than computer scientists, including linegraph, scatterplot, histogram, etc, although these are still a work in progress.
Python 2.6, 2.7.
PIL/Pillow (used for image loading, saving, and manipulation)
PyAgg is installed with pip from the commandline:
pip install pyagg
It also works to just place the “pyagg” package folder in an importable location like “site-packages”.
Begin by importing the pyagg module:
To begin drawing, create your canvas instance and define its coordinate system, in this case based on percentages to easily draw using relative positions:
canvas = pyagg.Canvas("210mm","297mm", background=(222,222,222), ppi=96) canvas.percent_space()
Next, draw some graphical elements:
canvas.draw_line([10,10, 50,90, 90,10], smooth=True, fillcolor=(222,0,0), fillsize="2cm") canvas.draw_triangle((50,50),fillsize=30, fillcolor=(0,0,0, 111))
And some text:
canvas.draw_text((50,50),"PyAgg is for drawing!", textanchor="center", textfont="Segoe UI", textsize=42)
Once you are done, view or save your image:
More examples and tutorial to follow…
This code is free to share, use, reuse, and modify according to the MIT license, see license.txt
Karim Bahgat (2015)