This example demonstrates how to build a Python Worker that dynamically generates images using the Pillow (PIL) library.
The Worker provides four different image generation endpoints:
- Gradient Generator (
/gradient) - Creates gradient images with customizable colors and dimensions - Badge Generator (
/badge) - Generates badges or buttons with text - Placeholder Generator (
/placeholder) - Creates placeholder images with dimensions displayed - Chart Generator (
/chart) - Produces simple bar charts
First ensure that uv is installed:
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer
Now, if you run uv run pywrangler dev within this directory, it should use the config
in wrangler.jsonc to run the example.
uv run pywrangler devThen visit:
http://localhost:8787/- Interactive demo page with all exampleshttp://localhost:8787/gradient?width=600&height=300&color1=FF6B6B&color2=4ECDC4- Gradient imagehttp://localhost:8787/badge?text=Python+Workers&bg_color=2196F3- Custom badgehttp://localhost:8787/placeholder?width=500&height=300- Placeholder imagehttp://localhost:8787/chart?values=15,30,25,40,20&labels=Mon,Tue,Wed,Thu,Fri- Bar chart
Deploy to Cloudflare Workers:
uv run pywrangler deploy