Reputation: 193
I want to make a subset of a raster image and put it in the dimension 800x600. I was looking through the Rasterio cookbook but it doesn't seem to allow me to input dimensions such as 800x600. Here is what I've been looking at: https://mapbox.s3.amazonaws.com/playground/perrygeo/rasterio-docs/cookbook.html
also, I saw this and thought it might work: https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html
I used the Reader code snippet:
import rasterio
with rasterio.open('MyRasterImage.tif') as src:
w = src.read(1, window=Window(0, 0, 800, 600))
print(w.shape)
However, when I go to run it it gives me the error message:
w = src.read(1, window = Window(0, 0, 800, 600))
NameError: name 'Window' is not defined
Not sure what is causing this error. I was thinking Windows was a built-in function in rasterio to where I could simply call it and resize the image to make a subset.
I'd also like to be able to display the new 800x600 image on screen (using Spyder) not sure how this is done.
Any and all help would be greatly appreciated and will upvote.
Thank you
Upvotes: 1
Views: 5252
Reputation: 202
import numpy
import rasterio
from matplotlib import pyplot
from rasterio.windows import Window
width = 800
height = 600
with rasterio.open('MyRasterImage.tif') as src:
w = src.read(1, window=Window(0, 0, width, height))
profile = src.profile
profile['width'] = width
profile['height'] = height
# Create output
result = numpy.full((width, height), dtype=profile['dtype'], fill_value=profile['nodata'])
#writting
with rasterio.open('/tmp/sampled_image.tif', 'w', **profile) as dataset:
dataset.write_band(1, result)
#plotting
with rasterio.open('/tmp/sampled_image.tif') as src:
pyplot.imshow(src.read(1), cmap='pink')
pyplot.show()
Upvotes: 1