1、遇到的问题:numpy版本

im_data = dataset.ReadAsArray(0,0,im_width,im_height)#获取数据 这句报错

升级numpy:pip install -U numpy 但是提示已经是最新版本

解决:卸载numpy 重新安装

2.直接从压缩包中读取tiff图像

参考:http://gdal.org/gdal_virtual_file_systems.html#gdal_virtual_file_systems_vsizip

当前情况是2层压缩: /'/vsitar/C:/Users/summer/Desktop/a_PAN1.tiff'

3.读tiff

def readTif(fileName):
	
	merge_img = 0
	driver = gdal.GetDriverByName('GTiff')
	driver.Register()
 
	dataset = gdal.Open(fileName)
	if dataset == None:
		print(fileName+ "掩膜失败,文件无法打开")
		return
	im_width = dataset.RasterXSize #栅格矩阵的列数
	print('im_width:', im_width) 
 
	im_height = dataset.RasterYSize #栅格矩阵的行数
	print('im_height:', im_height) 
	im_bands = dataset.RasterCount #波段数
	im_geotrans = dataset.GetGeoTransform()#获取仿射矩阵信息
	im_proj = dataset.GetProjection()#获取投影信息
	
 
	if im_bands == 1:
		band = dataset.GetRasterBand(1)
		im_data = dataset.ReadAsArray(0,0,im_width,im_height) #获取数据
		cdata = im_data.astype(np.uint8)
		merge_img = cv2.merge([cdata,cdata,cdata])
 
		cv2.imwrite('C:/Users/summer/Desktop/a.jpg', merge_img)
# 
	elif im_bands == 4:
	# 	# im_data = dataset.ReadAsArray(0,0,im_width,im_height)#获取数据
	# 	# im_blueBand = im_data[0,0:im_width,0:im_height] #获取蓝波段
	# 	# im_greenBand = im_data[1,0:im_width,0:im_height] #获取绿波段
	# 	# im_redBand = im_data[2,0:im_width,0:im_height] #获取红波段
	# 	# # im_nirBand = im_data[3,0:im_width,0:im_height] #获取近红外波段
	# 	# merge_img=cv2.merge([im_redBand,im_greenBand,im_blueBand])
 
	# 	# zeros = np.zeros([im_height,im_width],dtype = "uint8")
 
	# 	# data1 = im_redBand.ReadAsArray
 
	# 	band1=dataset.GetRasterBand(1)
	# 	band2=dataset.GetRasterBand(2)
	# 	band3=dataset.GetRasterBand(3)
	# 	band4=dataset.GetRasterBand(4)
	
		data1=band1.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #r #获取数据
		data2=band2.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #g #获取数据
		data3=band3.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #b #获取数据
		data4=band4.ReadAsArray(0,0,im_width,im_height).astype(np.uint16) #R #获取数据
	# 	print(data1[1][45])
	# 	output1= cv2.convertScaleAbs(data1, alpha=(255.0/65535.0))
	# 	print(output1[1][45])
	# 	output2= cv2.convertScaleAbs(data2, alpha=(255.0/65535.0))
	# 	output3= cv2.convertScaleAbs(data3, alpha=(255.0/65535.0))
 
		merge_img1 = cv2.merge([output3,output2,output1]) #B G R
		
		cv2.imwrite('C:/Users/summer/Desktop/merge_img1.jpg', merge_img1)

4.图像裁剪:

import cv2
import numpy as np
import os
 
tiff_file = './try_img/2.tiff'
save_folder = './try_img_re/'
if not os.path.exists(save_folder):
	os.makedirs(save_folder)
 
tif_img = cv2.imread(tiff_file)
width, height, channel = tif_img.shape
# print height, width, channel : 6908 7300 3
threshold = 1000
overlap = 100
 
step = threshold - overlap
x_num = width/step + 1
y_num = height/step + 1
print x_num, y_num
 
N = 0
yj = 0 
 
for xi in range(x_num):
	for yj in range(y_num):
	# print xi
		if yj <= y_num:
			print yj
			x = step*xi
	  y = step*yj
 
	  wi = min(width,x+threshold)
	  hi = min(height,y+threshold)
	  # print wi , hi
 
	  if wi-x < 1000 and hi-y < 1000:
	  	im_block = tif_img[wi-1000:wi, hi-1000:hi]
 
	  elif wi-x > 1000 and hi-y < 1000:
	  	im_block = tif_img[x:wi, hi-1000:hi]
 
	  elif wi-x < 1000 and hi-y > 1000:
	  	im_block = tif_img[wi-1000:wi, y:hi]
 
	 	else:
	  	im_block = tif_img[x:wi,y:hi]
	  	
	  cv2.imwrite(save_folder + 'try' + str(N) + '.jpg', im_block)
	  N += 1

以上这篇对Python3+gdal 读取tiff格式数据的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?