I need to use ImageMagick as PIL does not have the amount of image functionality available that I am looking for. However, I am wanting to use Python.
我需要使用ImageMagick,因为PIL没有我正在寻找的图像功能。但是,我想使用Python。
The python bindings (PythonMagick) have not been updated since 2009. The only thing I have been able to find is os.system
calls to use the command line interface but this seems clunky.
python绑定(PythonMagick)自2009年以来一直没有更新。我唯一能找到的是os.system调用使用命令行界面,但这看起来很笨重。
Is there any way to access the API directly using ctypes
and conversions of some sort? As a last resort is there any other library out there that has the extensive amount of image editing tools like ImageMagick that I have looked over?
有没有办法直接使用某种类型的ctypes和转换来访问API?作为最后的手段,还有其他任何图书馆都有大量的ImageMagick图像编辑工具,我已经查看了吗?
3 个解决方案
#1
59
I would recommend using Wand (explanations follows).
我建议使用Wand(解释如下)。
I was looking for proper binding to ImageMagick library, that would:
我正在寻找与ImageMagick库的正确绑定,这将:
- work error/problem free
- 工作错误/无问题
- be regularly maintained and up to date
- 定期维护和更新
- allow nice objective Python
- 允许很好的客观Python
But indeed python API (binding) has too many different (mostly discontinued) versions. After reading a nice historical overview by Benjamin Schweizer it has all become clear
(also see his
github wiki)
:
但实际上python API(绑定)有太多不同的(大多数已停产)版本。在阅读了Benjamin Schweizer的精彩历史概述后,一切都变得清晰了(另见他的github wiki):
- GraphicsMagick
- GraphicsMagick工具
- PythonMagick - first implementation
- PythonMagick - 第一次实现
- PythonMagickWand/Achim Domma - first Wand - a CDLL implementation
- PythonMagickWand / Achim Domma - 第一个Wand - 一个CDLL实现
- PythonMagickWand/Ian Stevens
- PythonMagickWand / Ian Stevens
- MagickFoo - included in python-magickwand
- MagickFoo - 包含在python-magickwand中
- Wand/Hong Minhee - the latest project
- Wand / Hong Minhee - 最新项目
Now Wand is just a (reduced) C API to the ImageMagick ".. API is the recommended interface between the C programming language and the ImageMagick image processing libraries. Unlike the MagickCore C API, MagickWand uses only a few opaque types. Accessors are available to set or get important wand properties." (See project homepage)
现在,Wand只是ImageMagick的一个(简化的)C API。.. API是C编程语言和ImageMagick图像处理库之间的推荐接口。与MagickCore C API不同,MagickWand仅使用几种不透明类型。可以使用访问器设置或获取重要的魔杖属性。“ (见项目主页)
So it is already a simplified interface that is easer to maintain.
因此,它已经是一个易于维护的简化界面。
#2
2
I found no good Python binding for ImageMagick, so in order to use ImageMagick in Python program I had to use subprocess
module to redirect input/output.
我发现ImageMagick没有很好的Python绑定,所以为了在Python程序中使用ImageMagick,我不得不使用子进程模块来重定向输入/输出。
For example, let's assume we need to convert PDF file into TIF:
例如,假设我们需要将PDF文件转换为TIF:
path = "/path/to/some.pdf"
cmd = ["convert", "-monochrome", "-compress", "lzw", path, "tif:-"]
fconvert = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = fconvert.communicate()
assert fconvert.returncode == 0, stderr
# now stdout is TIF image. let's load it with OpenCV
filebytes = numpy.asarray(bytearray(stdout), dtype=numpy.uint8)
image = cv2.imdecode(filebytes, cv2.IMREAD_GRAYSCALE)
Here I used tif:-
to tell ImageMagick's command-line utility that I want to get TIF image as stdout stream. In the similar way you may tell it to use stdin stream as input by specifying -
as input filename.
在这里我使用tif: - 告诉ImageMagick的命令行实用程序,我想将TIF图像作为stdout流。以类似的方式,您可以通过指定 - 作为输入文件名来告诉它使用stdin流作为输入。
#3
0
This has worked for me for the following command to create an image from text for the letter "P":
这对我来说有用,可以通过以下命令从文本中为字母“P”创建图像:
import subprocess
cmd = '/usr/local/bin/convert -size 30x40 xc:white -fill white -fill black -font Arial -pointsize 40 -gravity South -draw "text 0,0 \'P\'" /Users/fred/desktop/draw_text2.gif'
subprocess.call(cmd, shell=True)
#1
59
I would recommend using Wand (explanations follows).
我建议使用Wand(解释如下)。
I was looking for proper binding to ImageMagick library, that would:
我正在寻找与ImageMagick库的正确绑定,这将:
- work error/problem free
- 工作错误/无问题
- be regularly maintained and up to date
- 定期维护和更新
- allow nice objective Python
- 允许很好的客观Python
But indeed python API (binding) has too many different (mostly discontinued) versions. After reading a nice historical overview by Benjamin Schweizer it has all become clear
(also see his
github wiki)
:
但实际上python API(绑定)有太多不同的(大多数已停产)版本。在阅读了Benjamin Schweizer的精彩历史概述后,一切都变得清晰了(另见他的github wiki):
- GraphicsMagick
- GraphicsMagick工具
- PythonMagick - first implementation
- PythonMagick - 第一次实现
- PythonMagickWand/Achim Domma - first Wand - a CDLL implementation
- PythonMagickWand / Achim Domma - 第一个Wand - 一个CDLL实现
- PythonMagickWand/Ian Stevens
- PythonMagickWand / Ian Stevens
- MagickFoo - included in python-magickwand
- MagickFoo - 包含在python-magickwand中
- Wand/Hong Minhee - the latest project
- Wand / Hong Minhee - 最新项目
Now Wand is just a (reduced) C API to the ImageMagick ".. API is the recommended interface between the C programming language and the ImageMagick image processing libraries. Unlike the MagickCore C API, MagickWand uses only a few opaque types. Accessors are available to set or get important wand properties." (See project homepage)
现在,Wand只是ImageMagick的一个(简化的)C API。.. API是C编程语言和ImageMagick图像处理库之间的推荐接口。与MagickCore C API不同,MagickWand仅使用几种不透明类型。可以使用访问器设置或获取重要的魔杖属性。“ (见项目主页)
So it is already a simplified interface that is easer to maintain.
因此,它已经是一个易于维护的简化界面。
#2
2
I found no good Python binding for ImageMagick, so in order to use ImageMagick in Python program I had to use subprocess
module to redirect input/output.
我发现ImageMagick没有很好的Python绑定,所以为了在Python程序中使用ImageMagick,我不得不使用子进程模块来重定向输入/输出。
For example, let's assume we need to convert PDF file into TIF:
例如,假设我们需要将PDF文件转换为TIF:
path = "/path/to/some.pdf"
cmd = ["convert", "-monochrome", "-compress", "lzw", path, "tif:-"]
fconvert = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = fconvert.communicate()
assert fconvert.returncode == 0, stderr
# now stdout is TIF image. let's load it with OpenCV
filebytes = numpy.asarray(bytearray(stdout), dtype=numpy.uint8)
image = cv2.imdecode(filebytes, cv2.IMREAD_GRAYSCALE)
Here I used tif:-
to tell ImageMagick's command-line utility that I want to get TIF image as stdout stream. In the similar way you may tell it to use stdin stream as input by specifying -
as input filename.
在这里我使用tif: - 告诉ImageMagick的命令行实用程序,我想将TIF图像作为stdout流。以类似的方式,您可以通过指定 - 作为输入文件名来告诉它使用stdin流作为输入。
#3
0
This has worked for me for the following command to create an image from text for the letter "P":
这对我来说有用,可以通过以下命令从文本中为字母“P”创建图像:
import subprocess
cmd = '/usr/local/bin/convert -size 30x40 xc:white -fill white -fill black -font Arial -pointsize 40 -gravity South -draw "text 0,0 \'P\'" /Users/fred/desktop/draw_text2.gif'
subprocess.call(cmd, shell=True)