ImageMagick files

This example uses the ImageMagick tool to create an animated GIF from a sequence of image files. In addition to the ImageMagick tool, this example also requires a RabbitMQ message broker. The command shown below will install and run RabbitMQ using Docker:

# Install and run RabbitMQ 3.13
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.13-management

The Python script for this example is shown below.

"""
Example of creating a campaign with a shell activity to create an animated
GIF. The GIF is created from a series of images using the ImageMagick tool.
"""

import logging
from pathlib import Path
from zambeze import Campaign, ShellActivity


def main():
    """
    Run a shell activity campaign to generate an animated GIF.
    """

    # Setup and configure logger
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    fmt = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    ch.setFormatter(fmt)

    logger.addHandler(ch)

    # Create the campaign
    campaign = Campaign("My ImageMagick Campaign", logger=logger)
    curr_dir = Path.cwd()

    activity = ShellActivity(
        name="ImageMagick",
        files=[
            f"local://{curr_dir}/../tests/campaigns/imagesequence/{i:02d}.jpg"
            for i in range(1, 11)
        ],
        command="convert",
        arguments=[
            "-delay",
            "20",
            "-loop",
            "0",
            f"{curr_dir}/../tests/campaigns/imagesequence/*.jpg",
            "a.gif",
        ],
        logger=logger,
    )

    campaign.add_activity(activity)

    # Run the campaign to execute the shell activity
    campaign.dispatch()


if __name__ == "__main__":
    main()

While RabbitMQ is running in a separate terminal session, use the commands shown here to start the zambeze agent then run the example script. This will create an animated GIF named a.gif in your home directory.

# Start the zambeze agent
zambeze start

# Run the example script
python imagemagick_files.py

# Stop the zambeze agent
zambeze stop

Below is a demo of running this example in the provided conda environment. Note that RabbitMQ is running on the computer where this demo was recorded.