BrushCue Example: Velvet Stage¶
You can use this tool online at https://www.brushcue.com/tools/velvet-stage
In [ ]:
!pip install brushcue
In [1]:
import io
from PIL import Image
import brushcue as bc
@bc.brushcue_fn
def apply(input_image, strength):
graded = input_image.color_transformer_shader(
"let l = clamp(input.x, 0.0, 1.0);\nlet lit = smoothstep(0.28, 0.82, l);\nlet chroma = length(input.yz);\nlet skin = smoothstep(0.005, 0.025, input.y) * smoothstep(0.0, 0.035, input.z) * (1.0 - smoothstep(0.10, 0.18, chroma));\nlet ab = input.yz * (0.70 + 0.30 * skin) + vec2f(0.019, 0.007) * lit;\nlet tone = l - 0.035 * sin(l * 3.14159265) * (1.0 - lit);\nreturn vec4f(mix(input.xyz, vec3f(tone, ab), amount), input.w);",
"",
bc.ColorRepresentation.oklab_a(),
bc.ColorRepresentation.oklab_a(),
bc.Dictionary.create().add("amount", strength),
)
sampling = graded.color_transformer_shader(
"return vec4f(input.rgb * input.a, input.a);",
"",
bc.ColorRepresentation.srgb(),
bc.ColorRepresentation.srgb(),
bc.Dictionary.create(),
)
bounds = input_image.bounds()
reference_scale = (bounds.width().min(bounds.height()) / 1000.0).max(0.001)
radius = (1.8 * reference_scale).max(0.5)
return sampling.spacial_effect_shader(
"let lo = image_bounds.xy + vec2f(0.5);\nlet hi = image_bounds.zw - vec2f(0.5);\nlet center_sample = sample(clamp(position, lo, hi));\nlet center = vec4f(center_sample.rgb / max(center_sample.a, 0.00001), center_sample.a);\nif (center.a <= 0.0) { return center; }\nvar total = vec3f(0.0);\nvar weight = 0.0;\nfor (var y = -1; y <= 1; y = y + 1) {\n for (var x = -1; x <= 1; x = x + 1) {\n let offset = vec2f(f32(x), f32(y));\n let raw = sample(clamp(position + offset * radius, lo, hi));\n let c = vec4f(raw.rgb / max(raw.a, 0.00001), raw.a);\n let w = c.a * exp(-dot(c.rgb - center.rgb, c.rgb - center.rgb) * 65.0);\n total += c.rgb * w; weight += w;\n }\n}\nlet local = total / max(weight, 0.00001);\nlet uv = (position - image_bounds.xy) / max(reference_scale, 0.001);\nlet grain = hash(vec2i(floor(vec2f(uv.x + uv.y * 0.55, uv.y * 0.34))));\nlet mid = smoothstep(0.04, 0.20, dot(center.rgb, vec3f(0.2126, 0.7152, 0.0722)));\nlet result = mix(center.rgb, local, 0.45) + vec3f((grain - 0.5) * 0.025 * mid);\nreturn vec4f(mix(center.rgb, result, amount), center.a);",
"fn hash(p: vec2i) -> f32 {\n var n = u32(p.x) * 1597334677u + u32(p.y) * 3812015801u;\n n = (n ^ (n >> 16u)) * 2246822519u;\n n = (n ^ (n >> 13u)) * 3266489917u;\n return f32(n ^ (n >> 16u)) / 4294967295.0;\n}",
radius,
bc.Dictionary.create()
.add("amount", strength)
.add("radius", radius)
.add("image_bounds", bounds)
.add("reference_scale", reference_scale),
bc.ColorRepresentation.srgb(),
).crop(bounds)
strength = 1.0
input_image = bc.Composition.monet_women_with_parasol()
graph = apply(input_image, strength)
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]: