source: branches/0.5/win/PostNAS-0.5/bin/pct2rgb.py @ 23

Revision 23, 5.2 KB checked in by astrid.emde, 14 years ago (diff)
RevLine 
[23]1#!/usr/bin/env python
2#******************************************************************************
3#  $Id: pct2rgb.py 18194 2009-12-06 20:07:45Z rouault $
4#
5#  Name:     pct2rgb
6#  Project:  GDAL Python Interface
7#  Purpose:  Utility to convert palletted images into RGB (or RGBA) images.
8#  Author:   Frank Warmerdam, warmerdam@pobox.com
9#
10#******************************************************************************
11#  Copyright (c) 2001, Frank Warmerdam
12#
13#  Permission is hereby granted, free of charge, to any person obtaining a
14#  copy of this software and associated documentation files (the "Software"),
15#  to deal in the Software without restriction, including without limitation
16#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17#  and/or sell copies of the Software, and to permit persons to whom the
18#  Software is furnished to do so, subject to the following conditions:
19#
20#  The above copyright notice and this permission notice shall be included
21#  in all copies or substantial portions of the Software.
22#
23#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29#  DEALINGS IN THE SOFTWARE.
30#******************************************************************************
31
32try:
33    from osgeo import gdal
34    gdal.TermProgress = gdal.TermProgress_nocb
35except ImportError:
36    import gdal
37
38try:
39    import numpy as Numeric
40    Numeric.arrayrange = Numeric.arange
41except ImportError:
42    import Numeric
43
44
45import sys
46import os.path
47
48def Usage():
49    print('Usage: pct2rgb.py [-of format] [-b <band>] source_file dest_file')
50    sys.exit(1)
51
52# =============================================================================
53#       Mainline
54# =============================================================================
55
56format = 'GTiff'
57src_filename = None
58dst_filename = None
59out_bands = 3
60band_number = 1
61
62gdal.AllRegister()
63argv = gdal.GeneralCmdLineProcessor( sys.argv )
64if argv is None:
65    sys.exit( 0 )
66
67# Parse command line arguments.
68i = 1
69while i < len(argv):
70    arg = argv[i]
71
72    if arg == '-of':
73        i = i + 1
74        format = argv[i]
75
76    elif arg == '-b':
77        i = i + 1
78        band_number = int(argv[i])
79
80    elif arg == '-rgba':
81        out_bands = 4
82
83    elif src_filename is None:
84        src_filename = argv[i]
85
86    elif dst_filename is None:
87        dst_filename = argv[i]
88
89    else:
90        Usage()
91
92    i = i + 1
93
94if dst_filename is None:
95    Usage()
96   
97# ----------------------------------------------------------------------------
98# Open source file
99
100src_ds = gdal.Open( src_filename )
101if src_ds is None:
102    print('Unable to open ', src_filename)
103    sys.exit(1)
104
105src_band = src_ds.GetRasterBand(band_number)
106
107# ----------------------------------------------------------------------------
108# Ensure we recognise the driver.
109
110dst_driver = gdal.GetDriverByName(format)
111if dst_driver is None:
112    print('"%s" driver not registered.' % format)
113    sys.exit(1)
114
115# ----------------------------------------------------------------------------
116# Build color table.
117
118lookup = [ Numeric.arrayrange(256),
119           Numeric.arrayrange(256),
120           Numeric.arrayrange(256),
121           Numeric.ones(256)*255 ]
122
123ct = src_band.GetRasterColorTable()
124
125if ct is not None:
126    for i in range(min(256,ct.GetCount())):
127        entry = ct.GetColorEntry(i)
128        for c in range(4):
129            lookup[c][i] = entry[c]
130
131# ----------------------------------------------------------------------------
132# Create the working file.
133
134if format == 'GTiff':
135    tif_filename = dst_filename
136else:
137    tif_filename = 'temp.tif'
138
139gtiff_driver = gdal.GetDriverByName( 'GTiff' )
140
141tif_ds = gtiff_driver.Create( tif_filename,
142                              src_ds.RasterXSize, src_ds.RasterYSize, out_bands )
143
144
145# ----------------------------------------------------------------------------
146# We should copy projection information and so forth at this point.
147
148tif_ds.SetProjection( src_ds.GetProjection() )
149tif_ds.SetGeoTransform( src_ds.GetGeoTransform() )
150if src_ds.GetGCPCount() > 0:
151    tif_ds.SetGCPs( src_ds.GetGCPs(), src_ds.GetGCPProjection() )
152
153# ----------------------------------------------------------------------------
154# Do the processing one scanline at a time.
155
156gdal.TermProgress( 0.0 )
157for iY in range(src_ds.RasterYSize):
158    src_data = src_band.ReadAsArray(0,iY,src_ds.RasterXSize,1)
159
160    for iBand in range(out_bands):
161        band_lookup = lookup[iBand]
162
163        dst_data = Numeric.take(band_lookup,src_data)
164        tif_ds.GetRasterBand(iBand+1).WriteArray(dst_data,0,iY)
165
166    gdal.TermProgress( (iY+1.0) / src_ds.RasterYSize )
167   
168
169tif_ds = None
170
171# ----------------------------------------------------------------------------
172# Translate intermediate file to output format if desired format is not TIFF.
173
174if tif_filename != dst_filename:
175    tif_ds = gdal.Open( tif_filename )
176    dst_driver.CreateCopy( dst_filename, tif_ds )
177    tif_ds = None
178
179    gtiff_driver.Delete( tif_filename )
180
181
182
183
184
185
186
Note: See TracBrowser for help on using the repository browser.