BrushCue Example: Spherize Image¶
You can use this tool online at https://www.brushcue.com/tools/spherize-image
In [ ]:
!pip install brushcue
In [1]:
import io
from PIL import Image
import brushcue as bc
amount = 0.8
radius = 1.0
input_image = bc.Composition.monet_women_with_parasol()
input_bounds = input_image.bounds()
center = bc.Point2f.from_components(
(input_bounds.min_x() + (input_bounds.width() / 2.0)),
(input_bounds.min_y() + (input_bounds.height() / 2.0)),
)
effect_radius = (input_bounds.width().min(input_bounds.height()) / 2.0) * radius
graph = input_image.point_effect_shader(
"let n = clamp(distance_from_center / effect_radius, 0.0, 1.0);\nvar source_n = n;\nif (amount >= 0.0) {\n source_n = mix(n, asin(n) / 1.5707964, amount);\n} else {\n source_n = mix(n, sin(n * 1.5707964), -amount);\n}\nlet offset = position - effect_center_point;\nlet source = effect_center_point + offset / max(distance_from_center, 0.0001) * (source_n * effect_radius);\nreturn sample(clamp(source, image_bounds.xy + vec2<f32>(0.5), image_bounds.zw - vec2<f32>(0.5)));",
"",
center,
effect_radius,
bc.Dictionary.create().add("amount", amount).add("image_bounds", input_bounds),
bc.ColorRepresentation.srgb(),
).crop(input_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]: