Skip to content

Latest commit

 

History

History
88 lines (59 loc) · 2.59 KB

Is it possible to open rasters as array in NumPy without using another library?.md

File metadata and controls

88 lines (59 loc) · 2.59 KB

from Is it possible to open rasters as array in NumPy without using another library?

Numpy is made for processing arrays and not for reading image files. You need other modules to read the raster and convert it to an array.

If you do not want use GDAL or ArcPy:

    from scipy import misc
    raster = misc.imread('image.tif')
    type(raster)
    <type 'numpy.ndarray'>

from Is it possible to open rasters as array in NumPy without using another library?

    import Image
    import numpy as np
    raster =Image.open('image.tif')
    print raster.format, raster.size, raster.mode, raster.info
    TIFF (330, 440) P {'compression': 'raw', 'dpi': (300, 300)   
    imarray=np.array(raster)
    type(imarray)
    <type 'numpy.ndarray'>
  • you can also use matplotlib, only png file natively, using PIL for the others
 

    import matplotlib.pyplot as plt
    imarray = plt.imread('image.tif')
    type(imarray)
    <type 'numpy.ndarray'>
    import cv2
    im = cv2.imread("image.tif")
    type(im)
    <type 'numpy.ndarray'>

If you use Python 3.3 you can use Pillow or the last version of Scipy (> 0.12)

enter image description here

But you have no information about the georeferencing parameters of the raster

    from osgeo import gdal
    raster = gdal.Open("image.tif")
    imarray = np.array(raster.ReadAsArray())
    type(imarray)
    <type 'numpy.ndarray'>
    # georeferencing parameters 
    geotransform = raster.GetGeoTransform()
    print geotransform
    (162012.67788132755, 1.00078911763392, 0.0, 108172.86938540942, 0.0, -1.00078911763392)