Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59b546f

Browse files
committed
Move check for ImageMagick Windows path to bin_path().
That's a perfectly reasonable place to put the registry check and avoids introducing an additional private helper method.
1 parent 46ba49a commit 59b546f

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

lib/matplotlib/animation.py

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ class AVConvFileWriter(AVConvBase, FFMpegFileWriter):
700700
'''
701701

702702

703-
# Base class for animated GIFs with convert utility
703+
# Base class for animated GIFs with ImageMagick
704704
class ImageMagickBase(object):
705705
'''Mixin class for ImageMagick output.
706706
@@ -720,48 +720,33 @@ def output_args(self):
720720
return [self.outfile]
721721

722722
@classmethod
723-
def _init_from_registry(cls):
724-
if sys.platform != 'win32' or rcParams[cls.exec_key] != 'convert':
725-
return
726-
import winreg
727-
for flag in (0, winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY):
728-
try:
729-
hkey = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE,
730-
r'Software\Imagemagick\Current',
731-
0, winreg.KEY_QUERY_VALUE | flag)
732-
binpath = winreg.QueryValueEx(hkey, 'BinPath')[0]
733-
winreg.CloseKey(hkey)
734-
break
735-
except Exception:
736-
binpath = ''
737-
if binpath:
738-
for exe in ('convert.exe', 'magick.exe'):
739-
path = os.path.join(binpath, exe)
740-
if os.path.exists(path):
741-
binpath = path
742-
break
743-
else:
744-
binpath = ''
745-
rcParams[cls.exec_key] = rcParamsDefault[cls.exec_key] = binpath
746-
747-
@classmethod
748-
def isAvailable(cls):
749-
'''
750-
Check to see if a ImageMagickWriter is actually available.
751-
752-
Done by first checking the windows registry (if applicable) and then
753-
running the commandline tool.
754-
'''
755-
bin_path = cls.bin_path()
756-
if bin_path == "convert":
757-
cls._init_from_registry()
758-
return super().isAvailable()
759-
760-
761-
# Note: the base classes need to be in that order to get
762-
# isAvailable() from ImageMagickBase called and not the
763-
# one from MovieWriter. The latter is then called by the
764-
# former.
723+
def bin_path(cls):
724+
binpath = super().bin_path()
725+
if sys.platform == 'win32' and binpath == 'convert':
726+
# Check the registry to avoid confusing ImageMagick's convert with
727+
# Windows's builtin convert.exe.
728+
import winreg
729+
binpath = ''
730+
for flag in (0, winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY):
731+
try:
732+
with winreg.OpenKeyEx(
733+
winreg.HKEY_LOCAL_MACHINE,
734+
r'Software\Imagemagick\Current',
735+
0, winreg.KEY_QUERY_VALUE | flag) as hkey:
736+
parent = winreg.QueryValueEx(hkey, 'BinPath')[0]
737+
except OSError:
738+
pass
739+
if binpath:
740+
for exe in ('convert.exe', 'magick.exe'):
741+
candidate = os.path.join(parent, exe)
742+
if os.path.exists(candidate):
743+
binpath = candidate
744+
break
745+
rcParams[cls.exec_key] = rcParamsDefault[cls.exec_key] = binpath
746+
return binpath
747+
748+
749+
# Combine ImageMagick options with pipe-based writing
765750
@writers.register('imagemagick')
766751
class ImageMagickWriter(ImageMagickBase, MovieWriter):
767752
'''Pipe-based animated gif.
@@ -778,10 +763,7 @@ def _args(self):
778763
+ self.output_args)
779764

780765

781-
# Note: the base classes need to be in that order to get
782-
# isAvailable() from ImageMagickBase called and not the
783-
# one from MovieWriter. The latter is then called by the
784-
# former.
766+
# Combine ImageMagick options with temp file-based writing
785767
@writers.register('imagemagick_file')
786768
class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter):
787769
'''File-based animated gif writer.

0 commit comments

Comments
 (0)