Geuis
Geuis

Reputation: 42267

Using Imagemagick without making files?

I'm working in Python to create images from text. I've already been back and forth with PIL and frankly, its font and alignment options need a lot of work.

I can subprocess Imagemagick and it works great, except that it seems to always need to write a file to disk. I would like to subprocess the image creation and just get the data returned to Python, keeping everything in memory.

I've looked into a number of supposed Python wrappers for ImageMagick, but they're all hopelessly years out of date or not documented whatsoever. Even searching extensively on SO doesn't see to clearly point to a defacto way to use ImageMagic with Python. So I think going for subprocessing is the best way forward.

Upvotes: 4

Views: 1430

Answers (2)

Alex Jasmin
Alex Jasmin

Reputation: 39496

convert and the other ImageMagick commands can output image data to stdout if you specify format:- as the output file. You can capture that output in Python using the subprocess module.

For instance:

cmd = ["convert", "test.bmp", "jpg:-"]
output_stream = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout

Upvotes: 7

mikerobi
mikerobi

Reputation: 20878

It would be a lot more work than piping data to ImageMagick, but there are several Pango based solutions. I used pango and pygtk awhile back, and I am pretty sure you could develop a headless gtk or gdk application to render text to a pixbuf.

A simpler solution might be to use the python cairo bondings.

Pango works at a pretty low level, so simple stuff can be a lot more complicated, but rendering quality is hard to beat, and it gives you a lot of fine grained control over the layout.

Upvotes: 0

Related Questions