Convert a mask into a Polygon for images using shapely and rasterio

Sometimes it is necessary to transform masks into polygons to use polygon operations. The way to transform a raster or a binary mask into a polygon is pretty easy. Most of the solutions I found online were the other way (from polygon to a mask). But for my project I needed to convert the mask to a polygon.

Newsletter subscription


One Reply to “Convert a mask into a Polygon for images using shapely and rasterio”

  1. Brian White says:

    Thanks for posting this, Roc.
    I think the line:
    return shapely.geometry.shape(shape)
    should be commented out (and was perhaps intended for debugging?). It has the effect of returning just the first polygon amongst several polygons constituting a disconnected mask.

Leave a Reply

Your email address will not be published. Required fields are marked *