Main Content

Graphics in Lua - an example using the gd library

Archive - Originally posted on "The Horse's Mouth" - 2009-08-02 18:35:10 - Graham Ellis

In answer to the question "how would you generate graphics from Lua?" ... I would (personally) look at a binding to the gd library - http://luaforge.net/projects/lua-gd/ - as this is an excellent library, under the MIT license so there's no issue with ongoing licensing being different to Lua's.

This image is an example produced using the gd module within Lua. The principle is that you create an image using gd.create which returns a table on which you run various methods such as colorAllocate and fillRectangle ... before finishing with a method such as jpeg or png to output the Image.

The complete source code of the program I used to generate the image that illustrates this article is on our web site here. We will give you a brief introduction on our public Lua course; Computer Graphics (and the GD library) are something of a speciality of mine, so if you need to know more, please ask for an "extra day" or alert me ahead of time on a private course, and I'll be delighted to cover the topic!





For graphs (as opposed to graphics), look at http://luagraph.luaforge.net/ which is a binding to the Graphviz library. And there are also OpenGL, SDL and .pdf bindings ... see http://lua-users.org/wiki/LibrariesAndBindings