微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从python在后台运行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’只会快速缩小窗口,因此您仍然会失去焦点.出于某种原因,如果我将这些文件放在“ image”之前,则不会得到输出文件.

有什么方法可以使用os.system,os.spawnv,subprocess.Popen或其他系统命令在后台调用imagemagick?

我读到有关pythonmagickWand的文章,但仅找到nix的安装说明:Python bindings for ImageMagick’s MagickWand API

我可以在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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐