如何从 python 在后台运行 imagemagick

2024-01-05

如何在不打开新的命令行窗口并失去焦点的情况下使用 python 中的 imagemagick?

这个循环显示了问题;处理图像时系统变得无法使用,因为每次系统调用都会失去焦点:

for i in range(0,100,1):
    image = 'convert -background '+'black'+' -fill '+'white'+' -font '+'arial'+' -size '+'50'+'x50'+' -encoding utf8'+' -gravity center caption'+':'+'"just stole your focus"'+' '+'C:/'+'testFile.png'
    os.system(image)

“start /min”或“/b”只能快速最小化窗口,因此您仍然会失去焦点。由于某种原因,如果我将它们放在“图像”之前,我不会得到输出文件。

有什么方法可以使用os.system, os.spawnv, subprocess.Popen或者另一个在后台调用 imagemagick 的系统命令?

我读到了有关 PythonMagickWand 的内容,但只找到了 nix 的安装说明:ImageMagick 的 MagickWand API 的 Python 绑定 http://www.assembla.com/wiki/show/pythonmagickwand

我可以在 Windows 下安装/编译这些绑定吗?如果是这样,怎么办?

编辑:MRAB的解决方案:

import os
import subprocess

# Start all the processes.
processes = []
# Define directories
convertDir = 'C:/Program Files/ImageMagick-6.7.5-Q16/convert.exe'
outputDir = 'C:/test/'
outputFileName = 'testFile_look_ma_no_windows_'
if not os.path.exists(outputDir):
    os.makedirs(outputDir)

for i in range(100):
    outputDirFile = outputDir+outputFileName+str(i)+'.png'
    image = convertDir+' '+'-background'+' '+'blue'+' '+'-fill'+' '+'white'+' '+'-font'+' '+'arial,'+' '+'-size '+' '+'50x50'+' '+'-encoding'+' '+'utf8'+' '+'-gravity'+' '+'center'+' '+'caption:"did not steal your focus"'+' '+outputDirFile
    #CREATE_NO_WINDOW Flag: 0x08000000
    p = subprocess.Popen(image, creationflags=0x08000000)
    processes.append(p)

# Wait for all the processes to finish.
# Some may finish faster, so the files are out of order before all are done;
# if you need the files immediately, put p.wait after p = subprocess.Popen
# and comment out lines 5,18,24 and 25
for p in processes:
    p.wait()

print 'Done, created ',str(i+1),' images in ',outputDir

subprocesses.Popen 是推荐的方法:

# Start all the processes.
processes = []
for i in range(100):
    p = subprocess.Popen(['convert', '-background', 'black', '-fill', 'white', '-font', 'arial', '-size', '50x50', '-encoding', 'utf8', '-gravity', 'center', 'caption:"just stole your focus"', 'C:/testFile.png'])
    processes.append(p)

# Wait for all the processes to finish.
for p in processes:
    p.wait()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 python 在后台运行 imagemagick 的相关文章

随机推荐