python - 如何从 python 在后台运行 imagemagick

标签 python windows command-line focus imagemagick

如何在不打开新命令行窗口和失去焦点的情况下从 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 的安装说明:Python bindings for ImageMagick's MagickWand API

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

编辑: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()

关于python - 如何从 python 在后台运行 imagemagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9243296/

相关文章:

python-3.x - 无法将 PostgreSQL 与 Python3.6 连接,但与 Python2.7 的代码相同。问题意外

windows - GetRawInputDeviceInfo 中的 RIDI_PREPARSEDDATA 是什么?

command-line - 如何忽略要在wget中下载的文件的特定类型?

javascript - 我可以在Python中通过tornado websocket发送gzip压缩数据吗?

python - 有没有办法执行创建关系型 Pandas 数据框?

python - 从数组中随机选择正负数据

c++ - __RTC_CheckEsp 是如何实现的?

c# - C# 中的命令行 CD(更改目录)变体?

awk - linux csv文件将列连接成一列

python - spyder无法导入opencv