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