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 (
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e985dea5be8e484afd32a48617c8cffd76a4561d00a85bbce3961b6f9a9b82f
|
|
| MD5 |
750f51481db3db100efba94709fd4408
|
|
| BLAKE2b-256 |
b8f97df63c347488ea4339ae6503d4980d9ad97875d1d6e3db5538252df67372
|
Provenance
The following attestation bundles were made for coffeecode_cropper-1.0.0.tar.gz:
Publisher:
release.yml on kauelima21/coffeecode-cropper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coffeecode_cropper-1.0.0.tar.gz -
Subject digest:
4e985dea5be8e484afd32a48617c8cffd76a4561d00a85bbce3961b6f9a9b82f - Sigstore transparency entry: 1414741822
- Sigstore integration time:
-
Permalink:
kauelima21/coffeecode-cropper@0731378aa93169f13bf8ac449ef44ca42329d05b -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/kauelima21
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0731378aa93169f13bf8ac449ef44ca42329d05b -
Trigger Event:
push
-
Statement type:
File details
Details for the file coffeecode_cropper-1.0.0-py3-none-any.whl.
File metadata
- Download URL: coffeecode_cropper-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
013c17f7fb538602200c4b22ad3270e809be987baf8561eadc509598cbca1de9
|
|
| MD5 |
3509ff3e74d652664b63463904361779
|
|
| BLAKE2b-256 |
41c12ccae3453f87b2a9e56532b5293bb87091271187ade8f3d4fc3fccb87505
|
Provenance
The following attestation bundles were made for coffeecode_cropper-1.0.0-py3-none-any.whl:
Publisher:
release.yml on kauelima21/coffeecode-cropper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coffeecode_cropper-1.0.0-py3-none-any.whl -
Subject digest:
013c17f7fb538602200c4b22ad3270e809be987baf8561eadc509598cbca1de9 - Sigstore transparency entry: 1414741962
- Sigstore integration time:
-
Permalink:
kauelima21/coffeecode-cropper@0731378aa93169f13bf8ac449ef44ca42329d05b -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/kauelima21
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0731378aa93169f13bf8ac449ef44ca42329d05b -
Trigger Event:
push
-
Statement type: