#!/usr/bin/env python # coding: utf-8 # Juypter notebooks have magic functions! one of them is the timeit. %timeit is great for single lines and %%timeit for multi lines # In[ ]: get_ipython().run_line_magic('timeit', 'data = [x * x for x in range(1000)]') # In[ ]: get_ipython().run_cell_magic('timeit', '', 'data = []\nfor x in range(1000):\n data.append(x * x)\n') # That makes a pretty convincing case for using list comprehension! What is the fastest way to read a satellite image into a numpy array? Lets test using a Landsat 8 image (Band 4) downloaded from AWS # https://landsat-pds.s3.amazonaws.com/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/index.html # In[ ]: image_path = "D:/Fastest_image/LC81390452014295LGN00_B4.TIF" ### set the path to your image choice # Import all the Python libraries. I recommend using a virtual python environment for running the tests, but it is not essential # In[ ]: ## method 1 - opencv import cv2 import numpy as np # Import GDAL method 2 from osgeo import gdal ## import method 3 - skimage from skimage import io ## import method 4 - rasterio ##import rasterio # In[ ]: get_ipython().run_cell_magic('timeit', '', '### Open CV approach\n\nimg = cv2.imread(image_path, -1) ### add your image here\nprint img.shape\n') # In[ ]: get_ipython().run_cell_magic('timeit', '', '## GDAL approach\n\nraster_ds = gdal.Open(image_path, gdal.GA_ReadOnly)\nimage_gdal = raster_ds.GetRasterBand(1).ReadAsArray()\nprint image_gdal.shape\n') # In[ ]: get_ipython().run_cell_magic('timeit', '', '## skimage\n\nim = io.imread(image_path)\nprint im.shape\n') # In[ ]: get_ipython().run_cell_magic('timeit', '', 'with rasterio.open(image_path) as r:\n arr = r.read() # read raster\n print(arr.shape)\n') # In my test (albeit just 1 single band Landsat 8 image) GDAL is the winner! # 4 different ways to read satellite images into numpy arrays # The case for using list comprehension to speed up code # GDAL is the fastest! # BUT use the most suited approach to your needs! # use the timeit%% for multi line testing and timeit% for single line testing # In[ ]: