7

I know that it is possible to open a raster as an array in NumPy using GDAL, but I want to skip GDAL and use NumPy only, as it is cooler handling rasters with NumPy as matrices. There is a similar question here: but the answer solutions involve using other libraries.

Can you post the code here if you are aware of a NumPy workaround?

2
  • 3
    Is there a reason that your code needs to be 'cool' over 'functional using the tools you already are aware of'? Nov 8, 2013 at 22:54
  • 2
    @JasonScheirer Yes, there is. For example, instead of using GDAL projecting tools, you apply the map projection formulas directly on the matrix values. This way, you have control of what you are doing, because you can see the actual raster cells as matrix values, you understand the process deeply and you do it yourself. This is what I call "cool". Nov 9, 2013 at 13:54

3 Answers 3

17

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'>
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)
5
  • I've found PIL to be a bit unstable, so I swapped over to Wand, which is a wrapper for ImageMagick. +1
    – Paul
    Nov 9, 2013 at 15:46
  • Excellent well rounded answer. Thank you for providing a number of alternatives. Jun 1, 2015 at 19:41
  • If I open the same tiff in arc, pillow, and scipy, pillow and scipy give the same numpy array, but arc gives a different one for some reason. Sep 30, 2015 at 15:04
  • I was not able to use the PIL nor the scipy option, because it was not able to read my tif file. The opencv method returned an array missing the fourth layer/band. Only the gdal method gave a correct numpy array. To install gdal for python using pip, you can use: pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal"
    – Gert
    Nov 28, 2017 at 14:41
  • scipy.misc.imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead. docs.scipy.org/doc/scipy/reference/generated/…
    – crypdick
    Aug 8, 2018 at 20:02
5

I don't think it is possible just using numpy. If you don't want to use GDAL, you can use SciPy to read in the raster with scipy.misc.imread().

4
  • It's also possible if you are working in ArcPy by using arcpy.RasterToNumPyArray(). But again, this is using another library. So the answer to this question (to reiterate ustroetz and the link from the original question) appears to be "No, it is not possible to open a raster as array using just NumPy at this time". Perhaps this could be a requested feature for a future NumPy release?
    – Conor
    Nov 8, 2013 at 19:28
  • I am using Python 3, and misc doesn't seem to have a imread() method. Nov 8, 2013 at 19:41
  • As gene pointed out, SciPy should work with Python 3. Other options to read images with Python 3 can be found here.
    – ustroetz
    Nov 8, 2013 at 22:33
  • @multigoodverse imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.
    – crypdick
    Aug 8, 2018 at 20:01
1

Me, I find the format drivers of GDAL too useful to do without, so I wrapped them up in a simple package designed for use with Numpy: https://github.com/sgillies/rasterio. (Very early version, requires Cython, your mileage may vary.)

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.