Browse Source

bugfix in slicer

master
Evgeniy Ierusalimov 2 weeks ago
parent
commit
f4b925642e
1 changed files with 10 additions and 0 deletions
  1. 10
    0
      src/split/slicer.py

+ 10
- 0
src/split/slicer.py View File

@@ -30,9 +30,19 @@ def load_image(path: Path) -> np.ndarray:
30 30
     image = cv2.imread(str(path), cv2.IMREAD_UNCHANGED)
31 31
     if image is None:
32 32
         raise ValueError(f"Не удалось загрузить изображение: {path}")
33
+    if image.ndim == 3 and image.shape[2] == 4:
34
+        image = _rgba_to_white_bg(image)
33 35
     return image
34 36
 
35 37
 
38
+def _rgba_to_white_bg(image: np.ndarray) -> np.ndarray:
39
+    b, g, r, a = cv2.split(image)
40
+    alpha = a.astype(np.float32) / 255.0
41
+    bgr = cv2.merge([b, g, r])
42
+    composite = bgr * alpha[..., None] + 255.0 * (1.0 - alpha[..., None])
43
+    return composite.astype(np.uint8)
44
+
45
+
36 46
 def rotate_image(image: np.ndarray, degrees: int) -> np.ndarray:
37 47
     if degrees == 90:
38 48
         return cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)

Loading…
Cancel
Save