如何用python读取和写入TIFF文件1

如何用python读取和写入TIFF文件1,第1张

用python读取TIFF文件,可采用以下代码世渗

framedim = [2048,2048]

nb_elem = framedim[0]*framedim[1]

offset = 4096

formatdata = np.uint16

f = open(path, '手如rb')

f.seek(offset)#TODO: only header size for tiff !!

d = np.fromfile(f, dtype=formatdata, count=nb_elem).reshape(framedim)

写入TIFF文件,则需要pylibtiff库

例如

from libtiff import TIFF

tif = TIFF.open(path, '毕返启w')

tif.write_image(image)

image为二维ndarray

用 gdal库。

库参考和下载在这:http://www.gdal.org/

下面这个文件瞎写的,你可以修改下。。需要加装库,头文件和lib

//tif.h

enum

{

TOPX = 0,

CELLX,

XXXX,

TOPY,

XXXXX,

CELLY,

GeoTfInfoArrCount

}

 template<typename type, int nBand =1>

 class CTifFileOp

 {

 public:

 CTifFileOp(string FilePath=string(""))

virtual ~CTifFileOp()

double get_nodata_value()

{

return m_pDataset->GetRasterBand(nBand)->GetNoDataValue()

}

int GetGeoTransform(double* gt)const

int ChangeFile(string strPath)

void ReadTiffDataset(type * DataGet, int begRow = 0, int begCol = 0, int numRows = 1, int numCols = 1)const

void WriteTiffDataset(type * DataGet, int begRow = 0, int begCol = 0, int numRows = 1, int numCols = 1)

void ReadTiffDataset(vector<type> & DataGet, int begRow = 0, int begCol = 0, int numRows = 1, int numCols = 1)const

{

DataGet.clear()

DataGet.resize(numCols*numRows)

ReadTiffDataset(&*DataGet.begin(), begRow, begCol, numRows, numCols)

}

size_t GetColNum()

{

openDs()

return m_pDataset->GetRasterXSize()

}

size_t GetRowNum()

{

openDs()

return m_pDataset->GetRasterYSize()

}

//获取波段数

int GetRasterCount()

{

openDs()

return m_pDataset->GetRasterCount()

}

//获取坐族闷标系信息

const char * GetProjectionRef()

{

openDs()

return m_pDataset->GetProjectionRef()

}

//获取数据类型

GDALDataType GetDataType(int iRaster = nBand)

{

openDs()

return m_pDataset->GetRasterBand(iRaster)->GetRasterDataType()

}

void GetAllData(vector<type> & DataGet)

{

//   double geoInfo[GeoTfInfoArrCount] = { 0 }

//   GetGeoTransform(geoInfo)

openDs()

ReadTiffDataset(DataGet, 0, 0, GetRowNum(), 悉数GetColNum())

}

 private:

 string m_strFile

 GDALDataset *m_pDataset = nullptr

 bool m_bWrite = false

 private:

 void openDs(GDALAccess openMode = GA_ReadOnly)

 bool CanWrite()const

 {

 return m_bWrite

 }

}

 

 template<typename type, int nBand>

 int CTifFileOp<type, nBand>::GetGeoTransform(double* gt) const

 {

// openDs()

 m_pDataset->GetGeoTransform(gt)

 return 睁穗首0

 }

 

 template<typename type, int nBand>

 CTifFileOp<type, nBand>::CTifFileOp(string FilePath)

 :m_strFile(FilePath), m_pDataset(nullptr)

 {

openDs()

 }

 template<typename type, int nBand>

 CTifFileOp<type, nBand>::~CTifFileOp()

 {

if (m_pDataset)

{

GDALClose(m_pDataset)

m_pDataset = nullptr

//是这样关闭 数据集 么?

}

 }

 template<typename type, int nBand>

 void CTifFileOp<type, nBand>::openDs(GDALAccess openMode)

{

 if (m_pDataset)//当不为nullptr,认为是打开的

 {

 return

 }

GDALAllRegister()

const char *pszFormat = "GTiff"

GDALDriver *poDriver

poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat)

if (poDriver == nullptr){

return

}

if (!m_strFile.empty())

{

m_pDataset = ((GDALDataset *)GDALOpen(m_strFile.c_str(), openMode))

//  if (m_pDataset != NULL)

//  {

//  int ibandCounts = m_pDataset->GetRasterCount()

//  ibandCounts |= (long(1)<<0)

//  }

if (GA_Update == openMode)

{

m_bWrite = true

}

else

m_bWrite = false

}

// OGRCleanupAll()

}

template<typename type, int nBand>

int CTifFileOp<type, nBand>::ChangeFile(string strPath)

{

m_strFile = strPath

if (m_pDataset)

{

GDALClose(m_pDataset)

m_pDataset = nullptr

//是这样关闭 数据集 么?

}

openDs()

return 0

}

template<typename type, int nBand>

void CTifFileOp<type, nBand>::ReadTiffDataset(type * DataGet, int begRow, int begCol, int numRows, int numCols)const

{

if (!DataGet || !m_pDataset)

{

return

}

if (begRow < 0 || begCol < 0 || numRows <= 0 || numCols <= 0)

{

return

}

GDALDataType datatype = m_pDataset->GetRasterBand(nBand)->GetRasterDataType()

//不知道下面读数据的这个类型参数有什么意义

m_pDataset->RasterIO(GF_Read,begCol,begRow,numCols,numRows,DataGet,numCols,numRows,datatype,1,0,0,0,0)

}

template<typename type,int nBand>

void CTifFileOp<type, nBand>::WriteTiffDataset(type * DataGet, int begRow, int begCol, int numRows, int numCols)

{

if (!DataGet || !m_pDataset)

{

return

}

if (begRow < 0 || begCol < 0 || numRows <= 0 || numCols <= 0)

{

return

}

if (!CanWrite())

{

GDALClose(m_pDataset)

m_pDataset = nullptr

openDs(GA_Update)

}

GDALDataType datatype = m_pDataset->GetRasterBand(nBand)->GetRasterDataType()

//不知道下面读数据的这个类型参数有什么意义

m_pDataset->RasterIO(GF_Write, begCol, begRow, numCols, numRows, DataGet, numCols, numRows, datatype, 1, 0, 0, 0, 0)

}

如果你要在程序中 *** 作TIFF文仔或岁件,可以访问PaintLib的主页http://www.paintlib.de/paintlib/。Paintlib是一个团清C++类,支持PNG、TGA、TIFF、JPEG/念睁JFIF、Windows BMP、Mac PICT和EPS。

或者到http://www.libtiff.org下载libtiff库。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12227649.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存