3

I was hoping to be able to generate montages using PythonMagick. The documentation seems very sparse, but I've been trying to hunt it down using the code completion part of Eclipse at least, as well as a few other questions' suggestions here on Stack Overflow. It seems that the MagickWand API has the function I am looking for, according to this:

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

However, I cannot seem to find it in PythonMagick. Is this simply unavailable? If so I might just ditch the rest of my PythonMagick code and rely on subprocess.call on a portable ImageMagick distribution or something like that (this program will have to be portable, and run on Windows with an easy port to Mac OS... so far I have a few other PythonMagick commands working so I'd like to keep this route going if possible).

Thanks!

4

2 に答える 2

1

python imagemagick/graphicsmagick バインディングを使用すると非常に役立ちますが、残念ながらまだすべての機能が利用できるわけではありません。私は実際に@FizxMikeで同じ問題を抱えていました。モンタージュを使用してからさらにいくつかの操作を行う必要がありましたが、ファイルをハードディスクに保存し、適切な pgmagick オブジェクトに再ロードして残りの操作を実行し、再度保存するのは遅かったです。

最終的にはサブプロセス ソリューションを使用しましたが、ファイルに保存する代わりに、出力を stdout にリダイレクトしました。次に、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

私は同じ問題を抱えてます

これは私がすることです(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 に答える