|
| 1 | +# Blend |
| 2 | + |
| 3 | +The `Blend` module provides GPU-accelerated layer compositing with multiple blend modes. |
| 4 | + |
| 5 | +All blend operations modify the `base` image in-place by compositing the `overlay` on top of it. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Positioning |
| 10 | + |
| 11 | +### Anchors |
| 12 | + |
| 13 | +Position overlays relative to common anchor points. By default, `anchor='top-left'`, which makes `offset_x` and `offset_y` behave as absolute pixel coordinates—just like standard graphics APIs. |
| 14 | + |
| 15 | +**Example:** |
| 16 | + |
| 17 | +<div style="display: flex; gap: 20px; align-items: start;"> |
| 18 | + <div style="flex: 1;"> |
| 19 | + |
| 20 | +```python |
| 21 | +from pyimagecuda import Image, Fill, Blend, save |
| 22 | + |
| 23 | +base = Image(800, 600) |
| 24 | +overlay = Image(200, 200) |
| 25 | + |
| 26 | +Fill.color(base, (0.2, 0.2, 0.3, 1.0)) |
| 27 | +Fill.color(overlay, (1.0, 0.0, 0.0, 1.0)) |
| 28 | + |
| 29 | +Blend.normal(base, overlay, anchor='center') |
| 30 | +save(base, 'output.png') |
| 31 | +``` |
| 32 | + |
| 33 | + </div> |
| 34 | + <div style="flex: 1;"> |
| 35 | + <img src="https://offerrall.github.io/pyimagecuda/images/blend_anchor.png" alt="Anchor positioning" style="width: 100%;"> |
| 36 | + </div> |
| 37 | +</div> |
| 38 | + |
| 39 | +**Available anchors:** `'top-left'`, `'top-center'`, `'top-right'`, `'center-left'`, `'center'`, `'center-right'`, `'bottom-left'`, `'bottom-center'`, `'bottom-right'` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +### Offsets |
| 44 | + |
| 45 | +Add pixel offsets from the anchor point for fine-tuning. |
| 46 | + |
| 47 | +**Example:** |
| 48 | + |
| 49 | +<div style="display: flex; gap: 20px; align-items: start;"> |
| 50 | + <div style="flex: 1;"> |
| 51 | + |
| 52 | +```python |
| 53 | +from pyimagecuda import Image, Fill, Blend, save |
| 54 | + |
| 55 | +base = Image(800, 600) |
| 56 | +overlay = Image(200, 200) |
| 57 | + |
| 58 | +Fill.color(base, (0.2, 0.2, 0.3, 1.0)) |
| 59 | +Fill.color(overlay, (1.0, 0.0, 0.0, 1.0)) |
| 60 | + |
| 61 | +Blend.normal(base, overlay, anchor='center', |
| 62 | + offset_x=50, offset_y=-30) |
| 63 | +save(base, 'output.png') |
| 64 | +``` |
| 65 | + |
| 66 | + </div> |
| 67 | + <div style="flex: 1;"> |
| 68 | + <img src="https://offerrall.github.io/pyimagecuda/images/blend_offset.png" alt="Offset positioning" style="width: 100%;"> |
| 69 | + </div> |
| 70 | +</div> |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Blend Modes |
| 75 | + |
| 76 | +### Normal |
| 77 | + |
| 78 | +Standard alpha blending (Porter-Duff over operation). |
| 79 | + |
| 80 | +**Example:** |
| 81 | + |
| 82 | +<div style="display: flex; gap: 20px; align-items: start;"> |
| 83 | + <div style="flex: 1;"> |
| 84 | + |
| 85 | +```python |
| 86 | +from pyimagecuda import Image, Fill, Blend, save |
| 87 | + |
| 88 | +base = Image(800, 600) |
| 89 | +overlay = Image(400, 300) |
| 90 | + |
| 91 | +c1 = (0.2, 0.2, 0.3, 1.0) |
| 92 | +c2 = (0.4, 0.3, 0.5, 1.0) |
| 93 | +c3 = (1.0, 0.5, 0.0, 0.8) |
| 94 | +c4 = (1.0, 0.0, 0.5, 0.8) |
| 95 | + |
| 96 | +Fill.gradient(base, c1, c2, 'radial') |
| 97 | +Fill.gradient(overlay, c3, c4, 'horizontal') |
| 98 | + |
| 99 | +Blend.normal(base, overlay, anchor='center') |
| 100 | +save(base, 'output.png') |
| 101 | +``` |
| 102 | + |
| 103 | + </div> |
| 104 | + <div style="flex: 1;"> |
| 105 | + <img src="https://offerrall.github.io/pyimagecuda/images/blend_normal.png" alt="Normal blend" style="width: 100%;"> |
| 106 | + </div> |
| 107 | +</div> |
| 108 | + |
| 109 | +**Use for:** Standard layer compositing, logos, overlays. |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +### Multiply |
| 114 | + |
| 115 | +Darkens the image by multiplying color values. |
| 116 | + |
| 117 | +**Example:** |
| 118 | + |
| 119 | +<div style="display: flex; gap: 20px; align-items: start;"> |
| 120 | + <div style="flex: 1;"> |
| 121 | + |
| 122 | +```python |
| 123 | +from pyimagecuda import Image, Fill, Blend, save |
| 124 | + |
| 125 | +base = Image(800, 600) |
| 126 | +overlay = Image(400, 300) |
| 127 | + |
| 128 | +c1 = (0.2, 0.2, 0.3, 1.0) |
| 129 | +c2 = (0.4, 0.3, 0.5, 1.0) |
| 130 | +c3 = (1.0, 0.5, 0.0, 0.8) |
| 131 | +c4 = (1.0, 0.0, 0.5, 0.8) |
| 132 | + |
| 133 | +Fill.gradient(base, c1, c2, 'radial') |
| 134 | +Fill.gradient(overlay, c3, c4, 'horizontal') |
| 135 | + |
| 136 | +Blend.multiply(base, overlay, anchor='center') |
| 137 | +save(base, 'output.png') |
| 138 | +``` |
| 139 | + |
| 140 | + </div> |
| 141 | + <div style="flex: 1;"> |
| 142 | + <img src="https://offerrall.github.io/pyimagecuda/images/blend_multiply.png" alt="Multiply blend" style="width: 100%;"> |
| 143 | + </div> |
| 144 | +</div> |
| 145 | + |
| 146 | +**Use for:** Shadows, darkening effects, texture overlays. |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +### Screen |
| 151 | + |
| 152 | +Lightens the image (inverse of multiply). |
| 153 | + |
| 154 | +**Example:** |
| 155 | + |
| 156 | +<div style="display: flex; gap: 20px; align-items: start;"> |
| 157 | + <div style="flex: 1;"> |
| 158 | + |
| 159 | +```python |
| 160 | +from pyimagecuda import Image, Fill, Blend, save |
| 161 | + |
| 162 | +base = Image(800, 600) |
| 163 | +overlay = Image(400, 300) |
| 164 | + |
| 165 | +c1 = (0.2, 0.2, 0.3, 1.0) |
| 166 | +c2 = (0.4, 0.3, 0.5, 1.0) |
| 167 | +c3 = (1.0, 0.5, 0.0, 0.8) |
| 168 | +c4 = (1.0, 0.0, 0.5, 0.8) |
| 169 | + |
| 170 | +Fill.gradient(base, c1, c2, 'radial') |
| 171 | +Fill.gradient(overlay, c3, c4, 'horizontal') |
| 172 | + |
| 173 | +Blend.screen(base, overlay, anchor='center') |
| 174 | +save(base, 'output.png') |
| 175 | +``` |
| 176 | + |
| 177 | + </div> |
| 178 | + <div style="flex: 1;"> |
| 179 | + <img src="https://offerrall.github.io/pyimagecuda/images/blend_screen.png" alt="Screen blend" style="width: 100%;"> |
| 180 | + </div> |
| 181 | +</div> |
| 182 | + |
| 183 | +**Use for:** Highlights, light effects, glows. |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +### Add |
| 188 | + |
| 189 | +Additive blending (colors add up, clamped to 1.0). |
| 190 | + |
| 191 | +**Example:** |
| 192 | + |
| 193 | +<div style="display: flex; gap: 20px; align-items: start;"> |
| 194 | + <div style="flex: 1;"> |
| 195 | + |
| 196 | +```python |
| 197 | +from pyimagecuda import Image, Fill, Blend, save |
| 198 | + |
| 199 | +base = Image(800, 600) |
| 200 | +overlay = Image(400, 300) |
| 201 | + |
| 202 | +c1 = (0.2, 0.2, 0.3, 1.0) |
| 203 | +c2 = (0.4, 0.3, 0.5, 1.0) |
| 204 | +c3 = (1.0, 0.5, 0.0, 0.8) |
| 205 | +c4 = (1.0, 0.0, 0.5, 0.8) |
| 206 | + |
| 207 | +Fill.gradient(base, c1, c2, 'radial') |
| 208 | +Fill.gradient(overlay, c3, c4, 'horizontal') |
| 209 | + |
| 210 | +Blend.add(base, overlay, anchor='center') |
| 211 | +save(base, 'output.png') |
| 212 | +``` |
| 213 | + |
| 214 | + </div> |
| 215 | + <div style="flex: 1;"> |
| 216 | + <img src="https://offerrall.github.io/pyimagecuda/images/blend_add.png" alt="Add blend" style="width: 100%;"> |
| 217 | + </div> |
| 218 | +</div> |
| 219 | + |
| 220 | +**Use for:** Light effects, lens flares, glowing elements. |
| 221 | + |
| 222 | +--- |
| 223 | + |
| 224 | +## Parameters |
| 225 | + |
| 226 | +All blend modes share the same parameters: |
| 227 | + |
| 228 | +- `base` (Image): Base layer (modified in-place) |
| 229 | +- `overlay` (Image): Layer to composite on top |
| 230 | +- `anchor` (str): Position anchor point (default: `'top-left'`) |
| 231 | +- `offset_x` (int): Horizontal offset from anchor in pixels (default: 0) |
| 232 | +- `offset_y` (int): Vertical offset from anchor in pixels (default: 0) |
| 233 | +- `opacity` (float): Blend opacity from 0.0 (transparent) to 1.0 (opaque) (default: 1.0) |
0 commit comments