3

我希望能够使用 PythonMagick 生成蒙太奇。文档似乎很稀疏,但我一直在尝试至少使用 Eclipse 的代码完成部分以及 Stack Overflow 上的一些其他问题的建议来寻找它。据此,MagickWand API 似乎具有我正在寻找的功能:

http://www.imagemagick.org/api/MagickWand/montage_8c.html

但是,我似乎无法在 PythonMagick 中找到它。这根本不可用吗?如果是这样,我可能会放弃我的 PythonMagick 代码的其余部分并依赖可移植 ImageMagick 发行版或类似的东西上的 subprocess.call(该程序必须是可移植的,并且可以在 Windows 上运行,并且可以轻松移植到 Mac OS...到目前为止,我还有一些其他 PythonMagick 命令正在工作,所以如果可能的话,我想继续这条路线)。

谢谢!

4

2 回答 2

1

使用 python imagemagick/graphicsmagick 绑定有很大帮助,但不幸的是,并不是所有的功能都在那里。我实际上对@FizxMike 有同样的问题。我需要使用蒙太奇,然后做一些进一步的操作,但是将文件保存在硬盘上,然后将其重新加载到适当的 pgmagick 对象中,以便完成其余操作并再次保存它很慢。

最终我使用了 subprocess 解决方案,但我没有保存在文件中,而是将输出重定向到 stdout。然后,我使用标准输出从 pgmagick.Image 对象中的 pgmagick.Blob 加载图像,并在 python 代码中完成其余的处理。

该过程在代码中如下所示:

import os
import pgmagick
import subprocess

my_files = []
# Dir with the images that you want to operate on
dir_with_images = "."
for file in os.listdir(dir_with_images):
    if file.endswith(".png"):
        my_files.append(os.path.join(dir_with_images, file))

montage_cmd = ['gm', 'montage']
montage_cmd.extend(my_files)
# The trick is in the next line of code. Instead of saving in a file, e.g. myimage.png
# the montaged file will just be "printed" in the stdout with 'png:-'
montage_cmd.extend(['-tile', '2x2', '-background', 'none', '-geometry', '+0+0', 'png:-'])

# Use the command line 'gm montage' since there are not python bindings for it :(
p = subprocess.Popen(montage_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Get the stdout in a variable
stdout, stderr = p.communicate()

# Load the stdout in a python pgmagick Image object using the pgmagick.Blob
# and do the rest of the editing on python code
img = pgmagick.Image(pgmagick.Blob(stdout))
# Display the image
img.display()
geometry = pgmagick.Geometry(300, 200)
geometry.aspect(True)
# Resize the montaged image to 300x200, but keep the aspect ratio
img.scale(geometry)
# Display it again
img.display()
# And finally save it <- Only once disk access at this point.
img.write('myimage.png')
于 2015-04-25T18:12:44.277 回答
0

我有同样的问题,即使pgmagick缺少所需的 montageImage() 函数(Magick++ montage 示例

这就是我所做的(在 Django 视图中):

#ImageMagick CLI is better documented anyway (-background none preserves transparency)
subprocess.call("montage -border 0 -geometry "+str(cols)+"x -tile 1x"+str(len(pages))+" "+target_path[0:len(target_path)-4]+"[0-9]*.png -background none "+target_path,shell=True)`

不好玩,因为我必须先处理一堆文件......写入硬盘不是最快的事情,然后删除临时文件。

我宁愿在 ram 中完成这一切。

我自己仍在寻找更好的答案。

于 2012-07-17T02:41:36.960 回答