Skip to main content

Easy Python image thumbnail manager with on-disk cache and WebP output. Python fork of robsonvleite/cropper (PHP).

Project description

Cropper (Python)

Port em Python do pacote PHP coffeecode/cropper de Robson V. Leite. Gera thumbnails JPG/PNG com cache em disco e saida WebP por padrao, com a mesma API de duas chamadas: make() e flush(). Pensado para uso direto em Flask/FastAPI/scripts (sem dependencia de framework).

Python port of the PHP package coffeecode/cropper by Robson V. Leite. Generates JPG/PNG thumbnails with on-disk caching and WebP output by default. Two-method API (make and flush), framework-agnostic (Flask / FastAPI / scripts).

Destaques

  • API minima: make(image, width[, height]) e flush([image])
  • Cache no disco com chave por dimensao (regenera so quando muda)
  • Saida WebP por padrao (opt-out com webp=False)
  • Crop centralizado automatico quando width + height sao fornecidos
  • Resize proporcional quando so width
  • Pillow (substitui ext-gd + webp-convert do PHP)

Instalacao

pip install coffeecode-cropper

Requer Python >= 3.9 e Pillow >= 10.

Uso

from coffeecode_cropper import Cropper

thumb = Cropper("cache", quality=75, compressor=5, webp=True)

# Resize proporcional pela largura
thumb.make("images/foto.jpg", 200)
# → "cache/foto-200-<hash>.webp"

# Crop centralizado para 400x400
thumb.make("images/foto.jpg", 400, 400)

# Banner widescreen
thumb.make("images/foto.jpg", 1200, 628)

# Limpa apenas as variacoes de uma imagem
thumb.flush("images/foto.jpg")

# Limpa todo o cache
thumb.flush()

FastAPI

from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from coffeecode_cropper import Cropper, CropperException

app = FastAPI()
cropper = Cropper("cache", quality=80, webp=True)

@app.get("/thumb/{name}")
def thumb(name: str, w: int, h: int | None = None):
    try:
        return FileResponse(cropper.make(f"images/{name}", w, h))
    except CropperException as e:
        raise HTTPException(400, str(e))

Veja example/fastapi_example.py para versao com flush() exposto.

API

Cropper(cache_path, quality=75, compressor=5, webp=True)

Parametro Tipo Descricao
cache_path str | Path Pasta onde os thumbs sao salvos. Criada se nao existir.
quality int Qualidade JPEG (1-100), tambem usado para WebP.
compressor int Nivel de compressao PNG (0-9).
webp bool Converte saida para WebP (default True).

make(image_path, width, height=None) -> str

Retorna o caminho do thumbnail (gera se nao existir, retorna do cache se sim).

  • width apenas → resize proporcional
  • width + height → crop centralizado para a proporcao alvo, depois resize

Excecoes:

  • ImageNotFoundError — arquivo de origem nao existe
  • UnsupportedImageError — origem nao e JPG/PNG
  • CropperException — base; cobre arquivos corrompidos / falhas de I/O

flush(image_path=None) -> None

  • Sem argumentos: apaga todos os arquivos da pasta de cache.
  • Com image_path: apaga apenas thumbs daquela imagem (matched via CRC32 do basename).

Equivalencia com a versao PHP

PHP Python
CoffeeCode\Cropper\Cropper coffeecode_cropper.Cropper
make($img, $w, $h) make(img, w, h)
flush($img) flush(img)
Erros como string ("Image not found") Excecoes (ImageNotFoundError etc.)
ext-gd + rosell-dk/webp-convert Pillow
bool $webP = false (default PHP) webp: bool = True (default Python)

Diferencas intencionais: errors viram excecoes (mais Pythonico) e o default de WebP foi promovido para True (alinhado com a recomendacao da v1.3+ da versao PHP).

Creditos

  • API e design original: Robson V. Leite — coffeecode/cropper (MIT)
  • Port Python: Kaue Leal

Licenca

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

coffeecode_cropper-1.0.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

coffeecode_cropper-1.0.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coffeecode_cropper-1.0.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for coffeecode_cropper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4e985dea5be8e484afd32a48617c8cffd76a4561d00a85bbce3961b6f9a9b82f
MD5 750f51481db3db100efba94709fd4408
BLAKE2b-256 b8f97df63c347488ea4339ae6503d4980d9ad97875d1d6e3db5538252df67372

See more details on using hashes here.

Provenance

The following attestation bundles were made for coffeecode_cropper-1.0.0.tar.gz:

Publisher: release.yml on kauelima21/coffeecode-cropper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for coffeecode_cropper-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 013c17f7fb538602200c4b22ad3270e809be987baf8561eadc509598cbca1de9
MD5 3509ff3e74d652664b63463904361779
BLAKE2b-256 41c12ccae3453f87b2a9e56532b5293bb87091271187ade8f3d4fc3fccb87505

See more details on using hashes here.

Provenance

The following attestation bundles were made for coffeecode_cropper-1.0.0-py3-none-any.whl:

Publisher: release.yml on kauelima21/coffeecode-cropper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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