1+ from pyimagecuda import Image , save , Fill , Blend , Effect
2+
3+
4+ # Create large transparent canvas
5+ canvas = Image (1920 , 1080 )
6+ Fill .color (canvas , rgba = (0.0 , 0.0 , 0.0 , 0.0 ))
7+
8+ # Create smaller square with gradient
9+ square_size = 600
10+ square = Image (square_size , square_size )
11+ Fill .gradient (square ,
12+ rgba1 = (0.2 , 0.5 , 0.9 , 1.0 ), # Blue
13+ rgba2 = (0.9 , 0.3 , 0.6 , 1.0 ), # Pink
14+ direction = 'diagonal' )
15+
16+ # Round the corners
17+ Effect .rounded_corners (square , radius = 60.0 )
18+
19+ # Blend square onto canvas FIRST (centered)
20+ pos_x = (canvas .width - square_size ) // 2
21+ pos_y = (canvas .height - square_size ) // 2
22+ Blend .normal (canvas , square , pos_x = pos_x , pos_y = pos_y )
23+
24+ # Apply drop shadow with auto-allocated buffers
25+ canvas_with_shadow = Effect .drop_shadow (canvas ,
26+ offset_x = 20 ,
27+ offset_y = 20 ,
28+ blur = 40 ,
29+ color = (0.0 , 0.0 , 0.0 , 0.7 ))
30+
31+ save (canvas_with_shadow , "./effect_showcase.png" )
32+
33+
34+ # --- VERSION WITH CACHED BUFFERS FOR PERFORMANCE ---
35+
36+ # Pre-allocate reusable buffers
37+ dst_buffer = Image (canvas .width , canvas .height )
38+ shadow_buffer = Image (canvas .width , canvas .height )
39+ temp_buffer = Image (canvas .width , canvas .height )
40+
41+ # Reset canvas for second version
42+ Fill .color (canvas , rgba = (0.0 , 0.0 , 0.0 , 0.0 ))
43+ Blend .normal (canvas , square , pos_x = pos_x , pos_y = pos_y )
44+
45+ # Apply drop shadow with cached buffers (no allocation)
46+ Effect .drop_shadow (canvas ,
47+ offset_x = 20 ,
48+ offset_y = 20 ,
49+ blur = 40 ,
50+ color = (0.0 , 0.0 , 0.0 , 0.7 ),
51+ dst_buffer = dst_buffer ,
52+ shadow_buffer = shadow_buffer ,
53+ temp_buffer = temp_buffer )
54+
55+ save (dst_buffer , "./effect_showcase_cached.png" )
56+
57+
58+ # Cleanup
59+ canvas .free ()
60+ square .free ()
61+ canvas_with_shadow .free ()
62+ dst_buffer .free ()
63+ shadow_buffer .free ()
64+ temp_buffer .free ()
0 commit comments