BrushCue Example: Soft Pastel¶
You can use this tool online at https://www.brushcue.com/tools/soft-pastel
In [ ]:
!pip install brushcue
In [1]:
import io
from PIL import Image
import brushcue as bc
stroke_size = 18.0
softness = 0.85
paper_texture = 0.7
effect_strength = 1.0
input_image = bc.Composition.monet_women_with_parasol()
bounds = input_image.bounds()
reference_scale = (
bc.Float.max(1.0, bc.Float.min(bounds.width(), bounds.height())) / 1000.0
)
radius = bc.Float.max(0.5, (stroke_size * reference_scale))
graph = input_image.spacial_effect_shader(
"let lo = image_bounds.xy + vec2f(0.5);\nlet hi = image_bounds.zw - vec2f(0.5);\nlet p = clamp(position, lo, hi);\nlet source = sample(p);\nif (source.a <= 0.0) { return vec4f(0.0); }\nlet uv = (p - image_bounds.xy) / reference_scale;\n// Alpha-weighted neighborhoods ignore colors hidden in transparent pixels.\nvar total = vec3f(0.0);\nvar weights = 0.0;\nfor (var y = -3; y <= 3; y = y + 1) {\n for (var x = -3; x <= 3; x = x + 1) {\n let offset = vec2f(f32(x), f32(y));\n let c = sample(clamp(p + offset * radius / 3.0, lo, hi));\n let spatial = exp(-dot(offset, offset) * 0.22);\n let difference = luma(c.rgb) - luma(source.rgb);\n let range = exp(-difference * difference * 20.0);\n let weight = spatial * range * c.a;\n total += c.rgb * weight;\n weights += weight;\n }\n}\nlet local = total / max(weights, 0.0001);\nlet tooth = noise(uv * 0.8);\n// Elongated noise breaks pigment into broad, diagonal dry-media strokes.\nlet stroke_uv = vec2f(uv.x + uv.y * 0.45, uv.y - uv.x * 0.45);\nlet strokes = noise(stroke_uv / vec2f(2.5, 22.0));\nlet softened = mix(source.rgb, local, softness);\n// Gentle tonal steps retain pastel color regions without hard posterization.\nlet levels = 14.0;\nlet pigment = mix(softened, floor(clamp(softened, vec3f(0.0), vec3f(1.0)) * levels + vec3f(0.5)) / levels, 0.35);\nlet paper = vec3f(0.98, 0.96, 0.92) * (1.0 - paper_texture * tooth * 0.04);\nlet dry_gaps = paper_texture * smoothstep(0.5, 0.95, tooth) * (0.12 + strokes * 0.2);\nlet chalk = mix(pigment, paper, 0.08 + dry_gaps);\nlet shading = 1.0 - paper_texture * (1.0 - strokes) * 0.07;\nlet result = mix(source.rgb, chalk * shading, effect_strength);\nreturn vec4f(clamp(result, vec3f(0.0), vec3f(1.0)), source.a);",
"fn hash(p: vec2f) -> f32 {\n let q = fract(p * vec2f(0.1031, 0.11369));\n let n = dot(q, q.yx + vec2f(19.19));\n return fract((q.x + q.y + n) * (q.x + n));\n}\nfn noise(p: vec2f) -> f32 {\n let i = floor(p);\n let f = fract(p);\n let u = f * f * (vec2f(3.0) - 2.0 * f);\n return mix(mix(hash(i), hash(i + vec2f(1.0, 0.0)), u.x),\n mix(hash(i + vec2f(0.0, 1.0)), hash(i + vec2f(1.0)), u.x), u.y);\n}\nfn luma(c: vec3f) -> f32 {\n return dot(clamp(c, vec3f(0.0), vec3f(1.0)), vec3f(0.299, 0.587, 0.114));\n}",
0.0,
bc.Dictionary.create()
.add("image_bounds", bounds)
.add("reference_scale", reference_scale)
.add("radius", radius)
.add("softness", softness)
.add("paper_texture", paper_texture)
.add("effect_strength", effect_strength),
bc.ColorRepresentation.srgb(),
).crop(bounds)
ctx = bc.Context()
composition = graph.execute(ctx)
data_bytes = composition.to_image_bytes(ctx)
img = Image.open(io.BytesIO(data_bytes))
img.thumbnail((400, 400)) # Remove this line for full resolution
img
[wgpu] using backend Vulkan — adapter 'NVIDIA GeForce RTX 5070' (DiscreteGpu), driver 'NVIDIA'
Out[1]: