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

Skip to content

Commit bb7a182

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 7bcf618 commit bb7a182

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
@@ -704,7 +704,7 @@ class AVConvFileWriter(AVConvBase, FFMpegFileWriter):
704704
'''
705705

706706

707-
# Base class for animated GIFs with convert utility
707+
# Base class for animated GIFs with ImageMagick
708708
class ImageMagickBase(object):
709709
'''Mixin class for ImageMagick output.
710710
@@ -724,48 +724,33 @@ def output_args(self):
724724
return [self.outfile]
725725

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

784769

785-
# Note: the base classes need to be in that order to get
786-
# isAvailable() from ImageMagickBase called and not the
787-
# one from MovieWriter. The latter is then called by the
788-
# former.
770+
# Combine ImageMagick options with temp file-based writing
789771
@writers.register('imagemagick_file')
790772
class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter):
791773
'''File-based animated gif writer.

0 commit comments

Comments
 (0)