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 (
makeandflush), framework-agnostic (Flask / FastAPI / scripts).
Destaques
- API minima:
make(image, width[, height])eflush([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+heightsao fornecidos - Resize proporcional quando so
width - Pillow (substitui
ext-gd+webp-convertdo 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).
widthapenas → resize proporcionalwidth+height→ crop centralizado para a proporcao alvo, depois resize
Excecoes:
ImageNotFoundError— arquivo de origem nao existeUnsupportedImageError— origem nao e JPG/PNGCropperException— 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.
Release files for coffeecode-cropper 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coffeecode_cropper-1.0.0.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coffeecode_cropper-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / coffeecode_cropper-1.0.0.tar.gz
| Download URL | coffeecode_cropper-1.0.0.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e985dea5be8e484afd32a48617c8cffd76a4561d00a85bbce3961b6f9a9b82f
|
|
BLAKE2b-256 checksum How to use checksums |
b8f97df63c347488ea4339ae6503d4980d9ad97875d1d6e3db5538252df67372
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 30, 2026.
Transparency logRelease files / coffeecode_cropper-1.0.0-py3-none-any.whl
| Download URL | coffeecode_cropper-1.0.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
013c17f7fb538602200c4b22ad3270e809be987baf8561eadc509598cbca1de9
|
|
BLAKE2b-256 checksum How to use checksums |
41c12ccae3453f87b2a9e56532b5293bb87091271187ade8f3d4fc3fccb87505
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 30, 2026.
Transparency log