Skip to main content

CLI tool for converting images between different formats and creating GIFs from image sequences, directories, or video files. Supports JPG, PNG, BMP, and WebP.

Project description

transimage - image format conversion and GIF creation

Description

transimage is a Python package and CLI tool for converting images between different formats and creating GIFs from image sequences, directories, or video files. It uses Pillow for image operations and OpenCV for video frame extraction.

send your PR based god🙏🏻

Features

  • Convert images between JPG, PNG, BMP, and WebP formats
  • Batch conversion of multiple images or entire directories
  • Create GIFs from a list of images, a directory of images, or a video file
    • Configurable GIF parameters: FPS, size, crop, loop, and video trim
    • Preserves aspect ratio on resize and handles transparency
  • Simple command-line interface with subcommands
  • Skips conversion if the input and output formats are the same

Dependencies

  • Pillow >= 11.0.0
  • Poetry

Optional dependency for creating GIFs:

  • opencv-python-headless

Setup

Development Environment

  1. Clone the repository
  2. Install Poetry: pip install poetry or pipx install poetry
  3. Install all dependencies: poetry install
  4. (Optional) For video support: poetry install --extras opencv

Usage

All commands are run from the project root using Poetry’s virtual environment. Replace poetry run python -m transimage with transimage if you installed the package globally.

Image Conversion

Convert a single image or all images in a directory:

poetry run python -m transimage convert <input_path> <output_path> <output_format>
  • <input_path>: Path to a single image file or a directory of images
  • <output_path>: Directory where converted images will be saved (do not include file name)
  • <output_format>: Target format (jpg, png, bmp, webp)

Examples

poetry run python -m transimage convert ./photo.jpg ./output png
poetry run python -m transimage convert ./images/ ./converted webp

GIF Creation

Create a GIF from images or video:

poetry run python -m transimage gif <input> -o <output.gif> [options]
  • <input>: A directory path, a video file path, or a list of image file paths (list them after the command)
  • -o, --output: Output GIF file path (required)

Options

Option Type Default Description
--fps float 24 Frames per second
--size int int - Max width and height (aspect ratio preserved)
--crop int int int int - Crop box: left top right bottom
--loop int 0 Loop count (0 = infinite)
--start-time float - Start time in seconds (video only)
--end-time float - End time in seconds (video only)
--skip-frames int 1 Only use every Nth frame (video only)

Examples

From a directory of images (sorted by name):

poetry run python -m transimage gif ./frames -o movie.gif --fps 12 --size 640 480

From a list of explicit image files:

poetry run python -m transimage gif a.jpg b.jpg c.jpg -o combined.gif --fps 2 --loop 0

From a video file (requires the opencv extra):

poetry run python -m transimage gif video.mp4 -o clip.gif --fps 15 --start-time 2.5 --end-time 5 --size 320 240

To preserve real-time speed from a video, adjust --skip-frames so that fps / skip_frames roughly equals the original video’s frame rate. For a 30 fps video, --fps 18 --skip-frames 2 gives a natural-looking GIF.

Programmatic Usage

Inside a script or interactive session, you can use the package directly after installing it in your environment.

Import the needed functions:

from transimage import ImageConverter, convert_image, collect_images, GIFCreator, create_gif

Image Conversion

converter = ImageConverter('input.jpg', 'output.png', 'png')
converter.convert()

Batch conversion:

import os
from transimage import collect_images, convert_image

input_dir = 'images/'
output_dir = 'converted/'
fmt = 'png'
for img_path in collect_images(input_dir):
    name = os.path.splitext(os.path.basename(img_path))[0]
    out_path = os.path.join(output_dir, f"{name}.{fmt}")
    convert_image(img_path, out_path, fmt)

GIF Creation

Using the convenience function:

create_gif(
    input=['a.jpg', 'b.jpg', 'c.jpg'],   # or a directory path, or a video file path
    output='animation.gif',
    fps=10,
    size=(320, 240),
    loop=0
)

Or with the class for more control:

creator = GIFCreator(
    sources='video.mp4',
    output_path='clip.gif',
    fps=15,
    size=(480, 360),
    start_time=2.0,
    end_time=5.0,
    skip_frames=2
)
creator.create()

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Please submit issues regarding any oversight you see. Pull requests for improvements are welcome.

Set up

  1. Install the developer dependencies: poetry install (dev deps are included by default)
  2. Add your changes
  3. Test your code: poetry run pytest tests/
  4. Format and lint:
    • Format: poetry run black src/transimage tests
    • Lint: poetry run flake8 src/transimage tests
    • Run all together: poetry run black src/transimage tests && poetry run flake8 src/transimage tests && poetry run pytest tests
  5. Rise and repeat until finished.

Version

2.0.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transimage-2.0.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transimage-2.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file transimage-2.0.0.tar.gz.

File metadata

  • Download URL: transimage-2.0.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.10 Windows/11

File hashes

Hashes for transimage-2.0.0.tar.gz
Algorithm Hash digest
SHA256 ecb6f732e4b888e159a4f59e01a74d939fab09c135add42c689bfa060c0f6081
MD5 57540cc87493ddd99db863d73d64c017
BLAKE2b-256 032b456874c80d244cc918c38df21dd8afbe264a7dcb9fd1cca04d0f011639f3

See more details on using hashes here.

File details

Details for the file transimage-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: transimage-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.10 Windows/11

File hashes

Hashes for transimage-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1cd28987bbd6e88128818ab17a1f898086e54c706188a1e130e36e8017b434d3
MD5 7b1215ac9f29ad2733d1f88a1637ceff
BLAKE2b-256 6a50c985e48945a74cf7961cf17b3417b387ecfebf8c594ac588800de72ea257

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page