I'd like to call the "convert" utility from ImageMagick from my Python script using Popen, like so:
Popen(["convert", input_path, "-flop", output_file_path])
(The above example simply reverses the image horizontally)
The problem is that when I run the script in Windows, it mistakenly calls the convert.exe
utility that ships with Windows to convert FAT partitions to NTFS! (located in \Windows\system32)
Now, if I randomly open a command prompt at any directory other than system32, and type "convert", it correctly runs the ImageMagick executable. So, this implies that Popen is automatically looking in system32. How can I make it not look in system32, and run the correct executable?
Search for a program is not trivial. I'd specify the full path to the convert.exe executable explicitly instead.
subprocess
uses CreateProcess
on Windows that looks in system32
directory even before any other directory in %PATH%
:
... If the file name does not contain an extension,
.exe
is appended. Therefore, if the file name extension is .com, this parameter must include the .com extension. If the file name ends in a period (.) with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file in the following sequence:
Therefore convert
is equivalent to convert.exe
in this case. It first looks in a directory that contains sys.executable
e.g., C:\Python27
. Then in the current directory: where you started the Python script from. Then in system32
where it finds convert.exe
(filesystem utility, not imagemagick).
You could try to remove system32 directory from os.environ['PATH']
it may(?) suppress checking it: Popen(cmd, env=no_system32_environ)
but it is fragile (worse than the explicit path).
There is a related issue on Python bug tracker: "Subprocess picks the wrong executable on Windows."
cmd.exe
(the shell) uses different algorithm. See How does Windows locate files input in the shell?
If you set shell=True
then the search sequence for convert
program:
convert
is not an internal shell command%PATHEXT%
defines which file extensions are checked and in what order e.g., convert.com, convert.exe, convert.bat, convert.cmd if %PATHEXT%
is .com;.exe;.bat;.cmd
.
As a completely different approach, you may want to try out PythonMagick
, a Python wrapper for ImageMagick. This way you can access convert
's functions from within Python, and you won't have to spawn outside processes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With