Skip to content

Instantly share code, notes, and snippets.

@prakharcode
Last active September 18, 2020 02:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakharcode/24fbb243fe06c842b51b1bc14d58d99f to your computer and use it in GitHub Desktop.
Save prakharcode/24fbb243fe06c842b51b1bc14d58d99f to your computer and use it in GitHub Desktop.
Resample using rasterio
import rasterio
from rasterio import Affine
from rasterio.warp import reproject, Resampling
with rasterio.open('path/to/band') as src:
scale = 2 # scaling amount of data so a scale of 2 would double the pixel size
aff = src.transform
# adjust the new affine transform to new scale
newaff = Affine(aff.a / scale, aff.b, aff.c,
aff.d, aff.e / scale, aff.f)
meta = src.profile
meta.update(affine = newaff)
with rasterio.open('path/to/output/destination', 'w', **meta) as dst:
reproject(source=rasterio.band(src, 1),
destination=rasterio.band(dst, 1),
resampling = Resampling.bilinear)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment