A Python tool to trim empty borders from images, leave a margin, and convert them into fixed aspect ratios (1.5 and 3.75) without distortion.
It works well for images with transparent backgrounds (e.g., logos, icons), but also supports opaque images with flat-color or white backgrounds.
- Content-aware trimming:
- Uses transparency (alpha channel) if present.
- Falls back to detecting flat background color from the corners.
- Margin control:
- 1.5 ratio output keeps ~5% margin by default.
- 3.75 ratio output keeps ~1% margin by default.
- Both are configurable with separate flags.
- Aspect ratio correction:
- Always pads (never crops or distorts) to reach the exact target ratio.
- Ensures all content is preserved.
- Batch mode: Process a single file or an entire folder of images at once.
- Output naming:
- Saves files as
originalname_150ratio.png(for 1.5 ratio). - Saves files as
originalname_375ratio.png(for 3.75 ratio).
- Saves files as
- Format support:
- PNG, JPEG/JPG, and SVG (requires CairoSVG).
- Output is always PNG (to preserve alpha transparency).
-
Clone the repo:
git clone https://github.com/yourname/logo_converter.git cd logo_converter -
(Optional but recommended) Create a virtual environment:
python3 -m venv .venv source .venv/bin/activate -
Install dependencies:
pip install pillow cairosvg
- Pillow: Image processing library
- CairoSVG: Required for handling SVG input
python logo_converter.py logo.pngThis will generate two files in the same folder:
logo_150ratio.png(1.5 aspect ratio, ~5% margin)logo_375ratio.png(3.75 aspect ratio, ~1% margin)
python logo_converter.py ./logos/All supported images (.png, .jpg, .jpeg, .svg) in the folder will be converted.
| Option | Default | Description |
|---|---|---|
--margin150 |
0.05 |
Margin fraction for 1.5 ratio output (e.g., 0.05 = 5%). |
--margin375 |
0.01 |
Margin fraction for 3.75 ratio output (e.g., 0.01 = 1%). |
--alpha-threshold |
10 |
Alpha cutoff [0..255] for treating a pixel as "solid". |
--corner-tol |
12 |
Tolerance vs. corner background color. |
--bg-white-threshold |
245 |
Pixels above this are considered "near white" (likely background). |
--pad-bg |
white |
Background for padding: "transparent", "white", or hex like "#333333". |
Input: Meta_logo.png
python logo_converter.py Meta_logo.png --pad-bg transparentOutputs:
Meta_logo_150ratio.png(1.5 aspect ratio, ~5% margin, transparent padding)Meta_logo_375ratio.png(3.75 aspect ratio, ~1% margin, transparent padding)
MIT License. See LICENSE for details.