带调色板TIFF到Jpeg的转化Python脚本
2007-04-28 08:30 flyingfish 阅读(1423) 评论(0) 编辑 收藏 举报发现东西存成文件放到电脑里边超级难找,找不到就成了破烂,说不定下次就被Format掉了。最近赶51前赶快把机器里不积累的小东西发到BLOG上来存档。
下边是很早转DRG数据时写的Python脚本都快在电脑里边沉下去了。
1 # 功能:用于将调色板GEOTIFF转化为JPEG并获取元数据.py
2 # 作者:贠建明
3 # 日期:2007-1-22
4
5 import os,sys,string
6
7 # 遍历目录获取TIF文件名
8 def GetTiffFileName(dir):
9 filenames = []
10
11 dirlist=os.listdir(dir)
12 for subdir in dirlist:
13 subdir = os.path.join(dir, subdir)
14 if os.path.isdir(subdir):
15 subfilelist=os.listdir(subdir)
16 for filename in subfilelist:
17 if filename[-4:].lower()==".tif":
18 filenames.append(os.path.join(subdir, filename))
19 else:
20 filenames.append(subdir)
21
22 filelist=file("c:/filelist.txt",'w')
23 for filename in filenames:
24 filelist.write(filename + "\n")
25
26 filelist.close()
27 return filenames
28
29
30 # 获取元数据到文件
31 def GetMetadata(filenames):
32 metainfo = file('c:/metainfo.txt','w')
33 for filename in filenames:
34 os.system("gdalinfo > C:/info.txt " + filename)
35 info = file('C:/info.txt','r')
36 while True:
37 line = info.readline()
38 if len(line) == 0:
39 break
40
41 if line[:4] == 'Size':
42 #文件地址
43 metainfo.write('\n')
44 i = filename.rfind('\\')+1
45 path = "c:\\JPG\\" + filename[i:-4] + ".jpg"
46 metainfo.write(path)
47 metainfo.write(',')
48 #分幅号
49 noOrigin = filename[-17:-7]
50 intNO = ord(noOrigin[0])-ord('A') + 1
51 mapNo = str(intNO)+noOrigin[1:3]+noOrigin[4:]
52 metainfo.write(mapNo)
53 metainfo.write(',')
54 # 列行号
55 metainfo.write(line[8:len(line)-1])
56 if line[:10] == 'Upper Left':
57 metainfo.write(',')
58 metainfo.write(line[14:len(line)-3])
59 if line[:11] == 'Lower Right':
60 metainfo.write(',')
61 metainfo.write(line[14:len(line)-3])
62 metainfo.write(',')
63 # 投影带号
64 strip = string.atoi(filename[-16:-14]) #'H:\DRG\I48E009021\I48E009021DRG.tif'[-16:-14]='48'
65 strip = strip-30
66 metainfo.write(str(strip))
67
68 info.close()
69
70 metainfo.close();
71
72 for filename in filenames:
73 os.system("gdalinfo >> C:/AllInfo.txt " + filename)
74
75
76 # 进行TIFF到Jpeg的转化
77 def GeoTiff2Jpeg(filenames):
78 for filename in filenames:
79 os.system("call C:\FWTools1.0.5\setfw.bat & pct2rgb "+filename+" c:/rgb.tif")
80 i = filename.rfind('\\')+1
81 os.system("gdal_translate c:/rgb.tif -of JPEG c:/JPG/" + filename[i:-4] + ".jpg")
82
83
84 if __name__=="__main__":
85 files = GetTiffFileName('I:\\5万实验数据\\DRG')
86 #GetMetadata(files)
87 GeoTiff2Jpeg(files)
88
2 # 作者:贠建明
3 # 日期:2007-1-22
4
5 import os,sys,string
6
7 # 遍历目录获取TIF文件名
8 def GetTiffFileName(dir):
9 filenames = []
10
11 dirlist=os.listdir(dir)
12 for subdir in dirlist:
13 subdir = os.path.join(dir, subdir)
14 if os.path.isdir(subdir):
15 subfilelist=os.listdir(subdir)
16 for filename in subfilelist:
17 if filename[-4:].lower()==".tif":
18 filenames.append(os.path.join(subdir, filename))
19 else:
20 filenames.append(subdir)
21
22 filelist=file("c:/filelist.txt",'w')
23 for filename in filenames:
24 filelist.write(filename + "\n")
25
26 filelist.close()
27 return filenames
28
29
30 # 获取元数据到文件
31 def GetMetadata(filenames):
32 metainfo = file('c:/metainfo.txt','w')
33 for filename in filenames:
34 os.system("gdalinfo > C:/info.txt " + filename)
35 info = file('C:/info.txt','r')
36 while True:
37 line = info.readline()
38 if len(line) == 0:
39 break
40
41 if line[:4] == 'Size':
42 #文件地址
43 metainfo.write('\n')
44 i = filename.rfind('\\')+1
45 path = "c:\\JPG\\" + filename[i:-4] + ".jpg"
46 metainfo.write(path)
47 metainfo.write(',')
48 #分幅号
49 noOrigin = filename[-17:-7]
50 intNO = ord(noOrigin[0])-ord('A') + 1
51 mapNo = str(intNO)+noOrigin[1:3]+noOrigin[4:]
52 metainfo.write(mapNo)
53 metainfo.write(',')
54 # 列行号
55 metainfo.write(line[8:len(line)-1])
56 if line[:10] == 'Upper Left':
57 metainfo.write(',')
58 metainfo.write(line[14:len(line)-3])
59 if line[:11] == 'Lower Right':
60 metainfo.write(',')
61 metainfo.write(line[14:len(line)-3])
62 metainfo.write(',')
63 # 投影带号
64 strip = string.atoi(filename[-16:-14]) #'H:\DRG\I48E009021\I48E009021DRG.tif'[-16:-14]='48'
65 strip = strip-30
66 metainfo.write(str(strip))
67
68 info.close()
69
70 metainfo.close();
71
72 for filename in filenames:
73 os.system("gdalinfo >> C:/AllInfo.txt " + filename)
74
75
76 # 进行TIFF到Jpeg的转化
77 def GeoTiff2Jpeg(filenames):
78 for filename in filenames:
79 os.system("call C:\FWTools1.0.5\setfw.bat & pct2rgb "+filename+" c:/rgb.tif")
80 i = filename.rfind('\\')+1
81 os.system("gdal_translate c:/rgb.tif -of JPEG c:/JPG/" + filename[i:-4] + ".jpg")
82
83
84 if __name__=="__main__":
85 files = GetTiffFileName('I:\\5万实验数据\\DRG')
86 #GetMetadata(files)
87 GeoTiff2Jpeg(files)
88