Skip to main content

Overview

This demo showcases how to use Trigger.dev with Python to process an image using Pillow (PIL) from a URL and upload it to S3-compatible storage bucket.

Prerequisites

Features

  • A Trigger.dev task to trigger the image processing Python script, and then upload the processed image to S3-compatible storage
  • The Trigger.dev Python build extension to install dependencies and run Python scripts
  • Pillow (PIL) for powerful image processing capabilities
  • AWS SDK v3 for S3 uploads
  • S3-compatible storage support (AWS S3, Cloudflare R2, etc.)

GitHub repo

View the project on GitHub

Click here to view the full code for this project in our examples repository on GitHub. You can fork it and use it as a starting point for your own project.

The code

Build configuration

After you’ve initialized your project with Trigger.dev, add these build settings to your trigger.config.ts file:
trigger.config.ts
Learn more about executing scripts in your Trigger.dev project using our Python build extension here.

Task code

This task uses the python.runScript method to run the image-processing.py script with the given image URL as an argument. You can adjust the image processing parameters in the payload, with options such as height, width, quality, output format, etc.
src/trigger/processImage.ts

Add a requirements.txt file

Add the following to your requirements.txt file. This is required in Python projects to install the dependencies.
requirements.txt

The Python script

The Python script uses Pillow (PIL) to process an image. You can see the original script in our examples repository here.
src/python/image-processing.py

Testing your task

  1. Create a virtual environment python -m venv venv
  2. Activate the virtual environment, depending on your OS: On Mac/Linux: source venv/bin/activate, on Windows: venv\Scripts\activate
  3. Install the Python dependencies pip install -r requirements.txt
  4. Set up your S3-compatible storage credentials in your environment variables, in .env for local development, or in the Trigger.dev dashboard for production:
  5. Copy the project ref from your Trigger.dev dashboard and add it to the trigger.config.ts file.
  6. Run the Trigger.dev CLI dev command (it may ask you to authorize the CLI if you haven’t already).
  7. Test the task in the dashboard by providing a valid image URL and processing options.
  8. Deploy the task to production using the Trigger.dev CLI deploy command.

Example Payload

These are all optional parameters that can be passed to the image-processing.py Python script from the processImage.ts task.

Deploying your task

Deploy the task to production using the CLI command npx trigger.dev@latest deploy

Learn more about using Python with Trigger.dev

Python build extension

Learn how to use our built-in Python build extension to install dependencies and run your Python code.