Skip to content

Commit

Permalink
Detect appropriate imagemagick convert command
Browse files Browse the repository at this point in the history
  • Loading branch information
fcooper8472 committed Aug 22, 2024
1 parent 83dca3e commit cb0a04f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
45 changes: 44 additions & 1 deletion src/videotoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
8 changes: 7 additions & 1 deletion src/videotoolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import sys
import subprocess

import videotoolkit

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit cb0a04f

Please sign in to comment.