I couldn't find a way, so far, to change GeoTiff-TAGs in my TIFF-file. My OS is Linux Lubuntu 18.04 and I'm using Python 3.x. Just EXIF-TAGs were possible to change via various approaches.
For both reading and writing the EXIF-TAGs, I could use e.g.:
- command-line tool: exiftool
- python-module: exifread
- python-module: PIL
- python-module: tifffile
- python-module: skimage.external.tifffile
Especially the python-modules seem to be "blind on the eye" of GeoTiff-Tags, i.e. they read (and write) only the EXIF-TAGs, but not the GeoTiff-Tags. With exiftool, I can at least read and printout the GeoTiff-TAGs along with the other ones. For instance, a console output via exiftool of my TIFF-file, which includes the GeoTiff-Tags as well, would be:
Input:
exiftool -D -G -a -u -U -f "newfile.tif"
Output:
[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
Then, I tried like to change/delete e.g. the GeoTiff-TAG "Projection", which throws the following warning message without changing anything: Input:
exiftool "-Projection=" "newfile.tif"
Output:
Warning: Sorry, Projection is not writable. Nothing to do.
On my search on the internet I've found other libraries and packages, which perhaps could help me out, but so far no avail, as I didn't find documentation, or it didn't work:
- libtiff (via command tiffset, such as "tiffset -s Tag-Number Tag-Value dst-file", which changes/adds only EXIF-Tags, but doesn't touch the GeoTIFF-Tags)
- libgeotiff
- gdal_edit, gdal_translate (or gdal in general)
As for the latter approach, gdal_edit and gdal_translate, I tried the following with no effect (even though there was no error-message whatsoever):
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"
As you can see, in the example, I tried to modify the GeoTIFF-Tag number 3075, named "ProjCoordTrans" to a costum string. When carrying any of these 4 commands, no error was thrown, but checking afterwards the TIFF-Tags via
exiftool -D -G -a -u -U -f -s "newfile.tif"
I found the same TAG remained still unchanged.
EDIT - Trying out rasterio:
When trying to add a new TAG via the python library rasterio, as mentioned by Ryan in his answer below, it also doesn't resolve the issue as the newly added TAG is not assigned any TAG-number, and also rasterio captures only a few of the standard EXIF-TAGs, but none of the GeoTIFF-TAGs, wherefore unfortunately rasterio renders useless for this concern as well.
import rasterio
filename = '/home/andylu/Desktop/Models_Master/AERSURFACE/Input/Play_around_with_exiftools/playaround_file.tif'
savefilename = '/home/andylu/Desktop/Models_Master/AERSURFACE/Input/Play_around_with_exiftools/playaround_file_mod.tif'
with rasterio.open(filename, 'r') as src_ds:
tags = src_ds.tags()
print("\nTags BEFORE changing:\n{}".format(tags))
with rasterio.open(savefilename, 'w', **src_ds.meta) as dst_ds:
tags['NEWTAG'] = 'NEW VALUE'
dst_ds.update_tags(**tags)
dst_ds.write(src_ds.read())
with rasterio.open(savefilename, 'r') as src_ds:
tags = src_ds.tags()
print("\nTags AFTER changing:\n{}".format(tags))
Tags BEFORE changing:
{'3075': 'Custom', 'AREA_OR_POINT': 'Area', 'ProjCoordTrans': 'Custom', 'TIFFTAG_RESOLUTIONUNIT': '1 (unitless)', 'TIFFTAG_SOFTWARE': 'Custom Bla 2', 'TIFFTAG_XRESOLUTION': '1', 'TIFFTAG_YRESOLUTION': '1'}
Tags AFTER changing:
{'3075': 'Custom', 'AREA_OR_POINT': 'Area', 'NEWTAG': 'NEW VALUE', 'ProjCoordTrans': 'Custom', 'TIFFTAG_RESOLUTIONUNIT': '1 (unitless)', 'TIFFTAG_SOFTWARE': 'Custom Bla 2', 'TIFFTAG_XRESOLUTION': '1', 'TIFFTAG_YRESOLUTION': '1'}
Afterwards, double-check the changes via exiftool as follows (note the rather random allocation of the newly added tag "NEWTAG" in between the EXIF and GeoTIFF-tags):
(base) andylu@andylu-ThinkPad-Edge-E130:~/Desktop/Models_Master/AERSURFACE/Input/Play_around_with_exiftools$ exiftool -D -G -a -u -U -f playaround_file_mod.tif
[ExifTool] - ExifTool Version Number : 10.80
[File] - File Name : playaround_file_mod.tif
[File] - Directory : .
[File] - File Size : 1501 kB
[File] - File Modification Date/Time : 2020:01:10 18:01:06+01:00
[File] - File Access Date/Time : 2020:01:10 18:01:06+01:00
[File] - File Inode Change Date/Time : 2020:01:10 18:01:06+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)
[EXIF] 256 Image Width : 1148
[EXIF] 257 Image Height : 1337
[EXIF] 258 Bits Per Sample : 8
[EXIF] 259 Compression : Uncompressed
[EXIF] 262 Photometric Interpretation : BlackIsZero
[EXIF] 273 Strip Offsets : (Binary data 1387 bytes, use -b option to extract)
[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 : Custom Bla 2
[EXIF] 339 Sample Format : Unsigned
[EXIF] 33550 Pixel Scale : 30 30 0
[EXIF] 33922 Model Tie Point : 0 0 0 1514925 1583985 0
[EXIF] 42112 GDAL Metadata : <GDALMetadata>. <Item name="3075">Custom</Item>. <Item name="NEWTAG">NEW VALUE</Item>. <Item name="ProjCoordTrans">Custom</Item>.</GDALMetadata>.
[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 : USA_Contiguous_Albers_Equal_Area_Conic_USGS_version
[GeoTiff] 2048 Geographic Type : NAD83
[GeoTiff] 2049 Geog Citation : NAD83
[GeoTiff] 2054 Geog Angular Units : Angular Degree
[GeoTiff] 2057 Geog Semi Major Axis : 6378137
[GeoTiff] 2059 Geog Inv Flattening : 298.257222101004
[GeoTiff] 3072 Projected CS Type : User Defined
[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] 3080 Proj Nat Origin Long : -96
[GeoTiff] 3081 Proj Nat Origin Lat : 23
[GeoTiff] 3082 Proj False Easting : 0
[GeoTiff] 3083 Proj False Northing : 0
[Composite] - Image Size : 1148x1337
I'd like to know if there is a possibility to change GeoTiff-Tags. There must be a possibility.