diff --git a/src/videotoolkit/__init__.py b/src/videotoolkit/__init__.py index 18f58ab..920410f 100644 --- a/src/videotoolkit/__init__.py +++ b/src/videotoolkit/__init__.py @@ -6,11 +6,54 @@ from .tools import create_start_slide, create_end_slide from .utils import add_logo, generate_hex_grid, pdf_to_png -__all__ = ['create_start_slide', 'create_end_slide', 'add_logo', 'generate_hex_grid', 'pdf_to_png'] import logging +import subprocess logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) + +logger = logging.getLogger(__name__) + + +def detect_imagemagick_command(): + """ + Determine whether the appropriate ImageMagick command is `magick` (v7.x) or `convert` (v6.x). + :return: either `magick` or `convert` or, if not detected, `None` + """ + + magick_command = None + + try: + result = subprocess.run(['magick', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + if result.returncode == 0 and 'ImageMagick' in result.stdout: + magick_command = 'magick' + except (Exception,): + pass + + if magick_command: + return magick_command + + try: + result = subprocess.run(['convert', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + if result.returncode == 0 and 'ImageMagick' in result.stdout: + magick_command = 'convert' + except (Exception,): + pass + + if magick_command: + return magick_command + + logger.warning(f'Could not detect ImageMagick either as `magick` or `convert`. Some functionality will not work.') + return None + + +MAGICK_COMMAND = detect_imagemagick_command() +logger.info(f'ImageMagick command detected as {MAGICK_COMMAND}') + + +__all__ = ['create_start_slide', 'create_end_slide', 'add_logo', 'generate_hex_grid', 'pdf_to_png', MAGICK_COMMAND] diff --git a/src/videotoolkit/utils.py b/src/videotoolkit/utils.py index 50c0105..598cc03 100644 --- a/src/videotoolkit/utils.py +++ b/src/videotoolkit/utils.py @@ -12,6 +12,8 @@ import sys import subprocess +import videotoolkit + logger = logging.getLogger(__name__) @@ -53,6 +55,10 @@ def draw_hexagon(c: canvas.Canvas, x: float, y: float, size: float): def pdf_to_png(pdf_path: str | Path) -> None: + if not videotoolkit.MAGICK_COMMAND: + logger.error(f'ImageMagick not found; required for {__name__}') + sys.exit(1) + if isinstance(pdf_path, str): pdf_path = Path(pdf_path) @@ -70,7 +76,7 @@ def pdf_to_png(pdf_path: str | Path) -> None: try: # Construct the command to call ImageMagick's convert command = [ - 'magick', + videotoolkit.MAGICK_COMMAND, '-density', '72', str(pdf_path), str(png_path) diff --git a/tests/test_tools.py b/tests/test_tools.py index e335a2c..f9a5816 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -27,3 +27,7 @@ def test_create_start_slide(): def test_create_end_slide(): """Placeholder test for create_end_slide""" assert True # Replace with real test later + + +def test_magick_command(): + assert videotoolkit.MAGICK_COMMAND == 'magick' or videotoolkit.MAGICK_COMMAND == 'convert'