Search code examples
pythonimagescipytiffgeotiff

GeoTIFF issue with opening in PIL


Everytime I open an GeoTIFF image of a orthophoto in python (tried PIL, matplotlib, scipy, openCV) the image screws up. Some corners are beeing cropped , however the image remains its original shape. If I manually convert the tif to for instance a png in Photoshop and load it, it does work correctly. So it seems like PIL has some trouble handling tif files with objects that not fill the entire canvas. Does anyone have a solution for this problem?

Part of original Image:

Part of original Image

After opening:

After opening


Solution

  • It would have been really nice if you put the link of the figure that you are using (if it's free). I downloaded a sample GeoTIFF image from here, and I used gdal to open it.

    The shape of the geotiff.ReadAsArray() is (3, 1024, 2048) so I convert it to (1024, 2048, 3) (RGB) and open it with imshow:

    import gdal
    gdal.UseExceptions()
    import matplotlib.pyplot as plt
    import numpy as np
    
    geotiff = gdal.Open('/home/vafanda/Downloads/test.tif')
    geotiff_arr= geotiff.ReadAsArray()
    print np.shape(geotiff_arr)
    geotiff_shifted = np.rollaxis(geotiff_arr,0,3)
    print "Dimension converted to: "
    print np.shape(geotiff_shifted)
    plt.imshow(geotiff_shifted)
    plt.show()
    

    result:

    enter image description here