diff overlay_images.py @ 3:eb173a1fabc4 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
author imgteam
date Mon, 23 Sep 2024 10:26:14 +0000
parents f42d21fe65d8
children
line wrap: on
line diff
--- a/overlay_images.py	Thu Aug 10 07:29:24 2023 +0000
+++ b/overlay_images.py	Mon Sep 23 10:26:14 2024 +0000
@@ -7,6 +7,7 @@
 
 import argparse
 
+import giatools.io
 import matplotlib.colors
 import matplotlib.pyplot as plt
 import numpy as np
@@ -18,7 +19,7 @@
 
 
 def read_im_gray(fn):
-    img = skimage.io.imread(fn)
+    img = giatools.io.imread(fn)
     nDims = len(img.shape)
     assert nDims in [2, 3], 'this tool only supports single 2D images'
     if nDims == 3 and img.shape[-1] in [3, 4]:
@@ -33,15 +34,17 @@
     img = np.squeeze(img)
     assert img.ndim == 2 or (img.ndim == 3 and img.shape[-1] in (3, 4))
     if str(img.dtype).startswith('float'):
-        img = np.round(img * 255).astype('uint8')
-    elif img.dtype == 'uint16':
-        img = img // 256
-    elif img.dtype != 'uint8':
+        img = np.round(img * 255).astype(np.uint8)
+    elif img.dtype == np.uint16:
+        img = (img // 256).astype(np.uint8)
+    elif img.dtype != np.uint8:
         raise ValueError(f'unknown dtype: {img.dtype}')
     if img.ndim == 2:
-        return np.dstack([img] * 3).copy()
+        result = np.dstack([img] * 3).copy()
     else:
-        return img[:, :, :3].copy()
+        result = img[:, :, :3].copy()
+    assert result.dtype == np.uint8, result.dtype
+    return result
 
 
 def coloc_vis(in_red_fn, in_green_fn, out_fn):
@@ -59,8 +62,8 @@
 
 
 def blending(im1_fn, im2_fn, out_fn, alpha=0.5):
-    im1 = skimage.io.imread(im1_fn)
-    im2 = skimage.io.imread(im2_fn)
+    im1 = giatools.io.imread(im1_fn)
+    im2 = giatools.io.imread(im2_fn)
     assert im1.shape == im2.shape, 'Two images should have the same dimension'
     out_im = (1 - alpha) * im1 + alpha * im2
     if len(im1.shape) > 3:
@@ -70,8 +73,8 @@
 
 
 def seg_contour(im1_fn, im2_fn, out_fn, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'):
-    img = skimage.io.imread(im1_fn)
-    labels = skimage.io.imread(im2_fn)
+    img = giatools.io.imread(im1_fn)
+    labels = giatools.io.imread(im2_fn)
 
     result = get_rgb8_copy(img)
     cp = ContourPaint(labels, linewidth, where='center')