1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: rgb2pct.py 18194 2009-12-06 20:07:45Z rouault $ |
---|
4 | # |
---|
5 | # Name: rgb2pct |
---|
6 | # Project: GDAL Python Interface |
---|
7 | # Purpose: Application for converting an RGB image to a pseudocolored image. |
---|
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 | |
---|
32 | try: |
---|
33 | from osgeo import gdal |
---|
34 | except ImportError: |
---|
35 | import gdal |
---|
36 | |
---|
37 | import sys |
---|
38 | import os.path |
---|
39 | |
---|
40 | def Usage(): |
---|
41 | print('Usage: rgb2pct.py [-n colors | -pct palette_file] [-of format] source_file dest_file') |
---|
42 | sys.exit(1) |
---|
43 | |
---|
44 | # ============================================================================= |
---|
45 | # Mainline |
---|
46 | # ============================================================================= |
---|
47 | |
---|
48 | color_count = 256 |
---|
49 | format = 'GTiff' |
---|
50 | src_filename = None |
---|
51 | dst_filename = None |
---|
52 | pct_filename = None |
---|
53 | |
---|
54 | gdal.AllRegister() |
---|
55 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
56 | if argv is None: |
---|
57 | sys.exit( 0 ) |
---|
58 | |
---|
59 | # Parse command line arguments. |
---|
60 | i = 1 |
---|
61 | while i < len(argv): |
---|
62 | arg = argv[i] |
---|
63 | |
---|
64 | if arg == '-of': |
---|
65 | i = i + 1 |
---|
66 | format = argv[i] |
---|
67 | |
---|
68 | elif arg == '-n': |
---|
69 | i = i + 1 |
---|
70 | color_count = int(argv[i]) |
---|
71 | |
---|
72 | elif arg == '-pct': |
---|
73 | i = i + 1 |
---|
74 | pct_filename = argv[i] |
---|
75 | |
---|
76 | elif src_filename is None: |
---|
77 | src_filename = argv[i] |
---|
78 | |
---|
79 | elif dst_filename is None: |
---|
80 | dst_filename = argv[i] |
---|
81 | |
---|
82 | else: |
---|
83 | Usage() |
---|
84 | |
---|
85 | i = i + 1 |
---|
86 | |
---|
87 | if dst_filename is None: |
---|
88 | Usage() |
---|
89 | |
---|
90 | # Open source file |
---|
91 | |
---|
92 | src_ds = gdal.Open( src_filename ) |
---|
93 | if src_ds is None: |
---|
94 | print('Unable to open ', src_filename) |
---|
95 | sys.exit(1) |
---|
96 | |
---|
97 | if src_ds.RasterCount < 3: |
---|
98 | print('%s has %d band(s), need 3 for inputs red, green and blue.' \ |
---|
99 | % (src_filename, src_ds.RasterCount)) |
---|
100 | sys.exit(1) |
---|
101 | |
---|
102 | # Ensure we recognise the driver. |
---|
103 | |
---|
104 | dst_driver = gdal.GetDriverByName(format) |
---|
105 | if dst_driver is None: |
---|
106 | print('"%s" driver not registered.' % format) |
---|
107 | sys.exit(1) |
---|
108 | |
---|
109 | # Generate palette |
---|
110 | |
---|
111 | ct = gdal.ColorTable() |
---|
112 | if pct_filename is None: |
---|
113 | err = gdal.ComputeMedianCutPCT( src_ds.GetRasterBand(1), |
---|
114 | src_ds.GetRasterBand(2), |
---|
115 | src_ds.GetRasterBand(3), |
---|
116 | color_count, ct, |
---|
117 | callback = gdal.TermProgress, |
---|
118 | callback_data = 'Generate PCT' ) |
---|
119 | else: |
---|
120 | pct_ds = gdal.Open(pct_filename) |
---|
121 | ct = pct_ds.GetRasterBand(1).GetRasterColorTable().Clone() |
---|
122 | |
---|
123 | # Create the working file. We have to use TIFF since there are few formats |
---|
124 | # that allow setting the color table after creation. |
---|
125 | |
---|
126 | if format == 'GTiff': |
---|
127 | tif_filename = dst_filename |
---|
128 | else: |
---|
129 | tif_filename = 'temp.tif' |
---|
130 | |
---|
131 | gtiff_driver = gdal.GetDriverByName( 'GTiff' ) |
---|
132 | |
---|
133 | tif_ds = gtiff_driver.Create( tif_filename, |
---|
134 | src_ds.RasterXSize, src_ds.RasterYSize, 1) |
---|
135 | |
---|
136 | tif_ds.GetRasterBand(1).SetRasterColorTable( ct ) |
---|
137 | |
---|
138 | # ---------------------------------------------------------------------------- |
---|
139 | # We should copy projection information and so forth at this point. |
---|
140 | |
---|
141 | tif_ds.SetProjection( src_ds.GetProjection() ) |
---|
142 | tif_ds.SetGeoTransform( src_ds.GetGeoTransform() ) |
---|
143 | if src_ds.GetGCPCount() > 0: |
---|
144 | tif_ds.SetGCPs( src_ds.GetGCPs(), src_ds.GetGCPProjection() ) |
---|
145 | |
---|
146 | # ---------------------------------------------------------------------------- |
---|
147 | # Actually transfer and dither the data. |
---|
148 | |
---|
149 | err = gdal.DitherRGB2PCT( src_ds.GetRasterBand(1), |
---|
150 | src_ds.GetRasterBand(2), |
---|
151 | src_ds.GetRasterBand(3), |
---|
152 | tif_ds.GetRasterBand(1), |
---|
153 | ct, |
---|
154 | callback = gdal.TermProgress, |
---|
155 | callback_data = 'Generate PCT' ) |
---|
156 | |
---|
157 | tif_ds = None |
---|
158 | |
---|
159 | if tif_filename != dst_filename: |
---|
160 | tif_ds = gdal.Open( tif_filename ) |
---|
161 | dst_driver.CreateCopy( dst_filename, tif_ds ) |
---|
162 | tif_ds = None |
---|
163 | |
---|
164 | gtiff_driver.Delete( tif_filename ) |
---|
165 | |
---|