-
Notifications
You must be signed in to change notification settings - Fork 1
/
ls2rgb.py
executable file
·149 lines (120 loc) · 4.26 KB
/
ls2rgb.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
#!/usr/bin/env python
#--------------------------------------------------------------------------
# ls2rgb.py
#
# assembles three landsat channels to a rgb geotiff with gdal
#
# ls2rgb.py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ls2rgb.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dem_water. If not, see <http://www.gnu.org/licenses/>.
#
#--------------------------------------------------------------------------
try:
from osgeo import gdal
from osgeo.gdalconst import *
gdal.TermProgress = gdal.TermProgress_nocb
except ImportError:
import gdal
from gdalconst import *
try:
import numpy
except ImportError:
import Numeric as numpy
try:
from osgeo import gdalnumeric
except ImportError:
import gdalnumeric
from math import *
import sys
def Usage():
print 'ls2rgb.py - assembles three landsat channels to a rgb geotiff with gdal'
print
print 'Copyright (C) 2013 Christoph Hormann'
print 'This program comes with ABSOLUTELY NO WARRANTY;'
print 'This is free software, and you are welcome to redistribute'
print 'it under certain conditions; see COPYING for details.'
print
print 'Usage: ls2rgb.py [-g gamma] [-s scale] red_file green_file blue_file outfile'
print
sys.exit(1)
red_file = None
green_file = None
blue_file = None
outfile = None
format = 'GTiff'
type = GDT_Byte
gamma = 2.0
scale = 1.0
gdal.AllRegister()
argv = gdal.GeneralCmdLineProcessor( sys.argv )
if argv is None:
sys.exit(0)
i = 1
while i < len(sys.argv):
arg = sys.argv[i]
if arg == '-g':
i = i + 1
gamma = float(sys.argv[i])
elif arg == '-s':
i = i + 1
scale = float(sys.argv[i])
elif red_file is None:
red_file = arg
elif green_file is None:
green_file = arg
elif blue_file is None:
blue_file = arg
elif outfile is None:
outfile = arg
else:
Usage()
i = i + 1
if red_file is None:
Usage()
if green_file is None:
Usage()
if blue_file is None:
Usage()
if outfile is None:
Usage()
red_dataset = gdal.Open( red_file, GA_ReadOnly )
green_dataset = gdal.Open( green_file, GA_ReadOnly )
blue_dataset = gdal.Open( blue_file, GA_ReadOnly )
out_driver = gdal.GetDriverByName(format)
outdataset = out_driver.Create(outfile, red_dataset.RasterXSize, red_dataset.RasterYSize, 3, type)
outdataset.SetGeoTransform( red_dataset.GetGeoTransform() )
outdataset.SetProjection( red_dataset.GetProjection() )
numtype = gdalnumeric.GDALTypeCodeToNumericTypeCode(type)
red_band = red_dataset.GetRasterBand(1)
if (green_dataset.RasterCount > 1):
green_band = green_dataset.GetRasterBand(2)
else:
green_band = green_dataset.GetRasterBand(1)
if (blue_dataset.RasterCount > 2):
blue_band = blue_dataset.GetRasterBand(3)
else:
blue_band = blue_dataset.GetRasterBand(1)
red_outband = outdataset.GetRasterBand(1)
green_outband = outdataset.GetRasterBand(2)
blue_outband = outdataset.GetRasterBand(3)
gdal.TermProgress(0.0)
for i in range(red_band.YSize - 1, -1, -1):
red_scanline = red_band.ReadAsArray(0, i, red_band.XSize, 1, red_band.XSize, 1).astype(float)
green_scanline = green_band.ReadAsArray(0, i, green_band.XSize, 1, green_band.XSize, 1).astype(float)
blue_scanline = blue_band.ReadAsArray(0, i, blue_band.XSize, 1, blue_band.XSize, 1).astype(float)
red_scanline = numpy.power(red_scanline*scale/65535.0, 1.0/gamma)*255.0
green_scanline = numpy.power(green_scanline*scale/65535.0, 1.0/gamma)*255.0
blue_scanline = numpy.power(blue_scanline*scale/65535.0, 1.0/gamma)*255.0
red_outband.WriteArray(red_scanline.astype(numtype), 0, i)
green_outband.WriteArray(green_scanline.astype(numtype), 0, i)
blue_outband.WriteArray(blue_scanline.astype(numtype), 0, i)
gdal.TermProgress((float)(red_band.YSize-i)/red_band.YSize)