Juypter notebooks have magic functions! one of them is the timeit. %timeit is great for single lines and %%timeit for multi lines
%timeit data = [x * x for x in range(1000)]
%%timeit
data = []
for x in range(1000):
data.append(x * x)
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
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
## 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
%%timeit
### Open CV approach
img = cv2.imread(image_path, -1) ### add your image here
print img.shape
%%timeit
## GDAL approach
raster_ds = gdal.Open(image_path, gdal.GA_ReadOnly)
image_gdal = raster_ds.GetRasterBand(1).ReadAsArray()
print image_gdal.shape
%%timeit
## skimage
im = io.imread(image_path)
print im.shape
%%timeit
with rasterio.open(image_path) as r:
arr = r.read() # read raster
print(arr.shape)
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