Skip to content

Claude paints with code: a Claude Code skill for procedural illustration in 83 styles, from ink wash (水墨), watercolour and ukiyo-e to stained glass, Roman mosaic, Art Deco, shadow puppets, pastel UI, sketchbook doodles, spot illustrations, grainy picture books, dreamy hills, sunlit greenery. No image model: numpy + Pillow on a CPU, draw-on GIFs.

Add coloured-pencil, anime and editorial styles

cloveric67539f46c023
diff --git a/README.md b/README.md
index 0fe572c..c9f2a93 100644
--- a/README.md
+++ b/README.md
@@ -1,21 +1,27 @@
# Claude Drawing · Claude 绘图
-**Claude paints with code — no image model.** Ink wash · watercolour · torn-paper collage.
-**不用生图模型,Claude 用代码一笔一笔作画。** 水墨 · 水彩 · 剪纸拼贴。
+**Claude paints with code — no image model.** Ink wash · watercolour · paper collage · coloured pencil · anime · editorial.
+**不用生图模型,Claude 用代码一笔一笔作画。** 水墨 · 水彩 · 剪纸拼贴 · 韩国彩铅 · 日本动漫 · 编辑风手绘。
[English](#english) · [中文](#中文)
| Ink wash · 水墨 | Watercolour · 水彩 | Paper collage · 剪纸拼贴 |
|---|---|---|
| ![ink](examples/drawing_bawansiqian.gif) | ![watercolour](examples/drawing_watercolor_autumn.gif) | ![collage](examples/drawing_papercut_balloons.gif) |
+| **Coloured pencil · 韩国彩铅** | **Anime · 日本动漫** | **Editorial · 编辑风手绘** |
+| ![pencil](examples/drawing_colorpencil_dessert.gif) | ![anime](examples/drawing_anime_summer.gif) | ![editorial](examples/drawing_editorial_ideas.gif) |
| 八万四千法门 · *Many paths, one summit* | 月印万川 · *One moon in ten thousand rivers* |
|---|---|
| ![bawansiqian](examples/bawansiqian.jpg) | ![moon_river](examples/moon_river.jpg) |
| **秋日湖畔** · *Autumn Lake* | **热气球小镇** · *Balloon Day* |
| ![autumn](examples/watercolor_autumn.jpg) | ![balloons](examples/papercut_balloons.jpg) |
+| **午后甜点** · *Afternoon Dessert* | **夏空** · *Summer Sky* |
+| ![dessert](examples/colorpencil_dessert.jpg) | ![anime](examples/anime_summer.jpg) |
+| **灵感生长** · *Growing Ideas* | |
+| ![ideas](examples/editorial_ideas.jpg) | |
-Every mark above is computed: paper fibres, ink washes and mist, dry-brush strokes, pigment granulation and blooms, torn-paper edges, crayon wax and newsprint. No diffusion model, no API, no stock art. It is just Python with **numpy + Pillow** on a CPU: 2–4 s for an ink painting and 15–30 s for watercolour or collage, at 1920×1080.
+Every mark above is computed: paper fibres, ink washes and mist, dry-brush strokes, pigment granulation and blooms, torn-paper edges, pencil wax on paper tooth, cel-shaded clouds, risograph grain and halftones. No diffusion model, no API, no stock art. It is just Python with **numpy + Pillow** on a CPU: 2–4 s for an ink painting and 10–30 s for the other styles, at 1920×1080.
---
@@ -23,13 +29,16 @@ Every mark above is computed: paper fibres, ink washes and mist, dry-brush strok
### What it is
-A [Claude Code](https://claude.com/claude-code) skill and a small painting library with three styles:
+A [Claude Code](https://claude.com/claude-code) skill and a small painting library with six styles:
| Style | Module | What makes it read as the real medium |
|---|---|---|
| **Ink wash (水墨)** | `lib/inkpaint.py` · `Painting` | rice paper; occluding washes with wet rims and mist; bristle brush with flying-white; texture strokes (皴); moss dots; pines, boats, reeds; a moon reserved in white; calligraphy and seal |
| **Watercolour** | `lib/watercolor.py` · `Watercolor` | cold-press paper tooth; *transparent glazes* that mix subtractively; wet-edge darkening; granulation; blooms (back-runs); wet-in-wet feathering; lifting; splatter |
| **Paper collage** | `lib/papercut.py` · `Collage` | torn edges with a white fibrous core; paper depth shadows; coloured, kraft, crayon, newsprint and ruled-notebook papers; waxy crayon lines; cute faces with blush |
+| **Coloured pencil (Korean style)** | `lib/colorpencil.py` · `ColorPencil` | short directional hatching; wax that only catches on the paper tooth, with pressure pushing into the valleys; soft layered blending; light brown pencil outlines; white gel-pen sparkles |
+| **Anime (Japanese)** | `lib/anime.py` · `Anime` | saturated gradient skies; towering cumulus with *crescent* cel shadows on every puff; sun bloom, god rays and lens flare; flat-shaded fields, perspective roads, sagging power lines; clean line-art characters |
+| **Editorial (risograph)** | `lib/editorial.py` · `Editorial` | 4–5 spot inks on cream stock; per-ink grain, uneven density and mis-registration; translucent overprinting (overlaps make new colours); halftone shading; wobbly hand-drawn line; big simple shapes and stylised figures |
Ask Claude to *"draw it yourself"*, *"paint this with code"*, or *"claude绘图"*. It then works in three steps:
@@ -88,6 +97,9 @@ Full scenes: `python3 examples/<name>.py out.png --stages stages/`. `--stages` a
- **Ink**: paint far to near, and each wash occludes what is behind it. Tie dab spacing to bristle size so strokes never look beaded. Paths should be few, smooth and emerge from mist. Texture strokes should be long, follow the slope and sit on the shadow side. Ridge lines stay light and broken.
- **Watercolour**: keep the paper tooth subtle. Pool shadows wet-in-wet on one side (no bull's-eyes). Paint stems before leaves and keep them outside the leaf. Use at most three glazes in one place, and save lights or lift them out.
- **Collage**: big areas use flat or kraft paper, and crayon texture is only for accents. Torn white rims are 5–8 px wide and appear on only part of an edge. Shadows use a zero-padded shift, never a wrap-around one. A moon gets a halo, not sun rays.
+- **Coloured pencil**: hatch densely enough that strokes saturate, and let pressure decide how deep the wax reaches into the tooth. White objects (cream, china) are warm off-white with lavender or blue-grey shading, never white on white.
+- **Anime**: clouds are many lumpy puffs, each with a hard crescent shadow (the puff minus itself shifted toward the sun), not smooth circles with straight terminators. Keep characters' proportions (hem near the knee).
+- **Editorial**: use 4–5 inks and overlap them on purpose so overprints make the extra colours. Every ink layer gets grain, uneven density and a few pixels of mis-registration, otherwise it looks like clip-art.
### Roadmap
@@ -100,13 +112,16 @@ Full scenes: `python3 examples/<name>.py out.png --stages stages/`. `--stages` a
### 这是什么
-一个 [Claude Code](https://claude.com/claude-code) skill,加上一个小巧的绘图库,内置三种画风:
+一个 [Claude Code](https://claude.com/claude-code) skill,加上一个小巧的绘图库,内置六种画风:
| 画风 | 模块 | 为什么看起来像真的 |
|---|---|---|
| **水墨** | `lib/inkpaint.py` · `Painting` | 宣纸纹理;会遮挡身后的墨染山体,带水痕边和雾;分笔毛的毛笔与飞白;皴擦、苔点;松、孤舟、芦苇;烘云托月的留白月亮;题字与印章 |
| **水彩** | `lib/watercolor.py` · `Watercolor` | 冷压水彩纸纹理;**透明罩染**,叠色像真颜料一样变深、混色;水痕边;颜料颗粒;水渍花;湿画晕开;提白;甩点 |
| **剪纸拼贴** | `lib/papercut.py` · `Collage` | 撕纸露出的白色纤维边;纸片层叠的阴影;彩纸、牛皮纸、蜡笔、报纸、笔记本横线纸;蜡笔线条;带腮红的可爱圆脸 |
+| **韩国彩铅** | `lib/colorpencil.py` · `ColorPencil` | 细密的短排线;蜡只挂在纸纹凸起上,用力越大越能压进凹处;柔和叠色;浅褐色铅笔勾线;白色高光笔 |
+| **日本动漫** | `lib/anime.py` · `Anime` | 高饱和的天空渐变;每个鼓包都带**月牙形**硬边暗面的积雨云;阳光泛光、光束、镜头光斑;平涂田野、透视公路、下垂的电线;干净线稿的角色 |
+| **编辑风手绘** | `lib/editorial.py` · `Editorial` | 米色纸上 4–5 种专色;每层油墨有颗粒、浓淡不匀和套色错位;透明叠印(重叠处出新颜色);半调网点;歪扭的手绘墨线;大块简洁造型和几何人物 |
对 Claude 说「claude绘图」「你自己画」「用代码画」,它会:
1. **先定构图**:层次、焦点、留白、一处点睛色,而且画面要图解内容本身,不是随便一幅风景;
@@ -131,13 +146,16 @@ pip install -r ~/.claude/skills/claude-drawing/requirements.txt # numpy、Pill
### 快速上手
-三种画风的最小示例见上方英文部分的代码。完整范例:
+水墨、水彩、剪纸的最小示例见上方英文部分的代码。全部范例:
```bash
python3 examples/bawansiqian.py out.png --stages stages/ # 水墨:八万四千法门
python3 examples/moon_river.py out.png # 水墨:月印万川
python3 examples/watercolor_autumn.py out.png # 水彩:秋日湖畔
python3 examples/papercut_balloons.py out.png # 剪纸:热气球小镇
+python3 examples/colorpencil_dessert.py out.png # 韩国彩铅:午后甜点
+python3 examples/anime_summer.py out.png # 日本动漫:夏空
+python3 examples/editorial_ideas.py out.png # 编辑风:灵感生长
```
### 作画要点(Claude 交图前逐条自查)
@@ -145,6 +163,9 @@ python3 examples/papercut_balloons.py out.png # 剪纸:热
- **水墨**:先远后近,每层墨染遮挡身后;落笔间距跟笔毛挂钩,笔画不会一节一节;小路要少、平滑、从雾里伸出来;皴擦长而顺坡、集中在阴面;山脊线要淡而断续。
- **水彩**:纸纹要细;阴影用湿画偏一侧积色,不要画成靶心;先画叶梗,并且只画在叶子外面;同一处罩染不超过三层,亮部留白或提白。
- **剪纸**:大色块用平纸或牛皮纸,蜡笔只做点缀;撕口白边宽 5–8 像素,只出现在部分边缘;阴影偏移要补零,不能首尾相接;月亮用光圈,不画放射线。
+- **彩铅**:排线要够密,颜色才能叠到饱和,用力大小决定能压进多少纸纹;奶油、白瓷这类白色物体用暖奶白色配淡紫灰或淡蓝灰阴影,不要白画在白纸上。
+- **动漫**:云要用许多不规则团块,每团带月牙形的硬边暗面(团块减去朝太阳偏移的自身),不要画成光滑正圆加直线明暗交界;人物比例要对,裙摆在膝盖附近。
+- **编辑风**:专色 4–5 种,并刻意让它们重叠,靠叠印得到更多颜色;每层油墨都要有颗粒、浓淡不匀和几像素的套色错位,否则就像电脑剪贴画。
### 计划
diff --git a/SKILL.md b/SKILL.md
index f69e0a5..9319218 100644
--- a/SKILL.md
+++ b/SKILL.md
@@ -1,6 +1,6 @@
---
name: claude绘图
-description: Claude 绘图 / Claude Drawing —— 不借助任何生图模型,由 Claude 用代码一笔一笔「画」出图片(程序化绘画)。内置三种画风:中国水墨(宣纸、层层远山与雾、飞白、皴擦、苔点、松、孤舟、烘云托月、题字印章)、水彩(湿画渐变、透明罩染、水痕边、颜料颗粒、水渍花、提白)、剪纸拼贴(撕纸白边、纸片阴影、彩纸/牛皮纸/报纸/笔记本纸、蜡笔、腮红圆脸的可爱小人)。用户说「claude绘图」「用代码画」「你自己画」「不用生图模型画」「程序化绘画」「代码水墨/水彩/剪纸」,或 "draw it yourself", "paint with code", "procedural painting", "ink wash / watercolor / paper collage without an image model" 时使用;也用于需要逐步画出(draw-on)动画的插画。
+description: Claude 绘图 / Claude Drawing —— 不借助任何生图模型,由 Claude 用代码一笔一笔「画」出图片(程序化绘画)。内置六种画风:中国水墨(宣纸、远山与雾、飞白、皴擦、题字印章)、水彩(透明罩染、水痕边、颗粒、水渍花、倒影)、剪纸拼贴(撕纸白边、纸片阴影、报纸/笔记本纸、蜡笔、可爱小人)、韩国彩铅(细密排线、纸纹颗粒、柔和叠色、白色高光笔)、日本动漫(赛璐璐积雨云、蓝天、光晕光斑、线稿角色)、编辑风手绘(Riso 套色叠印、颗粒与错位、半调网点、手绘墨线、几何人物)。用户说「claude绘图」「用代码画」「你自己画」「不用生图模型画」「程序化绘画」,或点名水墨/水彩/剪纸/彩铅/动漫/编辑插画,或 "draw it yourself", "paint with code", "procedural painting", "watercolor / paper collage / colored pencil / anime / editorial illustration without an image model" 时使用;也用于需要逐步画出(draw-on)动画的插画。
---
# Claude 绘图
@@ -17,10 +17,19 @@ description: Claude 绘图 / Claude Drawing —— 不借助任何生图模型
| 水墨 | `lib/inkpaint.py` · `Painting` | `examples/bawansiqian.py` 八万四千法门、`examples/moon_river.py` 月印万川 | 2–4 秒 |
| 水彩 | `lib/watercolor.py` · `Watercolor` | `examples/watercolor_autumn.py` 秋日湖畔(秋树罩染、湖面倒影、红色小舟) | 约 15 秒 |
| 剪纸拼贴 | `lib/papercut.py` · `Collage` | `examples/papercut_balloons.py` 热气球小镇(条纹热气球、笑脸太阳、纸云、小房子) | 约 30 秒 |
+| 韩国彩铅 | `lib/colorpencil.py` · `ColorPencil` | `examples/colorpencil_dessert.py` 午后甜点(草莓蛋糕、红茶、草莓) | 约 17 秒 |
+| 日本动漫 | `lib/anime.py` · `Anime` | `examples/anime_summer.py` 夏空(积雨云、乡间公路、电线杆、草帽女孩背影) | 约 30 秒 |
+| 编辑风手绘 | `lib/editorial.py` · `Editorial` | `examples/editorial_ideas.py` 灵感生长(浇水长出灯泡的概念插画) | 约 11 秒 |
-三种画风共用 `lib/core.py` 里的底层工具:噪声、模糊、样条曲线、多边形遮罩、有机轮廓 `blob_pts`、毛笔 `bristle_stroke`、中文字体查找。
+各画风共用 `lib/core.py` 里的底层工具:噪声、模糊、样条曲线、多边形遮罩、有机轮廓 `blob_pts`、毛笔 `bristle_stroke`、中文字体查找。
-**用户没有指定画风时怎么选**:禅意、古典、山水题材用水墨;清新、花卉、风景、带颜色的题材用水彩;童趣、温暖、故事感、角色类题材用剪纸拼贴。
+**用户没有指定画风时怎么选**:
+- 禅意、古典、山水 → 水墨;
+- 清新风景、花卉 → 水彩;
+- 童趣、温暖、讲故事 → 剪纸拼贴;
+- 甜点、小物、日常可爱 → 韩国彩铅;
+- 青春、夏日、天空、背景美术 → 日本动漫;
+- 观点、概念、商业文章配图 → 编辑风手绘。
## 一、流程(每幅画都照做)
@@ -70,6 +79,30 @@ from core import spline, curve, blob_pts, fbm1d, blur, polygon_mask
- `face(cx, cy, r, mood='smile'|'sleep'|'dots')`:笑眼、嘴、腮红
- `text(s, x, y, size, 颜色, vertical)`
+**韩国彩铅 `ColorPencil`**:
+- `paper()`:细纸纹
+- 遮罩:`mask_poly(pts)`、`mask_blob()`、`mask_ellipse()`
+- `hatch(mask, 颜色, pressure, angle)`:短排线铺色。蜡只挂在纸纹凸起上,pressure 越大越能压进凹处;边缘自动变淡
+- `shade(mask, 颜色, angle=另一个角度)`:交叉排线加阴影
+- `outline(pts, '#8a6a5a')`:浅褐色铅笔勾线;`line(pts)` 用于不闭合的线
+- `sparkle(x, y, r)` 白色高光笔;`dots(mask, 颜色)` 草莓籽、糖粒之类
+- 白色物体不要用白色画(白纸上看不见):用暖奶白色,配淡紫灰或淡蓝灰阴影
+
+**日本动漫 `Anime`**:
+- `sky(top, mid, horizon_col, horizon)`:天空渐变
+- `cumulus(cx, base_y, w, h, light)`:积雨云。许多不规则团块从后往前叠,每团的暗面是「团块减去朝光源偏移的自身」形成的月牙形硬边
+- `sun_flare(x, y)`:光晕、光束、镜头光斑;`sparkles()`;最后加 `bloom()` 泛光和 `vignette()` 暗角
+- `field(top_y, colours)`:平涂分带的田野;`pole()` 电线杆、`wire(p0, p1, sag)` 下垂电线
+- 平涂和线稿:`fill(mask, 颜色)`、`gradient()`、`screen()`;`lines(pts, width, 颜色)` 画干净线稿;`poly()`、`circle()`
+- 角色:平涂两色(亮色和阴影色)加深色描边
+
+**编辑风手绘 `Editorial`**:
+- 形状:`circle()`、`ellipse()`、`rrect()` 圆角矩形、`blob()`、`poly()`
+- `fill(mask, 专色)`:印一层专色油墨,带颗粒、浓淡不匀、套色错位,并**叠印**(重叠处颜色相乘)
+- `halftone(mask, 专色, cell, angle, amount)`:半调网点做明暗
+- `line(pts, 墨色, width, wobble, breaks)`:歪扭的手绘墨线;`dashes()` 虚线;`stars()` 小十字星
+- 专色控制在 4–5 种,并刻意让它们互相重叠,叠出第三种颜色
+
**顺序**:都是先远后近、先大后小,文字最后叠加。水墨的雾要画在它该吞没的东西之后;水彩的叶梗要先画,并且只画在叶子外面。
## 三、毛病清单(都真实出现过)
@@ -110,9 +143,30 @@ from core import spline, curve, blob_pts, fbm1d, blur, polygon_mask
| 小鸟画成了一团 | 小鸟用两道细弧线(线宽约 2.6),不要用很短的粗笔 |
| 纸片被画面边缘切掉 | 云、气球这类主体要完整留在画面内,留出边距 |
+**韩国彩铅**
+
+| 问题 | 改法 |
+|---|---|
+| 颜色淡得几乎看不见 | 排线要够密(叠加后趋于饱和);颗粒阈值不要卡太狠,用 pressure 控制能压进多少纸纹 |
+| 奶油、白瓷等白色物体消失 | 用暖奶白色铺底,配淡紫灰或淡蓝灰阴影,再加一点白色高光 |
+
+**日本动漫**
+
+| 问题 | 改法 |
+|---|---|
+| 云是一个个正圆泡泡,明暗交界是斜直线 | 用不规则团块,数量多、大小不一;暗面用「团块减去偏移的自身」做成月牙形 |
+| 人物腿太长 | 裙摆到膝盖附近,露出的腿约为身高的三分之一 |
+
+**编辑风手绘**
+
+| 问题 | 改法 |
+|---|---|
+| 色块死板,像电脑矢量图 | 每层专色都要有颗粒、浓淡不匀和几像素的套色错位 |
+| 颜色太多太杂 | 4–5 种专色,靠叠印得到其他颜色(比如青叠黄出绿) |
+
## 四、做成动画
-- 各画风都有 `stage(name)` 和 `save(path, stages_dir)`,按阶段依次淡入叠化,就是「一幅画被逐步画出来」。README 里的四张动图就是这样做的。
+- 各画风都有 `stage(name)` 和 `save(path, stages_dir)`,按阶段依次淡入叠化,就是「一幅画被逐步画出来」。README 里的动图就是这样做的。
- 更细的逐笔动画:把一组笔画单独画到透明层上导出,再用 Motion Canvas 遮罩按顺序显现。这个还没做成现成接口。
## 五、文件
@@ -122,6 +176,9 @@ lib/core.py 公共底层(噪声、模糊、样条、遮罩、毛笔
lib/inkpaint.py 水墨
lib/watercolor.py 水彩
lib/papercut.py 剪纸拼贴
+lib/colorpencil.py 韩国彩铅
+lib/anime.py 日本动漫
+lib/editorial.py 编辑风手绘
examples/*.py 范例脚本;*.jpg 成品;drawing_*.gif 逐步画出的动图
中文字体:自动找 Kaiti/Songti(macOS)、Noto CJK(Linux)、KaiTi/SimSun(Windows),或设 INKPAINT_FONT
```
diff --git a/examples/anime_summer.jpg b/examples/anime_summer.jpg
new file mode 100644
index 0000000..63d5fd3
Binary files /dev/null and b/examples/anime_summer.jpg differ
diff --git a/examples/anime_summer.py b/examples/anime_summer.py
new file mode 100644
index 0000000..e8b0332
--- /dev/null
+++ b/examples/anime_summer.py
@@ -0,0 +1,109 @@
+"""Example (Japanese anime background): Summer Sky / 夏空 — towering cel-shaded cumulus, sun bloom and
+lens flare, a country road with power lines, and a girl in a straw hat seen from behind.
+Pure code, no image model.
+python3 anime_summer.py [out.png] [--stages DIR]
+"""
+import os, sys
+sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'lib'))
+import numpy as np
+from anime import Anime, hexc
+from core import spline, fbm1d, blur
+
+out = sys.argv[1] if len(sys.argv) > 1 and not sys.argv[1].startswith('--') else 'anime_summer.png'
+stages = sys.argv[sys.argv.index('--stages') + 1] if '--stages' in sys.argv else None
+
+a = Anime(1920, 1080, seed=720)
+rng = a.rng
+HOR = 640
+VX, VY = 1000, HOR # vanishing point
+
+# ---- sky, high wisps, towering clouds lit from the upper left
+a.sky('#1a4fb8', '#4c9be6', '#d3efff', horizon=HOR)
+for (x, y, w) in [(300, 120, 520), (820, 70, 380), (1650, 110, 300)]:
+ wisp = a.poly(spline([(x - w / 2, y + 10), (x - w / 4, y - 8), (x + w / 3, y - 4), (x + w / 2, y + 6), (x, y + 16), (x - w / 2, y + 10)], 10))
+ a.fill(blur(wisp, 6), '#ffffff', 0.45)
+a.cumulus(1330, HOR - 20, 980, 560, seed=3)
+a.cumulus(330, HOR - 10, 640, 240, seed=4)
+a.cumulus(760, HOR - 5, 360, 130, seed=5)
+a.stage('sky')
+
+# ---- distant hills and fields
+hills = [(0, HOR + 12)] + [(x, HOR - 22 - 26 * np.sin(x / 210 + 0.5) - 12 * fbm1d(40, 8, 3, 9)[i]) for i, x in enumerate(np.linspace(0, 1920, 40))] + [(1920, HOR + 12)]
+a.fill(a.poly(hills), '#5f8fae')
+a.fill(a.poly(hills) * (a.YY > HOR - 30), '#4d7c9c', 0.8)
+a.field(HOR, colours=('#86cf5e', '#62b54f'), bands=9)
+# rice-paddy rows converging on the vanishing point (thin darker lines)
+for k in range(-18, 19):
+ if k == 0: continue
+ x_bottom = VX + k * 160
+ a.lines([(VX + k * 3, VY + 2), (x_bottom, 1080)], 1.6, '#4f9a44', 0.5)
+a.stage('fields')
+
+# ---- the road
+road = [(VX - 6, VY), (VX + 6, VY), (1260, 1080), (620, 1080)]
+a.fill(a.poly(road), '#cfc6b2')
+a.fill(a.poly([(VX, VY), (VX + 6, VY), (1260, 1080), (1000, 1080)]), '#b9b09b', 0.7) # shaded half
+for side in (-1, 1):
+ x_b = 1260 if side > 0 else 620
+ a.lines([(VX + side * 5, VY), (x_b - side * 26, 1080)], 5, '#f4f1e8', 0.9)
+# power poles in perspective along the right side, with sagging wires
+tops = []
+for i in range(7):
+ t = (0.82 ** (i * 1.6))
+ x = VX + 40 + (1640 - VX - 40) * t
+ base = VY + (1040 - VY) * t
+ hgt = 30 + 560 * t
+ tops.append(a.pole(x, base, hgt, width=max(2, 12 * t)))
+for i in range(len(tops) - 1):
+ for dy in (0, 12):
+ a.wire((tops[i][0] - 30 * (0.82 ** (i * 1.6)), tops[i][1] + dy), (tops[i + 1][0], tops[i + 1][1] + dy * 0.8), sag=40 * (0.82 ** (i * 1.6)), width=1.8)
+a.wire((tops[0][0], tops[0][1]), (1920 + 40, tops[0][1] - 120), sag=60, width=2.2)
+a.wire((tops[0][0], tops[0][1] + 12), (1920 + 40, tops[0][1] - 90), sag=60, width=2.2)
+a.stage('road')
+
+# ---- tall grass in the foreground corners
+for (x0, x1) in [(0, 480), (1500, 1920)]:
+ for _ in range(90):
+ x = rng.uniform(x0, x1); h = rng.uniform(80, 200)
+ lean = rng.normal(0, 30)
+ blade = [(x - 7, 1085), (x + lean, 1085 - h), (x + 7, 1085)]
+ a.fill(a.poly(blade), rng.choice(['#3f8f3c', '#4ea447', '#2f7331']))
+
+# ---- girl in a straw hat, seen from behind (clean line art + two-tone cel shading)
+GX, GY = 880, 950 # feet
+INK = '#3b2d3c'
+legs = [[(GX - 24, GY - 110), (GX - 22, GY)], [(GX + 20, GY - 110), (GX + 22, GY)]]
+for L in legs:
+ a.lines(L, 22, '#f2cfb4'); a.lines(L, 22, '#e3b393', 0.35)
+a.fill(a.poly([(GX - 60, GY - 4), (GX + 64, GY - 4), (GX + 60, GY + 10), (GX - 58, GY + 10)]), '#000000', 0.12) # ground shadow
+dress = [(GX - 48, GY - 330), (GX + 48, GY - 330), (GX + 100, GY - 96), (GX - 100, GY - 96)]
+a.fill(a.poly(dress), '#fbfbff')
+a.fill(a.poly([(GX + 10, GY - 330), (GX + 48, GY - 330), (GX + 100, GY - 96), (GX + 22, GY - 96)]), '#c9d6f2') # shadow side
+for fx in (-40, 0, 38):
+ a.lines([(GX + fx * 0.4, GY - 240), (GX + fx, GY - 98)], 1.8, '#9fb2dc')
+a.lines(dress, 3, INK, closed=True)
+for side in (-1, 1): # arms
+ arm = [(GX + side * 46, GY - 322), (GX + side * 64, GY - 250), (GX + side * 70, GY - 185)]
+ a.lines(arm, 16, '#f2cfb4'); a.lines(arm, 16, '#e3b393', 0.3 if side < 0 else 0.0)
+hair = spline([(GX - 40, GY - 360), (GX - 60, GY - 300), (GX - 56, GY - 230), (GX - 30, GY - 205), (GX, GY - 214), (GX + 30, GY - 205), (GX + 56, GY - 230), (GX + 60, GY - 300), (GX + 40, GY - 360)], 10).tolist()
+a.fill(a.poly(hair + [(GX, GY - 400)]), '#5a3b2e')
+for hx in (-26, -8, 12, 30):
+ a.lines(spline([(GX + hx * 0.6, GY - 350), (GX + hx, GY - 300), (GX + hx * 1.1, GY - 250)], 6), 2, '#8a5a44', 0.8)
+a.lines(hair, 3, INK)
+# straw hat: crown, brim (cel shaded), red ribbon
+brim = [(GX + 118 * np.cos(t), GY - 372 + 34 * np.sin(t)) for t in np.linspace(0, 2 * np.pi, 80, endpoint=False)]
+a.fill(a.poly(brim), '#eed094')
+a.fill(a.poly(brim) * (a.YY > GY - 372), '#d9b36e', 0.9)
+crown = spline([(GX - 58, GY - 380), (GX - 52, GY - 430), (GX, GY - 448), (GX + 52, GY - 430), (GX + 58, GY - 380)], 10).tolist()
+a.fill(a.poly(crown + [(GX, GY - 372)]), '#f3da9f')
+a.fill(a.poly([(GX - 58, GY - 392), (GX + 58, GY - 392), (GX + 58, GY - 376), (GX - 58, GY - 376)]), '#d64553')
+a.lines(brim, 3, INK, closed=True); a.lines(crown, 3, INK)
+a.stage('girl')
+
+# ---- light: sun behind the upper left, bloom, sparkles, vignette
+a.sun_flare(470, 110, strength=1.0)
+a.sparkles(28, box=(250, 40, 900, 420))
+a.bloom(0.86, 20, 0.35)
+a.vignette(0.2)
+a.save(out, stages_dir=stages)
+print('saved', out)
diff --git a/examples/colorpencil_dessert.jpg b/examples/colorpencil_dessert.jpg
new file mode 100644
index 0000000..795ae2e
Binary files /dev/null and b/examples/colorpencil_dessert.jpg differ
diff --git a/examples/colorpencil_dessert.py b/examples/colorpencil_dessert.py
new file mode 100644
index 0000000..6d740cf
--- /dev/null
+++ b/examples/colorpencil_dessert.py
@@ -0,0 +1,134 @@
+"""Example (Korean-style coloured pencil): Afternoon Dessert / 午后甜点 — strawberry shortcake,
+a cup of tea, strawberries; soft layered hatching, grainy wax on paper tooth, brown pencil
+outlines, white gel-pen sparkles. Pure code, no image model.
+python3 colorpencil_dessert.py [out.png] [--stages DIR]
+"""
+import os, sys
+sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'lib'))
+import numpy as np
+from colorpencil import ColorPencil, hexc
+from core import spline, blob_pts, blur
+
+out = sys.argv[1] if len(sys.argv) > 1 and not sys.argv[1].startswith('--') else 'colorpencil_dessert.png'
+stages = sys.argv[sys.argv.index('--stages') + 1] if '--stages' in sys.argv else None
+
+p = ColorPencil(1920, 1080, seed=327)
+p.paper()
+rng = p.rng
+BROWN = '#8a6a5a'
+p.stage('paper')
+
+# ---- soft backdrop circle and a table band
+p.hatch(p.mask_ellipse(960, 540, 470, 430, feather=40), '#fbe7ad', pressure=0.35, angle=0.8, spacing=6, edge_fade=60)
+table = p.mask_poly([(0, 760), (1920, 740), (1920, 1080), (0, 1080)], feather=6) * np.clip((p.YY - 740) / 200, 0.4, 1)
+p.hatch(table, '#f2cdbf', pressure=0.4, angle=0.15, spacing=6, length=(60, 160), edge_fade=0)
+p.line(spline([(0, 762), (960, 752), (1920, 742)], 20), BROWN, 2.0, 0.35)
+p.stage('backdrop')
+
+# ---- plate
+PX, PY = 820, 780
+plate = p.mask_ellipse(PX, PY, 360, 92)
+p.hatch(plate, '#e3ebf1', pressure=0.5, angle=0.3)
+inner = p.mask_ellipse(PX, PY - 6, 285, 66)
+p.hatch(plate * (1 - inner), '#a9bccd', pressure=0.5, angle=2.4)
+ell = lambda cx, cy, rx, ry, n=80: [(cx + rx * np.cos(t), cy + ry * np.sin(t)) for t in np.linspace(0, 2 * np.pi, n, endpoint=False)]
+p.outline(ell(PX, PY, 360, 92), BROWN, 2.2, 0.55)
+p.outline(ell(PX, PY - 6, 285, 66), BROWN, 1.6, 0.3)
+
+# ---- strawberry shortcake slice (cut face with layers, cream top, crust side)
+TT, TB = (560, 610), (560, 680) # tip top / bottom
+BT, BB = (960, 520), (960, 720) # back edge of the cut face
+RT, RB = (1060, 548), (1060, 736) # far edge of the round side
+cut = [TT, BT, BB, TB]
+side = [BT, RT, RB, BB]
+top = [TT, (740, 520), (1000, 470), RT, BT]
+def lerp(a, b, t): return (a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t)
+def band(f0, f1):
+ return p.mask_poly([lerp(TT, TB, f0), lerp(BT, BB, f0), lerp(BT, BB, f1), lerp(TT, TB, f1)], feather=1.2)
+p.hatch(p.mask_poly(side), '#f7e6c4', pressure=0.5, angle=1.4)
+p.shade(p.mask_poly(side), '#e2b98a', pressure=0.35, angle=2.6)
+for (f0, f1, col, pr) in [(0.0, 0.12, '#fbe9d2', 0.5), (0.12, 0.42, '#f3c36b', 0.7), (0.42, 0.58, '#fbe4dc', 0.5),
+ (0.58, 0.9, '#f3c36b', 0.7), (0.9, 1.0, '#dfa45a', 0.75)]:
+ p.hatch(band(f0, f1), col, pressure=pr, angle=0.35)
+for k in range(5): # strawberry slices in the cream layer
+ t = 0.12 + k * 0.19
+ cx, cy = lerp(lerp(TT, TB, 0.5), lerp(BT, BB, 0.5), t)
+ r = 18 + 22 * t
+ m = p.mask_ellipse(cx, cy, r, r * 0.55)
+ p.hatch(m, '#ef6f7f', pressure=0.75, angle=0.8, spacing=4)
+ p.hatch(p.mask_ellipse(cx, cy, r * 0.5, r * 0.28), '#ffd0d6', pressure=0.4, angle=2.0)
+p.shade(band(0.12, 1.0) * np.clip((p.XX - 560) / 400, 0, 1) ** 2, '#d9a35f', pressure=0.35, angle=2.4)
+p.hatch(p.mask_poly(top), '#f7e4cb', pressure=0.62, angle=0.6)
+p.shade(p.mask_poly(top) * np.clip((p.YY - 470) / 140, 0, 1), '#d8c6d6', pressure=0.45, angle=2.2)
+for poly in (cut, side, top):
+ p.outline(poly, BROWN, 2.0, 0.55)
+for k in range(4): # layer lines on the cut face
+ f = [0.12, 0.42, 0.58, 0.9][k]
+ p.line([lerp(TT, TB, f), lerp(BT, BB, f)], BROWN, 1.3, 0.25)
+# cream dollops and a whole strawberry on top
+for (cx, cy, r) in [(700, 520, 34), (820, 500, 38), (930, 482, 32)]:
+ pts = blob_pts(cx, cy, r, r * 0.8, rough=0.12, n=60, seed=int(cx))
+ p.hatch(p.mask_poly(pts), '#f8e2c4', pressure=0.65, angle=0.6)
+ p.shade(p.mask_poly(pts) * np.clip((p.YY - cy + r * 0.3) / r, 0, 1), '#c9b3d1', pressure=0.7, angle=2.2)
+ p.outline(pts, BROWN, 1.6, 0.4)
+ p.line(spline([(cx - r * 0.5, cy - r * 0.1), (cx, cy - r * 0.45), (cx + r * 0.4, cy - r * 0.1)], 8), BROWN, 1.2, 0.25)
+SB = [(826, 470), (786, 430), (790, 395), (826, 385), (862, 395), (866, 430)]
+sb = spline(SB + [SB[0]], 10)
+p.hatch(p.mask_poly(sb), '#ea4f62', pressure=0.8, angle=0.9, spacing=4)
+p.shade(p.mask_poly(sb) * np.clip((p.XX - 820) / 50, 0, 1), '#b8324a', pressure=0.5, angle=2.3)
+p.outline(sb, '#8a3a44', 1.8, 0.6)
+for (lx, ly, a) in [(812, 384, -2.4), (826, 380, -1.57), (840, 384, -0.7)]:
+ leaf = [(lx, ly), (lx + np.cos(a - 0.4) * 22, ly + np.sin(a - 0.4) * 22), (lx + np.cos(a) * 34, ly + np.sin(a) * 34), (lx + np.cos(a + 0.4) * 22, ly + np.sin(a + 0.4) * 22)]
+ p.hatch(p.mask_poly(leaf), '#6fae5a', pressure=0.75, angle=1.2, spacing=4)
+ p.outline(leaf, '#4e7a3c', 1.4, 0.5)
+p.dots(p.mask_poly(sb), '#fbe3a0', n=16, r=(1.6, 2.6))
+p.stage('cake')
+
+# ---- teacup and saucer with steam
+CX, CY = 1390, 640
+p.hatch(p.mask_ellipse(CX, 760, 200, 52), '#dfe8ef', pressure=0.55, angle=0.3)
+p.shade(p.mask_ellipse(CX, 760, 200, 52) * (1 - p.mask_ellipse(CX, 752, 150, 34)), '#a9bccd', pressure=0.5, angle=2.3)
+p.outline(ell(CX, 760, 200, 52), BROWN, 2.0, 0.5)
+body = spline([(CX - 130, CY), (CX - 118, CY + 70), (CX - 60, CY + 120), (CX + 60, CY + 120), (CX + 118, CY + 70), (CX + 130, CY)], 12).tolist()
+cup = body + [(CX + 130, CY), (CX - 130, CY)]
+p.hatch(p.mask_poly(cup), '#f1eef0', pressure=0.5, angle=0.5)
+p.shade(p.mask_poly(cup) * np.clip((p.XX - CX + 20) / 120, 0, 1), '#aebfd0', pressure=0.55, angle=2.3)
+p.hatch(p.mask_poly(cup) * (np.abs(p.YY - (CY + 55)) < 14), '#f2a7b4', pressure=0.65, angle=0.2) # pink band
+for fx in (CX - 70, CX - 10, CX + 50): # little flowers
+ for a in np.linspace(0, 2 * np.pi, 5, endpoint=False):
+ p.hatch(p.mask_ellipse(fx + np.cos(a) * 7, CY + 95 + np.sin(a) * 7, 5.5, 5.5), '#f08aa0', pressure=0.8, spacing=3, length=(6, 12), edge_fade=0)
+handle = [(CX + 118, CY + 22), (CX + 178, CY + 18), (CX + 190, CY + 60), (CX + 150, CY + 92), (CX + 108, CY + 80)]
+p.line(spline(handle, 10), BROWN, 9, 0.35)
+p.line(spline(handle, 10), BROWN, 2.0, 0.55)
+p.outline(cup, BROWN, 2.0, 0.55)
+p.hatch(p.mask_ellipse(CX, CY, 124, 30), '#c9824a', pressure=0.65, angle=0.3)
+p.hatch(p.mask_ellipse(CX - 30, CY - 6, 50, 10), '#e9b27a', pressure=0.4, angle=0.3)
+p.outline(ell(CX, CY, 130, 34), BROWN, 2.0, 0.55)
+for k in range(3):
+ x0 = CX - 50 + k * 50
+ p.line(spline([(x0, CY - 50), (x0 - 18, CY - 100), (x0 + 12, CY - 150), (x0 - 10, CY - 200)], 10), '#b7aaa4', 2.2, 0.4)
+p.stage('tea')
+
+# ---- a few strawberries on the table, sparkles
+def berry(x, y, s, rot):
+ pts = spline([(x, y + 40 * s), (x - 34 * s, y), (x - 26 * s, y - 30 * s), (x, y - 36 * s), (x + 26 * s, y - 30 * s), (x + 34 * s, y), (x, y + 40 * s)], 10)
+ c, si = np.cos(rot), np.sin(rot)
+ pts = np.stack([x + (pts[:, 0] - x) * c - (pts[:, 1] - y) * si, y + (pts[:, 0] - x) * si + (pts[:, 1] - y) * c], 1)
+ m = p.mask_poly(pts)
+ p.hatch(m, '#ea4f62', pressure=0.8, angle=0.9, spacing=4)
+ p.shade(m * np.clip((p.XX - x) / (30 * s), 0, 1), '#b8324a', pressure=0.5, angle=2.3)
+ p.outline(pts, '#8a3a44', 1.6, 0.55)
+ p.dots(m, '#fbe3a0', n=12, r=(1.4, 2.4))
+ cx, cy = x + np.sin(-rot) * 36 * s, y - np.cos(rot) * 36 * s
+ for a in (-2.5, -1.57, -0.64):
+ leaf = [(cx, cy), (cx + np.cos(a + rot - 0.4) * 16 * s, cy + np.sin(a + rot - 0.4) * 16 * s), (cx + np.cos(a + rot) * 26 * s, cy + np.sin(a + rot) * 26 * s), (cx + np.cos(a + rot + 0.4) * 16 * s, cy + np.sin(a + rot + 0.4) * 16 * s)]
+ p.hatch(p.mask_poly(leaf), '#6fae5a', pressure=0.7, angle=1.2, spacing=4)
+ p.sparkle(x - 12 * s, y - 14 * s, 7 * s)
+berry(1180, 900, 1.0, 0.4)
+berry(1290, 950, 0.85, -0.5)
+berry(470, 920, 0.95, -0.2)
+for (x, y, r) in [(814, 404, 8), (1335, 612, 7), (560, 700, 6), (1160, 460, 10), (640, 380, 7), (1520, 470, 9)]:
+ p.sparkle(x, y, r)
+
+p.save(out, stages_dir=stages)
+print('saved', out)
diff --git a/examples/drawing_anime_summer.gif b/examples/drawing_anime_summer.gif
new file mode 100644
index 0000000..05b9738
Binary files /dev/null and b/examples/drawing_anime_summer.gif differ
diff --git a/examples/drawing_colorpencil_dessert.gif b/examples/drawing_colorpencil_dessert.gif
new file mode 100644
index 0000000..fa9bfe2
Binary files /dev/null and b/examples/drawing_colorpencil_dessert.gif differ
diff --git a/examples/drawing_editorial_ideas.gif b/examples/drawing_editorial_ideas.gif
new file mode 100644
index 0000000..54baf2c
Binary files /dev/null and b/examples/drawing_editorial_ideas.gif differ
diff --git a/examples/editorial_ideas.jpg b/examples/editorial_ideas.jpg
new file mode 100644
index 0000000..df8111d
Binary files /dev/null and b/examples/editorial_ideas.jpg differ
diff --git a/examples/editorial_ideas.py b/examples/editorial_ideas.py
new file mode 100644
index 0000000..0011be7
--- /dev/null
+++ b/examples/editorial_ideas.py
@@ -0,0 +1,95 @@
+"""Example (editorial illustration, risograph feel): Growing Ideas / 灵感生长 — a figure waters a plant
+that grows into a light bulb. Limited spot palette, overprinting inks, halftone shading,
+mis-registration, wobbly hand-drawn line. Pure code, no image model.
+python3 editorial_ideas.py [out.png] [--stages DIR]
+"""
+import os, sys
+sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'lib'))
+import numpy as np
+from editorial import Editorial
+from core import spline
+
+out = sys.argv[1] if len(sys.argv) > 1 and not sys.argv[1].startswith('--') else 'editorial_ideas.png'
+stages = sys.argv[sys.argv.index('--stages') + 1] if '--stages' in sys.argv else None
+
+YEL, COR, TEA, PNK, NAV = '#ffc857', '#ff6b57', '#1b9aaa', '#f7a8b8', '#26335c'
+e = Editorial(1920, 1080, seed=2027)
+rng = e.rng
+
+# ---- big simple backdrop shapes (overprinting where they meet)
+BX, BY = 1360, 360
+e.fill(e.circle(BX, BY - 20, 330), YEL)
+e.halftone(e.circle(BX, BY - 20, 330) * np.clip((e.XX - BX + (e.YY - BY)) / 420, 0, 1), COR, cell=12, angle=0.6, amount=0.55)
+arch = np.maximum(e.rrect(170, 250, 640, 1000, 30) * (e.YY > 480), e.circle(405, 480, 235))
+e.fill(arch, PNK)
+e.fill(e.poly([(0, 905), (1920, 890), (1920, 1080), (0, 1080)]), TEA)
+e.halftone(e.poly([(0, 905), (1920, 890), (1920, 1080), (0, 1080)]) * np.clip((e.YY - 900) / 180, 0, 1), NAV, cell=10, angle=0.3, amount=0.5)
+e.stage('shapes')
+
+# ---- the plant that grows into a light bulb
+PX = 1360
+pot = [(PX - 110, 760), (PX + 110, 760), (PX + 80, 920), (PX - 80, 920)]
+e.fill(e.poly(pot), COR)
+e.halftone(e.poly(pot) * np.clip((e.XX - PX) / 110, 0, 1), NAV, cell=9, angle=0.8, amount=0.55)
+e.fill(e.rrect(PX - 125, 738, PX + 125, 778, 8), COR)
+e.line(pot, NAV, 4, closed=True); e.line([(PX - 125, 740), (PX + 125, 740), (PX + 125, 778), (PX - 125, 778), (PX - 125, 740)], NAV, 4)
+stem = spline([(PX, 740), (PX - 30, 640), (PX + 20, 560), (PX, 470)], 16)
+e.line(stem, NAV, 6)
+for (t, side, L) in [(0.25, -1, 150), (0.45, 1, 170), (0.65, -1, 130)]:
+ q = stem[int(t * (len(stem) - 1))]
+ tip = (q[0] + side * L, q[1] - L * 0.45)
+ leaf = spline([tuple(q), (q[0] + side * L * 0.45, q[1] - L * 0.62), tip, (q[0] + side * L * 0.55, q[1] - L * 0.05), tuple(q)], 10)
+ e.fill(e.poly(leaf), TEA)
+ e.line(leaf, NAV, 3.5, closed=True)
+ e.line([tuple(q), (q[0] + side * L * 0.75, q[1] - L * 0.35)], NAV, 2.5)
+# bulb: glass left as paper, glowing rays, coral filament, navy screw base
+glass = spline([(PX - 40, 470), (PX - 60, 420), (PX - 120, 350), (PX - 110, 250), (PX, 190), (PX + 110, 250), (PX + 120, 350), (PX + 60, 420), (PX + 40, 470)], 12)
+e.fill(e.poly(glass), '#fff6dd', density=0.98)
+e.halftone(e.poly(glass) * np.clip((e.XX - PX + 60) / 180, 0, 1), YEL, cell=8, angle=0.4, amount=0.7)
+e.line(glass, NAV, 5)
+e.line(spline([(PX - 34, 440), (PX - 24, 360), (PX - 8, 330), (PX + 8, 360), (PX + 24, 330), (PX + 34, 440)], 10), COR, 4)
+for k in range(3):
+ e.fill(e.rrect(PX - 44 + k * 3, 470 + k * 16, PX + 44 - k * 3, 482 + k * 16, 5), NAV)
+for a in np.linspace(-2.9, -0.25, 7):
+ r0, r1 = 160, 215
+ e.line([(PX + np.cos(a) * r0, 320 + np.sin(a) * r0), (PX + np.cos(a) * r1, 320 + np.sin(a) * r1)], NAV, 5, wobble=0.5)
+e.stage('plant')
+
+# ---- the gardener: stylised figure with a watering can
+HX, HY = 520, 640 # hips
+for (fx, fy) in [(470, 890), (600, 895)]: # legs + shoes
+ e.line(spline([(HX + (fx - HX) * 0.2, HY), ((HX + fx) / 2, (HY + fy) / 2 + 10), (fx, fy)], 10), NAV, 34, wobble=0.6)
+ e.fill(e.ellipse(fx + 22, fy + 8, 40, 16), COR)
+torso = spline([(HX - 70, HY), (HX - 80, 540), (HX - 40, 470), (HX + 50, 462), (HX + 90, 520), (HX + 80, HY), (HX - 70, HY)], 12)
+e.fill(e.poly(torso), COR)
+e.halftone(e.poly(torso) * np.clip((HX - e.XX) / 90, 0, 1), NAV, cell=8, angle=0.8, amount=0.5)
+e.line(torso, NAV, 4)
+e.fill(e.rrect(HX - 14, 420, HX + 16, 470, 10), '#f2c4a8') # neck
+e.fill(e.circle(HX + 10, 385, 52), '#f2c4a8') # head
+hair = spline([(HX - 44, 420), (HX - 50, 360), (HX - 10, 322), (HX + 40, 326), (HX + 62, 360), (HX + 40, 352), (HX - 10, 360), (HX - 30, 420)], 10)
+e.fill(e.poly(hair), NAV)
+e.line([(HX + 36, 392), (HX + 46, 396)], NAV, 3, wobble=0.2) # closed eye
+arm = spline([(HX + 60, 480), (HX + 150, 540), (HX + 250, 560), (HX + 300, 548)], 12)
+e.line(arm, COR, 30, wobble=0.5); e.line(arm, NAV, 3, wobble=0.8)
+e.fill(e.circle(HX + 305, 548, 20), '#f2c4a8') # hand
+CX, CY = HX + 380, 560 # watering can
+e.fill(e.rrect(CX - 70, CY - 55, CX + 70, CY + 55, 18), TEA)
+e.halftone(e.rrect(CX - 70, CY - 55, CX + 70, CY + 55, 18) * np.clip((e.YY - CY) / 60, 0, 1), NAV, cell=8, angle=0.5, amount=0.55)
+e.line([(CX - 70, CY - 37), (CX - 70, CY + 55), (CX + 70, CY + 55), (CX + 70, CY - 37), (CX - 52, CY - 55), (CX - 70, CY - 37)], NAV, 4)
+e.line(spline([(CX - 60, CY - 50), (CX - 10, CY - 110), (CX + 50, CY - 55)], 10), NAV, 6)
+spout = [(CX + 70, CY + 10), (CX + 190, CY - 40), (CX + 200, CY - 26), (CX + 70, CY + 34)]
+e.fill(e.poly(spout), TEA); e.line(spout, NAV, 3.5, closed=True)
+for k in range(9): # water drops arcing to the pot
+ t = k / 8
+ x = CX + 205 + t * 240; y = CY - 30 + t * t * 170 - 40 * np.sin(np.pi * t)
+ e.fill(e.ellipse(x, y, 7, 11), TEA)
+e.stage('figure')
+
+# ---- small marks: dotted path, sparkles, floating dots
+e.dashes([(700, 250), (900, 180), (1080, 210)], NAV, 3, dash=16, gap=12)
+e.stars([(1640, 170, 16), (1060, 150, 12), (1710, 560, 12), (250, 170, 14)])
+for (x, y, r, c) in [(1760, 360, 18, COR), (160, 780, 14, YEL), (980, 820, 10, PNK), (1620, 760, 12, YEL)]:
+ e.fill(e.circle(x, y, r), c)
+
+e.save(out, stages_dir=stages)
+print('saved', out)
diff --git a/lib/anime.py b/lib/anime.py
new file mode 100644
index 0000000..44362dd
--- /dev/null
+++ b/lib/anime.py
@@ -0,0 +1,178 @@
+"""anime — Japanese anime background art, cel-shaded (numpy + Pillow only).
+
+The look: saturated gradient skies, towering cumulus built from stacked puffs with *hard-edged*
+light/shadow (cel shading), bloom and lens flare around the sun, crisp flat-shaded ground,
+perspective roads, power lines, and characters with clean dark line art.
+
+ from anime import Anime, hexc
+ a = Anime(1920, 1080, seed=1)
+ a.sky('#1f63c6', '#5aa9ec', '#d6f0ff', horizon=640)
+ a.cumulus(1300, 520, 700, 380)
+ a.sun_flare(1500, 180)
+ a.save('out.png')
+"""
+import numpy as np
+from PIL import Image, ImageDraw
+from core import blur, fbm1d, spline, noise2d, smoothstep, polygon_mask, blob_pts
+
+
+def hexc(h):
+ h = h.lstrip('#')
+ return np.array([int(h[i:i + 2], 16) for i in (0, 2, 4)], np.float32) / 255
+
+
+def _c(c):
+ return hexc(c) if isinstance(c, str) else np.asarray(c, np.float32)
+
+
+class Anime:
+ def __init__(self, W=1920, H=1080, seed=0):
+ self.W, self.H = W, H
+ self.rng = np.random.default_rng(seed)
+ self.YY, self.XX = np.mgrid[0:H, 0:W].astype(np.float32)
+ self.img = np.ones((H, W, 3), np.float32)
+ self.stages = []
+
+ def _seed(self):
+ return int(self.rng.integers(1 << 30))
+
+ # ------------------------------------------------------------ compositing
+ def fill(self, mask, colour, alpha=1.0):
+ a = np.clip(mask * alpha, 0, 1)[..., None]
+ self.img = self.img * (1 - a) + _c(colour) * a
+
+ def screen(self, mask, colour, alpha=1.0):
+ a = np.clip(mask * alpha, 0, 1)[..., None]
+ c = _c(colour)
+ self.img = 1 - (1 - self.img) * (1 - c * a)
+
+ def gradient(self, mask, c0, c1, y0, y1, alpha=1.0):
+ t = np.clip((self.YY - y0) / max(1, y1 - y0), 0, 1)[..., None]
+ col = _c(c0) * (1 - t) + _c(c1) * t
+ a = np.clip(mask * alpha, 0, 1)[..., None]
+ self.img = self.img * (1 - a) + col * a
+
+ def poly(self, pts, ss=2):
+ return polygon_mask(self.H, self.W, pts, ss=ss)
+
+ def circle(self, cx, cy, r):
+ return np.clip(r - np.hypot(self.XX - cx, self.YY - cy) + 0.5, 0, 1)
+
+ def lines(self, pts, width, colour, alpha=1.0, closed=False):
+ """clean anti-aliased line art"""
+ im = Image.new('L', (self.W * 2, self.H * 2), 0)
+ P = [(float(x) * 2, float(y) * 2) for x, y in pts]
+ if closed: P = P + P[:1]
+ ImageDraw.Draw(im).line(P, fill=255, width=max(1, int(round(width * 2))), joint='curve')
+ m = np.asarray(im.resize((self.W, self.H), Image.LANCZOS), np.float32) / 255
+ self.fill(m, colour, alpha)
+
+ # ------------------------------------------------------------ sky
+ def sky(self, top='#1f5fc4', mid='#58a6ec', horizon_col='#d9f1ff', horizon=640):
+ t = np.clip(self.YY / horizon, 0, 1)
+ c0, c1, c2 = _c(top), _c(mid), _c(horizon_col)
+ k = smoothstep(0, 0.55, t)[..., None]; k2 = smoothstep(0.5, 1.0, t)[..., None]
+ self.img = c0 * (1 - k) + c1 * k
+ self.img = self.img * (1 - k2) + c2 * k2
+
+ def cumulus(self, cx, base_y, w, h, light=(-0.6, -0.8), lit='#ffffff', mid='#edf3fc', shadow='#aec1e6',
+ deep='#90a8d8', puffs=60, seed=None):
+ """towering cumulus: many lumpy puffs stacked back-to-front. Each puff is cel-shaded with a
+ crescent shadow = puff minus a copy of itself shifted toward the light (hard edge), so the
+ shadows follow every bump like hand-painted anime clouds."""
+ r = np.random.default_rng(seed if seed is not None else self._seed())
+ lx, ly = light; ln = np.hypot(lx, ly); lx, ly = lx / ln, ly / ln
+ items = []
+ for i in range(puffs):
+ x = cx + r.normal(0, w * 0.24)
+ prof = np.exp(-((x - cx) / (w * 0.4)) ** 2)
+ y = base_y - h * prof * r.uniform(0.1, 1.0) ** 0.8
+ rad = (w * 0.035 + w * 0.075 * prof) * r.uniform(0.6, 1.3)
+ items.append((y, x, rad))
+ items.sort() # far/top puffs first, lower/front puffs overlap them
+ for (y, x, rad) in items:
+ pts = blob_pts(x, y, rad, rad * r.uniform(0.85, 1.0), rough=0.06, n=48, seed=int(r.integers(1 << 30)))
+ m = polygon_mask(self.H, self.W, pts, ss=2) * (self.YY < base_y + 2)
+ litm = self.circle(x + lx * rad * 0.42, y + ly * rad * 0.42, rad * 0.98)
+ core = self.circle(x - lx * rad * 0.35, y - ly * rad * 0.35, rad * 0.55)
+ self.fill(m, shadow) # whole puff in shadow tone...
+ self.fill(m * np.clip(litm, 0, 1), lit) # ...then the lit part as a hard-edged disc toward the light
+ self.fill(m * np.clip(litm, 0, 1) * np.clip(self.circle(x + lx * rad * 0.62, y + ly * rad * 0.62, rad * 0.9) * -1 + 1, 0, 1), mid, 0.8)
+ self.fill(m * core * (1 - np.clip(litm, 0, 1)), deep, 0.45)
+ base = self.poly([(cx - w * 0.55, base_y - h * 0.06), (cx + w * 0.55, base_y - h * 0.06), (cx + w * 0.6, base_y + 4), (cx - w * 0.6, base_y + 4)])
+ self.fill(base * (self.YY > base_y - h * 0.09), shadow, 0.9)
+
+ def sun_flare(self, x, y, strength=1.0, rays=9, flare=True):
+ d = np.hypot(self.XX - x, self.YY - y)
+ self.screen(np.exp(-(d / 60) ** 2), '#ffffff', 1.0 * strength)
+ self.screen(np.exp(-(d / 260) ** 2), '#fff6d8', 0.55 * strength)
+ self.screen(np.exp(-(d / 700) ** 2), '#fff1c8', 0.25 * strength)
+ ang = np.arctan2(self.YY - y, self.XX - x)
+ for k in range(rays):
+ a0 = self.rng.uniform(-np.pi, np.pi); wdt = self.rng.uniform(0.015, 0.04)
+ beam = np.exp(-((np.angle(np.exp(1j * (ang - a0)))) / wdt) ** 2) * np.exp(-d / 900)
+ self.screen(beam, '#fffbe8', 0.22 * strength)
+ if flare:
+ cx, cy = self.W / 2, self.H / 2
+ for t, r, col, al in [(0.45, 36, '#b7f0ff', 0.25), (0.7, 18, '#ffd0f0', 0.3), (1.05, 60, '#c9ffd9', 0.15), (1.35, 26, '#fff2b0', 0.25)]:
+ fx, fy = x + (cx - x) * t * 2, y + (cy - y) * t * 2
+ ring = np.clip(r - np.hypot(self.XX - fx, self.YY - fy), 0, 1) * 1.0
+ self.screen(blur(ring, 1.5), col, al * strength)
+
+ def sparkles(self, n=40, box=None, size=(1.5, 3.5), colour='#ffffff'):
+ x0, y0, x1, y1 = box or (0, 0, self.W, self.H)
+ for _ in range(n):
+ x, y = self.rng.uniform(x0, x1), self.rng.uniform(y0, y1)
+ r = self.rng.uniform(*size)
+ self.screen(np.exp(-((self.XX - x) ** 2 + (self.YY - y) ** 2) / (2 * r * r)), colour, 0.9)
+
+ # ------------------------------------------------------------ ground helpers
+ def field(self, top_y, colours=('#7cc95a', '#4f9f45'), bands=6, vanish=(960, 600)):
+ """cel-shaded field: alternating flat bands receding toward the horizon"""
+ vx, vy = vanish
+ for i in range(bands):
+ t0, t1 = i / bands, (i + 1) / bands
+ y_a = top_y + (self.H - top_y) * t0 ** 1.8
+ y_b = top_y + (self.H - top_y) * t1 ** 1.8
+ m = ((self.YY >= y_a) & (self.YY < y_b)).astype(np.float32)
+ self.fill(m, colours[i % 2])
+
+ def pole(self, x, base_y, height, width=6, colour='#3a3f4f'):
+ self.lines([(x, base_y), (x, base_y - height)], width, colour)
+ for k, cw in enumerate((height * 0.22, height * 0.16)):
+ yy = base_y - height + 8 + k * height * 0.07
+ self.lines([(x - cw / 2, yy), (x + cw / 2, yy)], max(2, width * 0.6), colour)
+ return (x, base_y - height + 8)
+
+ def wire(self, p0, p1, sag=30, width=1.6, colour='#2c3140'):
+ t = np.linspace(0, 1, 40)
+ xs = p0[0] + (p1[0] - p0[0]) * t
+ ys = p0[1] + (p1[1] - p0[1]) * t + sag * 4 * t * (1 - t)
+ self.lines(list(zip(xs, ys)), width, colour)
+
+ # ------------------------------------------------------------ finishing
+ def bloom(self, threshold=0.82, radius=18, amount=0.35):
+ lum = self.img.mean(2)
+ bright = np.clip((lum - threshold) / (1 - threshold), 0, 1)
+ b = np.stack([blur(self.img[..., k] * bright, radius) for k in range(3)], 2)
+ self.img = 1 - (1 - self.img) * (1 - b * amount)
+
+ def vignette(self, amount=0.18):
+ d = np.hypot((self.XX - self.W / 2) / (self.W / 2), (self.YY - self.H / 2) / (self.H / 2))
+ self.img *= (1 - amount * np.clip(d - 0.55, 0, 1))[..., None]
+
+ def composite(self):
+ return Image.fromarray((np.clip(self.img, 0, 1) * 255).astype(np.uint8))
+
+ def stage(self, name):
+ self.stages.append((name, self.composite()))
+
+ def save(self, path, stages_dir=None):
+ img = self.composite(); img.save(path)
+ if stages_dir:
+ import os
+ os.makedirs(stages_dir, exist_ok=True)
+ for i, (name, im) in enumerate(self.stages):
+ im.save(f"{stages_dir}/{i:02d}_{name}.png")
+ img.save(f"{stages_dir}/{len(self.stages):02d}_final.png")
+ return img
diff --git a/lib/colorpencil.py b/lib/colorpencil.py
new file mode 100644
index 0000000..fc27201
--- /dev/null
+++ b/lib/colorpencil.py
@@ -0,0 +1,161 @@
+"""colorpencil — soft Korean-style coloured-pencil illustration (numpy + Pillow only).
+
+Model: pencil wax only catches on the *peaks* of the paper tooth, so every pass is
+ deposit = strokes x pressure, thresholded against the paper tooth -> the classic grainy pencil look.
+Colour is built up in many light, short, directional strokes (hatching), layered and blended
+multiplicatively like wax; edges fade softly; outlines are a light brown pencil; white gel-pen
+highlights sparkle on top.
+
+ from colorpencil import ColorPencil, hexc
+ p = ColorPencil(1920, 1080, seed=1)
+ p.paper()
+ m = p.mask_poly(pts)
+ p.hatch(m, '#f4a7b9', pressure=0.6, angle=0.9) # base
+ p.hatch(m * shadow, '#d9728f', pressure=0.5, angle=2.2) # cross-hatched shading
+ p.outline(pts, '#8a6a5a')
+ p.sparkle(x, y, 8)
+ p.save('out.png')
+"""
+import numpy as np
+from PIL import Image, ImageDraw, ImageFont
+from core import blur, fbm1d, spline, noise2d, smoothstep, polygon_mask, blob_pts, cjk_font
+
+
+def hexc(h):
+ h = h.lstrip('#')
+ return np.array([int(h[i:i + 2], 16) for i in (0, 2, 4)], np.float32) / 255
+
+
+class ColorPencil:
+ def __init__(self, W=1920, H=1080, seed=0, paper_tone='#fcf8f0'):
+ self.W, self.H = W, H
+ self.rng = np.random.default_rng(seed)
+ self.YY, self.XX = np.mgrid[0:H, 0:W].astype(np.float32)
+ self.base = np.ones((H, W, 3), np.float32) * hexc(paper_tone)
+ self.img = self.base.copy()
+ self.tooth = np.full((H, W), 0.5, np.float32)
+ self.texts = []
+ self.stages = []
+
+ def _seed(self):
+ return int(self.rng.integers(1 << 30))
+
+ def noise(self, scale, octaves=3, seed=None):
+ return noise2d(self.H, self.W, scale, octaves, seed if seed is not None else self._seed())
+
+ # ------------------------------------------------------------ paper
+ def paper(self):
+ """drawing paper with a fine, slightly fibrous tooth"""
+ t = 0.6 * self.noise(1.3, 2) + 0.3 * self.noise(4, 2) + 0.1 * self.noise(40, 2)
+ self.tooth = (t - t.min()) / (t.max() - t.min())
+ self.base = self.base * (0.985 + 0.02 * self.noise(200, 3))[..., None] * (1 - 0.018 * (self.tooth - 0.5))[..., None]
+ self.img = self.base.copy()
+
+ # ------------------------------------------------------------ masks
+ def mask_poly(self, pts, feather=1.5):
+ return blur(polygon_mask(self.H, self.W, pts, ss=2), feather)
+
+ def mask_blob(self, cx, cy, rx, ry, rough=0.05, rot=0.0, feather=1.5):
+ return self.mask_poly(blob_pts(cx, cy, rx, ry, rough=rough, n=120, seed=self._seed(), rot=rot), feather)
+
+ def mask_ellipse(self, cx, cy, rx, ry, feather=1.2):
+ return blur(np.clip(1 - (((self.XX - cx) / rx) ** 2 + ((self.YY - cy) / ry) ** 2), 0, 1) * 50, feather).clip(0, 1)
+
+ # ------------------------------------------------------------ pencil
+ def _strokes(self, mask, angle, spacing, length, width, jitter):
+ """rasterise many short directional pencil strokes over the mask's bounding box -> coverage 0..1"""
+ ys, xs = np.nonzero(mask > 0.02)
+ if len(xs) == 0: return np.zeros((self.H, self.W), np.float32)
+ x0, x1, y0, y1 = xs.min() - 20, xs.max() + 20, ys.min() - 20, ys.max() + 20
+ acc = np.zeros((self.H, self.W), np.float32)
+ area = (x1 - x0) * (y1 - y0)
+ n = int(area / (spacing * np.mean(length)) * 3.5)
+ dx, dy = np.cos(angle), np.sin(angle)
+ for layer in range(3): # three passes so overlaps accumulate
+ im = Image.new('L', (self.W, self.H), 0); d = ImageDraw.Draw(im)
+ for _ in range(n // 3):
+ cx, cy = self.rng.uniform(x0, x1), self.rng.uniform(y0, y1)
+ L = self.rng.uniform(*length)
+ a = angle + self.rng.normal(0, jitter)
+ ex, ey = np.cos(a) * L / 2, np.sin(a) * L / 2
+ bend = self.rng.normal(0, L * 0.04)
+ pts = [(cx - ex, cy - ey), (cx - dy * bend, cy + dx * bend), (cx + ex, cy + ey)]
+ d.line(pts, fill=int(self.rng.uniform(150, 255)), width=int(width))
+ acc += np.asarray(im, np.float32) / 255
+ return 1 - np.exp(-blur(acc, 0.5) * 1.3) # overlapping strokes saturate
+
+ def hatch(self, mask, colour, pressure=0.55, angle=0.9, spacing=5, length=(30, 90), width=2, jitter=0.12, edge_fade=10):
+ """lay colour with short directional strokes; lighter toward the mask edge (soft pencil edge)"""
+ col = hexc(colour) if isinstance(colour, str) else colour
+ cov = self._strokes(mask, angle, spacing, length, width, jitter)
+ soft = mask * (blur((mask > 0.5).astype(np.float32), edge_fade) * 0.6 + 0.4) if edge_fade else mask
+ fill = smoothstep(0.15, 0.85, self.tooth + (pressure - 0.5) * 0.8) # wax catches on tooth peaks; pressing harder reaches the valleys
+ dep = np.clip(cov * soft * fill * (0.4 + pressure), 0, 0.95)
+ self.img *= (1 - dep[..., None] * (1 - col))
+
+ def shade(self, mask, colour, pressure=0.45, angle=2.2, **kw):
+ """cross-hatched shading layer (different angle)"""
+ self.hatch(mask, colour, pressure, angle, **kw)
+
+ def outline(self, pts, colour='#8a6a5a', width=2.2, pressure=0.7, closed=True):
+ """soft pencil line with slight wobble and varying pressure"""
+ col = hexc(colour) if isinstance(colour, str) else colour
+ P = np.asarray(pts, np.float32)
+ if closed: P = np.vstack([P, P[:1]])
+ n = len(P)
+ wob = fbm1d(n, max(4, n / 8), 3, self._seed())[:, None] * 1.2
+ seg = P + np.stack([wob[:, 0], np.roll(wob[:, 0], 7)], 1)
+ im = Image.new('L', (self.W, self.H), 0)
+ ImageDraw.Draw(im).line([tuple(map(float, q)) for q in seg], fill=255, width=int(round(width)), joint='curve')
+ line = blur(np.asarray(im, np.float32) / 255, 0.6)
+ press = 0.55 + 0.45 * self.noise(90, 2)
+ dep = np.clip(line * press * pressure * (0.75 + 0.6 * self.tooth) * 1.3, 0, 1)
+ self.img *= (1 - dep[..., None] * (1 - col))
+
+ def line(self, pts, colour='#8a6a5a', width=2.0, pressure=0.7):
+ self.outline(pts, colour, width, pressure, closed=False)
+
+ def sparkle(self, x, y, r=6, cross=True):
+ """white gel-pen highlight: dot, or a small four-point sparkle"""
+ a = np.clip((r * 0.45 - np.hypot(self.XX - x, self.YY - y)) / 0.8, 0, 1)
+ if cross:
+ a = np.maximum(a, np.clip((1.3 - np.abs(self.XX - x)) / 0.8, 0, 1) * (np.abs(self.YY - y) < r))
+ a = np.maximum(a, np.clip((1.3 - np.abs(self.YY - y)) / 0.8, 0, 1) * (np.abs(self.XX - x) < r))
+ self.img = self.img * (1 - a[..., None]) + np.array([1, 1, 1], np.float32) * a[..., None]
+
+ def dots(self, mask, colour, n=20, r=(2, 4)):
+ """seeds, sprinkles: small pencil dots inside a mask"""
+ col = hexc(colour) if isinstance(colour, str) else colour
+ ys, xs = np.nonzero(mask > 0.6)
+ if len(xs) == 0: return
+ for _ in range(n):
+ k = int(self.rng.integers(len(xs)))
+ rr = self.rng.uniform(*r)
+ a = np.clip((rr - np.hypot(self.XX - xs[k], self.YY - ys[k] * 1.0)) / 1.0, 0, 1) * (0.6 + 0.4 * self.tooth)
+ self.img *= (1 - a[..., None] * (1 - col))
+
+ # ------------------------------------------------------------ text / output
+ def text(self, s, x, y, size=48, colour='#8a6a5a'):
+ self.texts.append((s, x, y, size, colour))
+
+ def composite(self):
+ out = Image.fromarray((np.clip(self.img, 0, 1) * 255).astype(np.uint8))
+ fp = cjk_font()
+ if self.texts and fp:
+ dr = ImageDraw.Draw(out)
+ for s, x, y, size, colour in self.texts:
+ dr.text((x, y), s, font=ImageFont.truetype(fp, size, index=0), fill=tuple(int(v * 255) for v in hexc(colour)))
+ return out
+
+ def stage(self, name):
+ self.stages.append((name, self.composite()))
+
+ def save(self, path, stages_dir=None):
+ img = self.composite(); img.save(path)
+ if stages_dir:
+ import os
+ os.makedirs(stages_dir, exist_ok=True)
+ for i, (name, im) in enumerate(self.stages):
+ im.save(f"{stages_dir}/{i:02d}_{name}.png")
+ img.save(f"{stages_dir}/{len(self.stages):02d}_final.png")
+ return img
diff --git a/lib/editorial.py b/lib/editorial.py
new file mode 100644
index 0000000..145e97e
--- /dev/null
+++ b/lib/editorial.py
@@ -0,0 +1,143 @@
+"""editorial — modern editorial illustration with a risograph-print feel (numpy + Pillow only).
+
+The look: a limited palette of spot inks on cream stock; each ink is printed as its own layer with
+fine stochastic grain, slightly uneven density and a little mis-registration; inks are translucent,
+so overlaps *overprint* (multiply) into new colours; shading is done with halftone dots; a wobbly
+hand-drawn ink line holds it together; big simple shapes and stylised figures carry the idea.
+
+ from editorial import Editorial, hexc
+ e = Editorial(1920, 1080, seed=1)
+ e.fill(e.circle(1500, 300, 260), '#ffc857') # spot ink
+ e.halftone(e.circle(1500, 300, 260) * shadow, '#ff6b57')
+ e.line(pts, '#26335c', 4)
+ e.save('out.png')
+"""
+import numpy as np
+from PIL import Image, ImageDraw
+from core import blur, fbm1d, spline, noise2d, smoothstep, polygon_mask, blob_pts
+
+
+def hexc(h):
+ h = h.lstrip('#')
+ return np.array([int(h[i:i + 2], 16) for i in (0, 2, 4)], np.float32) / 255
+
+
+def _c(c):
+ return hexc(c) if isinstance(c, str) else np.asarray(c, np.float32)
+
+
+class Editorial:
+ def __init__(self, W=1920, H=1080, seed=0, stock='#f4eee3', misreg=3.0):
+ self.W, self.H = W, H
+ self.rng = np.random.default_rng(seed)
+ self.YY, self.XX = np.mgrid[0:H, 0:W].astype(np.float32)
+ paper = np.ones((H, W, 3), np.float32) * _c(stock)
+ paper *= (0.985 + 0.03 * noise2d(H, W, 180, 3, 1))[..., None]
+ specks = (self.rng.random((H, W)) > 0.9993).astype(np.float32)
+ paper *= (1 - 0.25 * blur(specks, 0.7))[..., None]
+ self.img = paper
+ self.misreg = misreg
+ self.offsets = {} # per-ink registration offset
+ self.stages = []
+
+ def _seed(self):
+ return int(self.rng.integers(1 << 30))
+
+ # ------------------------------------------------------------ shapes
+ def poly(self, pts):
+ return polygon_mask(self.H, self.W, pts, ss=2)
+
+ def circle(self, cx, cy, r):
+ return np.clip(r - np.hypot(self.XX - cx, self.YY - cy) + 0.5, 0, 1)
+
+ def ellipse(self, cx, cy, rx, ry):
+ return np.clip((1 - (((self.XX - cx) / rx) ** 2 + ((self.YY - cy) / ry) ** 2)) * min(rx, ry) / 2, 0, 1)
+
+ def rrect(self, x0, y0, x1, y1, r):
+ cx = np.clip(self.XX, x0 + r, x1 - r); cy = np.clip(self.YY, y0 + r, y1 - r)
+ return np.clip(r - np.hypot(self.XX - cx, self.YY - cy) + 0.5, 0, 1)
+
+ def blob(self, cx, cy, rx, ry, rough=0.08, rot=0.0):
+ return self.poly(blob_pts(cx, cy, rx, ry, rough=rough, n=90, seed=self._seed(), rot=rot))
+
+ # ------------------------------------------------------------ inks
+ def _shift(self, m, key):
+ if key not in self.offsets:
+ a = self.rng.uniform(0, 2 * np.pi)
+ self.offsets[key] = (int(round(np.cos(a) * self.misreg)), int(round(np.sin(a) * self.misreg)))
+ dx, dy = self.offsets[key]
+ out = np.zeros_like(m)
+ ys = slice(max(dy, 0), self.H + min(dy, 0)); ys0 = slice(max(-dy, 0), self.H + min(-dy, 0))
+ xs = slice(max(dx, 0), self.W + min(dx, 0)); xs0 = slice(max(-dx, 0), self.W + min(-dx, 0))
+ out[ys, xs] = m[ys0, xs0]
+ return out
+
+ def fill(self, mask, ink, density=0.92, grain=0.22):
+ """print one spot ink over `mask`: grainy, slightly uneven, mis-registered, overprinting (multiply)"""
+ col = _c(ink)
+ m = self._shift(mask, ink if isinstance(ink, str) else tuple(ink))
+ uneven = density - 0.08 * noise2d(self.H, self.W, 140, 3, self._seed())
+ g = self.rng.random((self.H, self.W)).astype(np.float32)
+ keep = smoothstep(-grain * 0.2, grain * 0.2, uneven - g) # most pixels take ink; a few specks drop out
+ dots = np.clip(blur(np.clip(m, 0, 1) * keep, 0.45), 0, 1)
+ self.img *= (1 - dots[..., None] * (1 - col))
+
+ def halftone(self, mask, ink, cell=11, angle=0.5, amount=0.6):
+ """halftone dots: dot size follows `mask * amount` (use for shading, gradients, texture)"""
+ c, s = np.cos(angle), np.sin(angle)
+ u = (self.XX * c + self.YY * s) / cell; v = (-self.XX * s + self.YY * c) / cell
+ du = u - np.round(u); dv = v - np.round(v)
+ d = np.hypot(du, dv)
+ rad = np.sqrt(np.clip(mask * amount, 0, 1)) * 0.62
+ dots = np.clip((rad - d) * cell / 1.2, 0, 1)
+ self.fill(dots, ink, density=0.95, grain=0.1)
+
+ def line(self, pts, ink='#26335c', width=4, wobble=1.6, closed=False, breaks=0.0):
+ """hand-drawn ink line: wobbly, slightly varying weight, optionally broken"""
+ P = np.asarray(pts, np.float32)
+ if closed: P = np.vstack([P, P[:1]])
+ n = len(P)
+ wob = np.stack([fbm1d(n, max(3, n / 6), 3, self._seed()), fbm1d(n, max(3, n / 6), 3, self._seed())], 1) * wobble
+ P = P + wob
+ im = Image.new('L', (self.W * 2, self.H * 2), 0); d = ImageDraw.Draw(im)
+ wv = width * (0.8 + 0.4 * (fbm1d(n, max(3, n / 4), 2, self._seed()) + 1) / 2)
+ for i in range(n - 1):
+ if breaks and self.rng.random() < breaks: continue
+ d.line([(P[i, 0] * 2, P[i, 1] * 2), (P[i + 1, 0] * 2, P[i + 1, 1] * 2)], fill=255, width=max(1, int(wv[i] * 2)))
+ r = wv[i]
+ d.ellipse((P[i + 1, 0] * 2 - r, P[i + 1, 1] * 2 - r, P[i + 1, 0] * 2 + r, P[i + 1, 1] * 2 + r), fill=255)
+ m = np.asarray(im.resize((self.W, self.H), Image.LANCZOS), np.float32) / 255
+ self.fill(m, ink, density=0.97, grain=0.12)
+
+ def dashes(self, pts, ink='#26335c', width=3, dash=14, gap=10):
+ P = spline(pts, 20) if len(pts) > 2 else np.asarray(pts, np.float32)
+ seg = np.linalg.norm(np.diff(P, axis=0), axis=1); s = np.concatenate([[0], np.cumsum(seg)])
+ t = 0.0
+ while t < s[-1]:
+ a, b = t, min(t + dash, s[-1])
+ q = [(np.interp(x, s, P[:, 0]), np.interp(x, s, P[:, 1])) for x in np.linspace(a, b, 4)]
+ self.line(q, ink, width, wobble=0.4)
+ t += dash + gap
+
+ def stars(self, items, ink='#26335c', width=3):
+ """little sparkle marks: (x, y, size)"""
+ for (x, y, r) in items:
+ self.line([(x - r, y), (x + r, y)], ink, width, wobble=0.3)
+ self.line([(x, y - r), (x, y + r)], ink, width, wobble=0.3)
+
+ # ------------------------------------------------------------ output
+ def composite(self):
+ return Image.fromarray((np.clip(self.img, 0, 1) * 255).astype(np.uint8))
+
+ def stage(self, name):
+ self.stages.append((name, self.composite()))
+
+ def save(self, path, stages_dir=None):
+ img = self.composite(); img.save(path)
+ if stages_dir:
+ import os
+ os.makedirs(stages_dir, exist_ok=True)
+ for i, (name, im) in enumerate(self.stages):
+ im.save(f"{stages_dir}/{i:02d}_{name}.png")
+ img.save(f"{stages_dir}/{len(self.stages):02d}_final.png")
+ return img