如何从后台运行imagemagick从python [英] How to run imagemagick in the background from python

查看:418
本文介绍了如何从后台运行imagemagick从python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



此循环显示问题;在处理图像时系统变得不可用,因为每次系统调用都失去焦点:

 
image ='convert -background'+'black'+'-fill'+'white'+'-font'+'arial'+'-size'+ '50'+'x50'编码utf8'+'重力中心标题'+':'+'只是偷了你的焦点'+''+'C:/'+'testFile.png'
os.system $ b

'start / min'或'/ b'只是快速最小化窗口,所以你仍然失去焦点。并且由于某种原因,如果我把这些放在'image'之前,我不会得到一个输出文件。



有一些方法使用 os。系统 os.spawnv subprocess.Popen 或另一个系统命令调用imagemagick背景?



我阅读了关于PythonMagickWand,但只找到nix的安装路线: ImageMagick的MagickWand API的Python绑定



我可以在windows下安装/编译这些绑定吗?

 编辑:MRAB的解决方案

import os
import subprocess

#启动所有进程。
processes = []
#定义目录
convertDir ='C:/ Program Files / ImageMagick-6.7.5-Q16 / convert.exe'
outputDir ='C: test /'
outputFileName ='testFile_look_ma_no_windows_'
如果不是os.path.exists(outputDir):
os.makedirs(outputDir)

for i in range 100):
outputDirFile = outputDir + outputFileName + str(i)+'。png'
image = convertDir +''+' - background'+''+'blue'+''+' '+''+'white'+'''''font'+''''''''''''''''''' +'utf8'+''+' - 重力'+''+'中心'+''+'caption:没有偷走你的焦点'+''+ outputDirFile
#CREATE_NO_WINDOW标志:0x08000000
p = subprocess.Popen(image,creationflags = 0x08000000)
processes.append(p)

#等待所有进程完成。
#有些可能会更快完成,所以文件是无序的,在所有完成之前;
#如果你立即需要文件,在p = subprocess.Popen
#之后放置p.wait,并在进程中注释掉行5,18,24和25
用于p:
p.wait()

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


解决方案

subprocesses.Popen是推荐的方法:

 #启动所有进程。 
processes = []
for i in range(100):
p = subprocess.Popen(['convert','-background','black','-fill','white ','-font','arial','-size','50x50','-encoding','utf8','-gravity','center','caption: C:/testFile.png'])
processes.append(p)

#等待所有进程完成。
for p in processes:
p.wait()


How can you use imagemagick from python without opening a new command line window and losing focus?

This loop show the problem; the system becomes unusable while working on images because you lose focus with every system call:

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' or '/b' only minimize the window quickly, so you still lose focus. And for some reason I don't get an output file if I put these before 'image'.

Is there some way to use os.system, os.spawnv, subprocess.Popen or another system command to call imagemagick in the background?

I read about PythonMagickWand but only found install directions for nix: Python bindings for ImageMagick's MagickWand API

Can I install/compile these bindings under windows? If so, how?

Edit: MRAB's solution:

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 is the recommended way to do it:

# 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()

这篇关于如何从后台运行imagemagick从python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆