开发者

Using Imagemagick without making files?

开发者 https://www.devze.com 2023-01-23 02:15 出处:网络
I\'m working in Python to开发者_如何学Python 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'm working in Python to开发者_如何学Python 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.


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


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.

0

精彩评论

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

关注公众号