你怎么能使用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)
“开始/分钟”或“/ b”只能将窗口快速缩小,所以仍然失去焦点。 由于某种原因,如果我把这些放在'图片'之前,我不会得到一个输出文件。
有没有办法使用os.system
, os.system
, os.spawnv
或其他系统命令在后台调用imagemagick?
我读了关于PythonMagickWand,但只发现nix的安装指导: ImageMagick的MagickWand API的Python绑定
我可以在Windows下安装/编译这些绑定吗? 如果是这样,怎么样?
编辑:MRAB的解决scheme:
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
打开子进程.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()