Skip to content

Commit 2a53cd0

Browse files
committed
Implements Pillow image generation example
1 parent 606f91f commit 2a53cd0

File tree

7 files changed

+727
-0
lines changed

7 files changed

+727
-0
lines changed

12-image-gen/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Image Generation with Pillow Example
2+
3+
This example demonstrates how to build a Python Worker that dynamically generates images using the Pillow (PIL) library.
4+
5+
## What It Does
6+
7+
The Worker provides four different image generation endpoints:
8+
1. **Gradient Generator** (`/gradient`) - Creates gradient images with customizable colors and dimensions
9+
2. **Badge Generator** (`/badge`) - Generates badges or buttons with text
10+
3. **Placeholder Generator** (`/placeholder`) - Creates placeholder images with dimensions displayed (like placehold.it)
11+
4. **Chart Generator** (`/chart`) - Produces simple bar charts
12+
13+
## How to Run
14+
15+
First ensure that `uv` is installed:
16+
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer
17+
18+
Now, if you run `uv run pywrangler dev` within this directory, it should use the config
19+
in `wrangler.jsonc` to run the example.
20+
21+
```bash
22+
uv run pywrangler dev
23+
```
24+
25+
Then visit:
26+
- `http://localhost:8787/` - Interactive demo page with all examples
27+
- `http://localhost:8787/gradient?width=600&height=300&color1=FF6B6B&color2=4ECDC4` - Gradient image
28+
- `http://localhost:8787/badge?text=Python+Workers&bg_color=2196F3` - Custom badge
29+
- `http://localhost:8787/placeholder?width=500&height=300` - Placeholder image
30+
- `http://localhost:8787/chart?values=15,30,25,40,20&labels=Mon,Tue,Wed,Thu,Fri` - Bar chart
31+
32+
## Deployment
33+
34+
Deploy to Cloudflare Workers:
35+
36+
```bash
37+
uv run pywrangler deploy
38+
```
39+
40+
## Learn More
41+
42+
- [Pillow Documentation](https://pillow.readthedocs.io/)
43+
- [Python Workers Documentation](https://developers.cloudflare.com/workers/languages/python/)
44+
- [ImageDraw Reference](https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html)

12-image-gen/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "python-image-gen",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "uv run pywrangler deploy",
7+
"dev": "uv run pywrangler dev",
8+
"start": "uv run pywrangler dev"
9+
},
10+
"devDependencies": {
11+
"wrangler": "^4.46.0"
12+
}
13+
}

12-image-gen/pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[project]
2+
name = "python-image-gen"
3+
version = "0.1.0"
4+
description = "Python image generation example using Pillow"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"webtypy>=0.1.7",
9+
"pillow",
10+
]
11+
12+
[dependency-groups]
13+
dev = [
14+
"workers-py",
15+
"workers-runtime-sdk"
16+
]

0 commit comments

Comments
 (0)