Skip to content

zhangcl/logo_converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

logo_converter

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.


Features

  • 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).
  • Format support:
    • PNG, JPEG/JPG, and SVG (requires CairoSVG).
    • Output is always PNG (to preserve alpha transparency).

Installation

  1. Clone the repo:

    git clone https://github.com/yourname/logo_converter.git
    cd logo_converter
  2. (Optional but recommended) Create a virtual environment:

    python3 -m venv .venv
    source .venv/bin/activate
  3. Install dependencies:

    pip install pillow cairosvg
    • Pillow: Image processing library
    • CairoSVG: Required for handling SVG input

Usage

Convert a single image

python logo_converter.py logo.png

This 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)

Convert an entire folder

python logo_converter.py ./logos/

All supported images (.png, .jpg, .jpeg, .svg) in the folder will be converted.

Options

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".

Example

Input: Meta_logo.png

python logo_converter.py Meta_logo.png --pad-bg transparent

Outputs:

  • Meta_logo_150ratio.png (1.5 aspect ratio, ~5% margin, transparent padding)
  • Meta_logo_375ratio.png (3.75 aspect ratio, ~1% margin, transparent padding)

License

MIT License. See LICENSE for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages