from osgeo import gdal # 读取tif数据集 def readTif(fileName): dataset = gdal.Open(fileName) if dataset == None: print(fileName + "文件无法打开") return dataset # 获取仿射矩阵信息 def Getgeotrans(fileName): dataset = readTif(fileName) return dataset.GetGeoTransform() # 像素坐标和地理坐标仿射变换 def CoordTransf(Xpixel, Ypixel, GeoTransform): XGeo = GeoTransform[0] + GeoTransform[1] * Xpixel + Ypixel * GeoTransform[2] YGeo = GeoTransform[3] + GeoTransform[4] * Xpixel + Ypixel * GeoTransform[5] return XGeo, YGeo def get_lon_lat(fileName, Xpixel, Ypixel): GeoTransform = Getgeotrans(fileName) XGeo, YGeo = CoordTransf(Xpixel, Ypixel, GeoTransform) return XGeo, YGeo fileName = "图片名称.tif" Xpixel, Ypixel = 719.0, 1600.5 print(get_lon_lat(fileName, Xpixel, Ypixel))