Automatic image editing with Python

This is an automatic translation generated by artificial intelligence. May contain errors.

As part of our work at OfiLibre, we generate many custom images for activities, videos, and outreach materials. To avoid relying on heavy or proprietary graphic-design tools, we have developed a set of Python scripts that let us automate image creation with a consistent, professional style.

These tools are based on the Pillow library, also known as PIL (Python Imaging Library), which makes it easy to manipulate images from code.

What can we do with these scripts?

  • Create images from graphic templates.
  • Insert text in different positions, with different sizes, colors, and fonts.
  • Adapt the layout according to the number of people or the type of content.
  • Use free fonts automatically downloaded from Google Fonts.
  • Export the images in PNG format, ready for use on the web or on social media.

General tool: dynamic image generator

We created a base tool that generates any type of image from a template and a YAML configuration file. This tool allows us to declaratively define all the texts that will appear (for example: title, subtitle, footer), their position, font, color, and alignment.

All you need is a base image (for example, a background template) and a config.yml file where we define which texts should appear and how.

This has proven especially useful for quickly generating banners, announcements, or cards without having to open a design program.

For more details, visit this GitLab repository, in the folder automatizacion/edicion-imagenes/.

However, because filling out the config.yml file can be complex—especially regarding the position (coordinates) of the elements—we designed scripts tailored to two frequent use cases in the office: generating thumbnails for cafes and generating thumbnails for materials belonging to Open Subjects. In these tailored scripts, the coordinates, colors, sizes, and fonts of the elements are preset, so you only specify their content.

Case 1: thumbnails for Cafes con OfiLibre

The Café con OfiLibre video series needed thumbnails that were consistent across episodes but flexible enough to accommodate different combinations of presenters and guests.

We developed a script that reads an image template and a configuration file with the title, date, and names, and automatically generates the thumbnail.

Depending on the number of people, the script chooses among several predefined templates. If there are 3 presenters and no guests, it uses a different template than if there are 2 presenters and 1 guest.

This allows us to maintain a uniform graphic line with minimal effort, and it makes image generation easier when there are many consecutive episodes.

For more details, visit this GitLab repository, in the folder automatizacion/miniaturas-cafes/.

Case 2: thumbnails for Open Subjects

Another use we gave it was generating covers for the videos of our Open Subjects, a project that disseminates free teaching materials.

Each cover includes:

  • The video title (for example, “Topic 3: Introduction to Digital Marketing”).
  • The subject name.
  • The degree it belongs to.
  • A license section, mentioning Creative Commons and authorship.

All of this is configured in a config.yml file, and the script automatically generates a high-quality image with all the information properly positioned and legible typography. In addition, it adapts to any template size while keeping the content centered.

For more details, visit this GitLab repository, in the folder automatizacion/miniaturas-asignaturas-abierto/.

Advantages of this approach

  • Time savings: No need to open any editing program—just adjust the texts and run a command.
  • Visual consistency: All images maintain a unified design.
  • Use of free fonts: We use Google Fonts, dynamically downloaded, avoiding reliance on proprietary fonts or manual installation.
  • Adaptability: The system can be easily modified for new campaigns or formats.

How to get started

  1. Make sure you have Python 3 installed.
  2. Clone the repository or download the scripts.
  3. Install the dependencies:
pip install -r requirements.txt
  1. Edit the config.yml file with the values you want to use.
  2. Run the corresponding script:
python <script_name>.py

Possible improvements

  • Support for automatically resizable images.
  • Inclusion of logos or additional images (for example, avatars).
  • Generation of multiple versions for different platforms (web, social media, print).

Using free tools like Python and PIL not only saves us time, but also gives us greater control over our materials, lets us adapt them freely, and allows us to share them under open licenses.