Update open_tiff function for supporting images written by rasterio and tifffile

#1
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -14,6 +14,7 @@ from mmseg.models import build_segmentor
14
  from mmseg.datasets.pipelines import Compose, LoadImageFromFile
15
 
16
  import rasterio
 
17
  import torch
18
 
19
  from mmseg.apis import init_segmentor
@@ -45,13 +46,19 @@ def stretch_rgb(rgb):
45
  return img_rescale
46
 
47
 
48
- def open_tiff(fname):
49
-
50
  with rasterio.open(fname, "r") as src:
51
-
52
- data = src.read()
53
-
54
- return data
 
 
 
 
 
 
 
55
 
56
  def write_tiff(img_wrt, filename, metadata):
57
 
 
14
  from mmseg.datasets.pipelines import Compose, LoadImageFromFile
15
 
16
  import rasterio
17
+ import tifffile
18
  import torch
19
 
20
  from mmseg.apis import init_segmentor
 
46
  return img_rescale
47
 
48
 
49
+ def open_tiff(fname): # it supports images written by rasterio and tifffile
 
50
  with rasterio.open(fname, "r") as src:
51
+ img_rasterio = src.read()
52
+
53
+ with tifffile.TiffFile(fname) as tif:
54
+ img_tifffile = tif.asarray()
55
+
56
+ if img_rasterio.shape == img_tifffile.shape:
57
+ return img_rasterio
58
+ if img_rasterio.shape[0] == img_tifffile.shape[-1]:
59
+ return img_rasterio
60
+ return img_tifffile
61
+
62
 
63
  def write_tiff(img_wrt, filename, metadata):
64