1

I have a single band GeoTIFF image which displays correctly in QGIS. However I tried viewing the same file using matplotlib and rasterio but I get a different output. Is there something that I am doing incorrectly? How do I display it like it is in QGIS? I would also like to use a colour map for representing the different values of the single band with a color. The EPSG of the GeoTIFF is 4326.

Here's my code:

import os
import rasterio
from rasterio.plot import show
from mpl_toolkits.basemap import Basemap
from matplotlib import pyplot as plt
from osgeo import gdal


if __name__ == "__main__":
    

    input_Dir = 'generated_46.tif'

  
    tiff_file = input_Dir
    raster = rasterio.open(tiff_file)
    print(raster.crs)

    bbox = raster.bounds

    show(raster.read(),cmap="gray")

Here's how it appears in qgis:

enter image description here

And this is my python output: enter image description here

Here is the GeoTIFF description: enter image description here enter image description here

  • @user2856 added – azis511 Jul 17 at 12:02
  • Check you min and max values! You should outspread them to 0-255 integer range. It is done by QGIS automatic. – Zoltan Jul 17 at 12:17
  • @Zoltan not sure how its done in Python. New to this. Could you help? – azis511 Jul 19 at 8:18
  • raster = raster / (max(raster) - min(raster)) * 256 – Zoltan Jul 19 at 9:32
0

This did it

raster = rasterio.open(tiff_file)
show(raster)

instead of this:

show(raster.read(1))

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.