-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_write_img.py
184 lines (144 loc) · 4.78 KB
/
read_write_img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# ---- This is <read_write_img.py> ----
"""
Read and write image files using gdal.
Author: Johannes Lohse
"""
import os
import sys
from loguru import logger
import numpy as np
from osgeo import gdal
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
def get_all_bands(f, resample=1, asType=np.float32):
"""Read all bands from input image.
Parameters
----------
f: path to input img file
resample: resampling interval in both x- and y-direction
asType: data type
Returns
-------
img: all bands in image file as array
"""
# open the img file
img_file = gdal.Open(f)
# logger
logger.debug(f'Opened img file: {f}')
logger.debug(f'Resampling interval set to: {resample}')
# count the bands
n_bands = img_file.RasterCount
# read tif file as image
if n_bands == 1:
img = img_file.ReadAsArray()[::resample,::resample].astype(asType)
else:
img = img_file.ReadAsArray()[:,::resample,::resample].astype(asType)
logger.debug(f'Read all bands ({n_bands}) from input file')
return img
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
def write_tif(f, X, asType=gdal.GDT_Float32, overwrite=False):
"""Write array (single or multiple bands) to tif file.
Parameters
----------
f : output tif file
X : input array
asType : gdal data type (default: gdal.GDT_Float32)
overwrite : overwrite if file already exists?
Returns
-------
new_file : return True/False if new tif file has been created
"""
# check if file already exists
if os.path.exists(f):
if overwrite==True:
logger.debug('File already exists, deleting old file')
os.remove(f)
elif overwrite==False:
logger.info('Output file already exists')
logger.info('Exiting without writing')
logger.info('Set overwrite option to True to force')
new_file = False
return new_file
# get dimensions and number of bands
dims = X.shape
if np.size(dims) == 2:
Ny, Nx = X.shape
n_bands = 1
elif np.size(dims) == 3:
n_bands, Ny, Nx = X.shape
if n_bands > Ny or n_bands > Nx:
logger.warning('Number of bands is larger than number of pixels')
logger.warning('Expected shape of input array is (n_bands, Nx, Ny)')
logger.warning('Exiting without writing')
new_file = False
return new_file
else:
logger.error(f'Cannot write array with shape {dims} to tif file')
logger.error('Exiting without writing')
new_file = False
return new_file
# get driver
output = gdal.GetDriverByName('GTiff').Create(f, Nx, Ny, n_bands, asType)
# write to file
if n_bands == 1:
output.GetRasterBand(1).WriteArray(X)
elif n_bands > 1:
for b in np.arange(n_bands):
output.GetRasterBand(int(b+1)).WriteArray(X[b,:,:])
output.FlushCache()
new_file = True
# logger
logger.debug('Wrote input array to tif file')
return new_file
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
def write_img(f, X, asType=gdal.GDT_Float32, overwrite=False):
"""Write array (single band) to img file (and hdr header).
Parameters
----------
f : output img file
X : input array
asType : gdal data type (default: gdal.GDT_Float32)
overwrite : overwrite if file already exists?
Returns
-------
new_file : return True/False if new img file has been created
Examples
--------
write_img('output_file.img', array, asType=gdal.GDT_Float32)
"""
# check if file already exists
if os.path.exists(f):
if overwrite==True:
logger.debug('File already exists, deleting old file')
os.remove(f)
os.remove(os.path.splitext(f)[0]+'.hdr')
elif overwrite==False:
logger.info('File already exists')
logger.info('Exiting without writing')
logger.info('Set overwrite option to True to force')
new_file = False
return new_file
# get dimensions and number of bands
dims = X.shape
if np.size(dims) == 2:
Ny, Nx = X.shape
nBands = 1
else:
logger.error(f'Cannot write array with shape {dims} to img file')
logger.error('Exiting without writing')
new_file = False
return new_file
# get driver
output = gdal.GetDriverByName('Envi').Create(f, Nx, Ny, nBands, asType)
# write to file
output.GetRasterBand(1).WriteArray(X)
output.FlushCache()
new_File = True
# logger
logger.debug('Wrote input array to img file')
return new_File
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
# ---- End of <read_write_img.py> ----