【问题标题】:Montage using PythonMagick in Python 3?在 Python 3 中使用 PythonMagick 蒙太奇?
【发布时间】:2012-03-10 20:12:43
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: windows python-3.x imagemagick pythonmagick


    【解决方案1】:

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

    这就是我所做的(在 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 中做这一切。

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

    【讨论】:

      【解决方案2】:

      使用 python imagemagick/graphicsmagick 绑定有很大帮助,但不幸的是,还没有所有功能。我实际上对@FizxMike 有同样的问题。我需要使用 montage 然后做一些进一步的操作,但是将文件保存在硬盘上,然后将其重新加载到适当的 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')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        相关资源
        最近更新 更多