How do I create a binary mask using Pixel values from a Numpy Array over a certain value (i.e. >1000), in Python [closed]

I would like to create a binary mask using the height values from a Digital Elevation Model, and export this out as a tif. For example, generate a mask for areas over 50m

My code so far:

from osgeo import gdal
import numpy as np
ds = gdal.Open("DEM.tif")

print(DEM.shape)
print(DEM.size)
print(DEM)

This results in reading the raster as an Array

The next step is for me to create a binary mask of values over, say, 1000m, and export this as a tif. file

closed as unclear what you're asking by BERA, whyzar, lynxlynxlynx, Dan C, MaryBethDec 5 '18 at 15:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• What have you tried so far? Have you looked at any tutorials and are stuck at a specific point of the workflow? – Erik Dec 5 '18 at 14:07
• @Erik, please see my edited post. I have read the raster file as a Numpy Array. I would now like to create a binary mask of the areas over 1000m – Rhodri_22 Dec 5 '18 at 15:02

You can create a raster mask using this:

from osgeo import gdal
import numpy as np
ds = gdal.Open("Norway_WD4O_Copy.tif")
ulx, xres, xskew, uly, yskew, yres =ds.GetGeoTransform()
band_ds = ds.GetRasterBand(1)

Mask_Zero = DEM < 1000 #In units of the raster, e.g. meters
Ones_array = np.ones((DEM.shape,DEM.shape),dtype=np.float32)