Skip to main content

Automatically convert PNG/JPG images to WebP with responsive sizes. CLI, file watcher, and Python API.

Project description

webp-convert (Python)

Automatically convert PNG/JPG/GIF/TIFF images to WebP with responsive size variants. Zero-config CLI, file watcher, and programmatic Python API.


Install

pip3 install webp-convert

Requires Python 3.8+. Uses Pillow — no external binaries required.


Quick start

# Convert everything in assets/images (default)
webp-convert

# Custom dir + quality
webp-convert src/img -q 90

# Watch mode — auto-converts on file change
webp-convert src/img --watch

# Custom responsive sizes
webp-convert src/img --sizes 1200,800,400

# Write outputs to a separate directory
webp-convert src/img -o dist/img

CLI reference

Usage: webp-convert [INPUT_DIR] [OPTIONS]

  Convert PNG/JPG images to WebP with responsive size variants.

Options:
  -o, --output TEXT       Output directory (default: same as source)
  -q, --quality INT       WebP quality 0-100  [default: 80]
      --lossless          Lossless encoding
      --sizes W,W,...     Responsive widths e.g. 1200,800,400 (0=full)
      --no-full           Skip full-size, only responsive variants
  -r, --recursive         Recurse into subdirectories
      --no-overwrite      Skip files that already have a .webp counterpart
  -w, --watch             Watch mode
      --silent            Suppress all output
  -h, --help              Show this message and exit

Programmatic API

from webp_convert import convert, watch, convert_file, ConvertConfig, SizeSpec

# --- Convert a directory (keyword args shortcut) ---
results = convert(input="src/images", quality=85)

# --- Convert with a config object ---
cfg = ConvertConfig(
    input="src/images",
    output="public/images",  # omit to write next to originals
    quality=85,
    lossless=False,
    recursive=True,
    overwrite=True,
    sizes=[
        SizeSpec(width=0,    suffix=""),       # full size
        SizeSpec(width=1200, suffix="-1200"),
        SizeSpec(width=800,  suffix="-800"),
        SizeSpec(width=400,  suffix="-400"),
    ],
    on_file=lambda r: print(f"{r.src}{r.dest} ({r.size_bytes} bytes)"),
)
results = convert(cfg)

# --- Convert a single file ---
results = convert_file("hero.png", cfg)

# --- Watch mode (context manager) ---
import time
watcher = watch(ConvertConfig(input="src/images")).start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    watcher.stop()

Output structure

Given hero.jpg in src/images/:

src/images/
├── hero.jpg          (original, kept by default)
├── hero.webp         (full size)
├── hero-800.webp     (800px wide, aspect-ratio preserved)
└── hero-400.webp     (400px wide)

HTML usage

<picture>
  <source
    type="image/webp"
    srcset="hero-400.webp 400w, hero-800.webp 800w, hero.webp 1600w"
    sizes="(max-width: 400px) 400px, (max-width: 800px) 800px, 1600px"
  />
  <img src="hero.jpg" alt="Hero image" />
</picture>

License

MIT

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

webp_convert-1.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

webp_convert-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file webp_convert-1.0.0.tar.gz.

File metadata

  • Download URL: webp_convert-1.0.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for webp_convert-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b7aa47240431f753c16e2c9d9038c311833d2605b7d10e873280f7bc27730452
MD5 135536fa126d975387c33c48bb39c735
BLAKE2b-256 e1c9f9b8a81113fd754320ba5bc6f654487b97370a6d9880b6dbbdc7bb9097d9

See more details on using hashes here.

File details

Details for the file webp_convert-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: webp_convert-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for webp_convert-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d4be6ebce9c2afa679f0da11609322346bf906ad774681722189e42d18dc6a3
MD5 46121a5c7819ab464b93ab6e7b11c9df
BLAKE2b-256 ff5062708397b79247d005d4697d1a4ec6377c73b03d10d6cc2f37f14640b649

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