gpt4 book ai didi

python - 如何更改或删除 GeoTIFF 标签?

转载 作者:行者123 更新时间:2023-12-01 06:41:43 40 4
gpt4 key购买 nike

到目前为止,我找不到更改 TIFF 文件中的 GeoTiff-TAG 的方法。我的操作系统是 Linux Lubuntu 18.04,我使用的是 Python 3.x。只是 EXIF-TAG 可以通过各种方法进行更改。

对于读取和写入 EXIF-TAG,我可以使用例如:

  • 命令行工具:exiftool
  • python 模块:exifread
  • python 模块:PIL
  • python 模块:tifffile
  • python 模块:skimage.external.tifffile

特别是Python模块似乎对GeoTiff标签“视而不见”,即它们只读取(和写入)EXIF标签,而不读取GeoTiff标签。使用 exiftool,我至少可以读取并打印 GeoTiff-TAG 以及其他标签。例如,通过我的 TIFF 文件的 exiftool 的控制台输出(其中还包括 GeoTiff 标签)将是:

输入:

exiftool -D -G -a -u -U -f "newfile.tif"

输出:

[ExifTool]          - ExifTool Version Number         : 10.80
[File] - File Name : newfile.tif
[File] - Directory : .
[File] - File Size : 1503 kB
[File] - File Modification Date/Time : 2019:12:19 17:32:17+01:00
[File] - File Access Date/Time : 2019:12:19 17:32:17+01:00
[File] - File Inode Change Date/Time : 2019:12:19 17:32:17+01:00
[File] - File Permissions : rw-rw-r--
[File] - File Type : TIFF
[File] - File Type Extension : tif
[File] - MIME Type : image/tiff
[File] - Exif Byte Order : Little-endian (Intel, II)
[File] - Current IPTC Digest : 79ffcf282ca6974ff99640a7421b40b7
[EXIF] 256 Image Width : 1148
[EXIF] 257 Image Height : 1337
[EXIF] 258 Bits Per Sample : 8
[EXIF] 259 Compression : Uncompressed
[EXIF] 262 Photometric Interpretation : RGB Palette
[EXIF] 273 Strip Offsets : (Binary data 1390 bytes, use -b option to extract)
[EXIF] 274 Orientation : Horizontal (normal)
[EXIF] 277 Samples Per Pixel : 1
[EXIF] 278 Rows Per Strip : 7
[EXIF] 279 Strip Byte Counts : (Binary data 954 bytes, use -b option to extract)
[EXIF] 282 X Resolution : 1
[EXIF] 283 Y Resolution : 1
[EXIF] 284 Planar Configuration : Chunky
[EXIF] 296 Resolution Unit : None
[EXIF] 305 Software : IMAGINE TIFF Support.Copyright 1991 - 1999 by ERDAS, Inc. All Rights Reserved.@(#)$RCSfile: etif.c $ $Revision: 1.11 $ $Date$
[EXIF] 320 Color Map : (Binary data 1536 bytes, use -b option to extract)
[EXIF] 339 Sample Format : Unsigned
[EXIF] 33550 Pixel Scale : 30 30 0
[EXIF] 33922 Model Tie Point : 0 0 0 1514925 1583985 0
[IPTC] 25 Keywords : word
[IPTC] 0 Application Record Version : 4
[GeoTiff] 1 Geo Tiff Version : 1.1.0
[GeoTiff] 1024 GT Model Type : Projected
[GeoTiff] 1025 GT Raster Type : Pixel Is Area
[GeoTiff] 1026 GT Citation : IMAGINE GeoTIFF Support.Copyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved.@(#)$RCSfile: egtf.c $ $Revision: 1.11.2.3 $ $Date: 2004/11/24 09:12:56EST $.Projection Name = USA_Contiguous_Albers_Equal_Area_Conic_USGS_version.Units = meters.GeoTIFF Units = meters
[GeoTiff] 2048 Geographic Type : NAD83
[GeoTiff] 3072 Projected CS Type : User Defined
[GeoTiff] 3073 PCS Citation : IMAGINE GeoTIFF Support.Copyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved.@(#)$RCSfile: egtf.c $ $Revision: 1.11.2.3 $ $Date: 2004/11/24 09:12:56EST $.Projection = Albers Conical Equal Area
[GeoTiff] 3074 Projection : User Defined
[GeoTiff] 3075 Proj Coord Trans : Albers Equal Area
[GeoTiff] 3076 Proj Linear Units : Linear Meter
[GeoTiff] 3078 Proj Std Parallel 1 : 29.5
[GeoTiff] 3079 Proj Std Parallel 2 : 45.5
[GeoTiff] 3081 Proj Nat Origin Lat : 23
[GeoTiff] 3082 Proj False Easting : 0
[GeoTiff] 3083 Proj False Northing : 0
[GeoTiff] 3088 Proj Center Long : -96
[Composite] - Image Size : 1148x1337
[Composite] - Megapixels : 1.5

然后,我尝试更改/删除例如GeoTiff-TAG“投影”,它会抛出以下警告消息而不更改任何内容:输入:

exiftool "-Projection=""newfile.tif"

输出:

警告:抱歉,投影不可写。无事可做。

在互联网上的搜索中,我找到了其他库和软件包,它们也许可以帮助我,但到目前为止没有用,因为我没有找到文档,或者它不起作用:

  • libtiff (通过命令 tiffset ,例如 "tiffset -s Tag-Number Tag-Value dst-file",仅更改/添加 EXIF 标签,但不触及 GeoTIFF 标签)
  • libgeotiff
  • gdal_editgdal_translate(或一般情况下的gdal)

对于后一种方法,gdal_editgdal_translate ,我尝试了以下操作但没有效果(即使没有任何错误消息):

gdal_edit.py -mo "3075=Custom" "newfile.tif"
gdal_edit.py -mo "ProjCoordTrans=Custom" "newfile.tif"
gdal_translate -mo "3075=Custom" "newfile.tif" "newfile_2.tif"
gdal_translate -mo "ProjCoordTrans=Custom" "newfile.tif" "newfile_2.tif"

如您所见,在示例中,我尝试将名为“ProjCoordTrans”的 GeoTIFF 标签编号 3075 修改为 Costum 字符串。当执行这 4 个命令中的任何一个时,都没有抛出错误,但随后通过检查 TIFF-Tags

exiftool -D -G -a -u -U -f -s "newfile.tif"

我发现相同的TAG仍然没有变化。

我想知道是否可以更改 GeoTiff 标签。一定有这种可能。

最佳答案

我找到了一个answer在 exiftool 本身的项目页面上。此后,我将总结在页面作者Phil Harvey的帮助下我发现的内容。 .

要将所有 GeoTiff 标签从一个文件复制到另一个文件,请执行以下操作:

exiftool -tagsfromfile SRCFILE -GeoTiffDirectory -GeoTiffDoubleParams -GeoTiffAsciiParams DSTFILE

这样就可以了。我假设这些伪标签被所有子 GeoTiff 标签中的 exiftools 分割,因为 python 中的其他工具将这些伪标签打印为标签及其相关编号。能够显示这些通用 3 个 Geotiff-TAG 的 Python 包

  • ('34735') 'GeoKeyDirectoryTag'
  • ('34736') 'GeoDoubleParamsTag'
  • ('34737') 'GeoAsciiParamsTag'

例如:

import PIL
import tifffile as tf
from skimage.external import tifffile as sk_tf

通过我的 python 脚本使用 PILLOW (PIL) 应用于示例 TIFF 的示例输出如下:

...
** GENERIC APPROACH USING PILLOW-package **

TAG number: '254'
TAG name: 'NewSubfileType'
TAG value: '(0,)'

TAG number: '256'
TAG name: 'ImageWidth'
TAG value: '(2388,)'

TAG number: '257'
TAG name: 'ImageLength'
TAG value: '(1651,)'

TAG number: '258'
TAG name: 'BitsPerSample'
TAG value: '(8,)'

TAG number: '259'
TAG name: 'Compression'
TAG value: '(1,)'

TAG number: '262'
TAG name: 'PhotometricInterpretation'
TAG value: '(1,)'

TAG number: '270'
TAG name: 'ImageDescription'
TAG value: '('{"shape": [1651, 2388]}',)'

TAG number: '274'
TAG name: 'Orientation'
TAG value: '(1,)'

TAG number: '277'
TAG name: 'SamplesPerPixel'
TAG value: '(1,)'

TAG number: '278'
TAG name: 'RowsPerStrip'
TAG value: '(1651,)'

TAG number: '282'
TAG name: 'XResolution'
TAG value: '((1, 1),)'

TAG number: '283'
TAG name: 'YResolution'
TAG value: '((1, 1),)'

TAG number: '284'
TAG name: 'PlanarConfiguration'
TAG value: '(1,)'

TAG number: '296'
TAG name: 'ResolutionUnit'
TAG value: '(1,)'

TAG number: '305'
TAG name: 'Software'
TAG value: '('tifffile.py',)'

TAG number: '339'
TAG name: 'SampleFormat'
TAG value: '(1,)'

TAG number: '33550'
TAG name: 'ModelPixelScaleTag'
TAG value: '(30.0, 30.0, 0.0)'

TAG number: '33922'
TAG name: 'ModelTiepointTag'
TAG value: '(0.0, 0.0, 0.0, 737125.0, 4328658.0, 0.0)'

TAG number: '34735'
TAG name: 'GeoKeyDirectoryTag'
TAG value: '(1, 1, 0, 15, 1024, 0, 1, 1, 1025, 0, 1, 1, 1026, 34737, 266, 0, 2048, 0, 1, 4269, 3072, 0, 1, 32767, 3073, 34737, 197, 266, 3074, 0, 1, 32767, 3075, 0, 1, 11, 3076, 0, 1, 9001, 3078, 34736, 1, 0, 3079, 34736, 1, 1, 3081, 34736, 1, 2, 3082, 34736, 1, 3, 3083, 34736, 1, 4, 3088, 34736, 1, 5)'

TAG number: '34736'
TAG name: 'GeoDoubleParamsTag'
TAG value: '(29.5, 45.5, 23.0, 0.0, 0.0, -96.0)'

TAG number: '34737'
TAG name: 'GeoAsciiParamsTag'
TAG value: '('IMAGINE GeoTIFF Support\nCopyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved\n@(#)$RCSfile: egtf.c $ $Revision: 1.11.2.3 $ $Date: 2004/11/24 09:12:56EST $\nProjection Name = USA_Contiguous_Albers_Equal_Area_Conic_USGS_version\nUnits = meters\nGeoTIFF Units = meters|IMAGINE GeoTIFF Support\nCopyright 1991 - 2001 by ERDAS, Inc. All Rights Reserved\n@(#)$RCSfile: egtf.c $ $Revision: 1.11.2.3 $ $Date: 2004/11/24 09:12:56EST $\nProjection = Albers Conical Equal Area|',)'
...

要单独更改投影标签,您当前必须手动编辑 GeoTiffDirectory 数据。

首先将(二进制)数据写入文件:

exiftool -geotiffdirectory -b FILE.tif > out.dat

然后找到例如的偏移量投影值,例如:

exiftool FILE.tif -v3 | grep -A 2 Projection -
| 9) Projection = 16017
| - Tag 0x0c02 (2 bytes, int16u[1]):
| 0056: 3e 91 [>.]

--> 编辑 out.dat 中的字节 0x56 和 0x57 以更改为您想要的任何投影,然后将修改后的 GeoTiff 目录放回到 TIFF 文件中:

exiftool "-geotiffdirectory<=out.dat" FILE.tif

尽管如此,值得注意的是,更改 3 个关键的总结 GeoTIFF 标签,而不是尝试通过修改上述二进制文件并将其重新分配给有问题的 TIFF 来操作单个 GeoTIFF 标签之一,可能会更有效。

为了完整起见,这 3 个 GeoTIFF 标签是:

  • ('34735') 'GeoKeyDirectoryTag'
  • ('34736') 'GeoDoubleParamsTag'
  • ('34737') 'GeoAsciiParamsTag'

尽管如此,仅更改二进制文件中的一个特定 GeoTIFF 标签也可能会更新新 TIFF 中的 3 个关键 GeoTIFF 摘要标签。我还没试过。

如果您需要添加一些对您有用的其他信息或经验,请告诉我。

干杯

关于python - 如何更改或删除 GeoTIFF 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59428868/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com