Python – How to Read .tif Image Coordinates

geotiff-tiffpython

I want to read the coordinates of a .tif file in order to use them inside Python code. Although gdalinfo command on Ubuntu terminal displays a lot of information, it is useless since I cannot use them inside larger Python code, unless I put them manually, which does not work on my occasion. I found and use this code:

import exifread
# Open image file for reading (binary mode)
f = open('image.tif', 'rb')

# Return Exif tags
tags = exifread.process_file(f)

# Print the tag/ value pairs
for tag in tags.keys():
    if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
        print("Key: %s, value %s" % (tag, tags[tag]))

from here: https://stackoverflow.com/questions/46477712/reading-tiff-image-metadata-in-python

The problem that I face is that I cannot get all the coordinates or at least 2 critical of them (such as upper left corner and right down corner), so that I can calculate all 4. What I get with the above mentioned code is only the upper left corner's coordinates… How can I fix that? How can I get and the right down corner's coordinates?

Best Answer

Rasterio would be an easy choice to get the information you need via Python. This code gets the bounding geometry of a geotiff.

import rasterio
dataset = rasterio.open('/home/gerry/Drone/MedTest/referencedimages/warped_DJI_0001.TIF')
#left edge
print(dataset.bounds[0])
#bottom edge
print(dataset.bounds[1])
#right edge
print(dataset.bounds[2])
#top edge
print(dataset.bounds[3])
Related Question