Ticket #2227: extract_ctable.py
| File extract_ctable.py, 458 bytes (added by , 6 years ago) | 
|---|
| Line | |
|---|---|
| 1 | import gdal | 
| 2 | import json | 
| 3 | import sys | 
| 4 | |
| 5 | if len(sys.argv) < 3: | 
| 6 | print "Required positional arguments: file path, output path." | 
| 7 | exit(1) | 
| 8 | else: | 
| 9 | fname = sys.argv[1] | 
| 10 | oname = sys.argv[2] | 
| 11 | |
| 12 | f = gdal.Open(fname) | 
| 13 | band = f.GetRasterBand(1) | 
| 14 | ct = band.GetRasterColorTable() | 
| 15 | clist = [] | 
| 16 | for i in range(ct.GetCount()): | 
| 17 | clist.append(list(ct.GetColorEntry(i))) | 
| 18 | |
| 19 | result = {} | 
| 20 | result["colorTable"] = clist | 
| 21 | |
| 22 | with open(oname, "w") as ofile: | 
| 23 | ofile.write(json.dumps(result)) | 
