<div dir="ltr">Here is how I did finally :<div><br></div><div>    INPUTS:</div><div>    mask_geojson = [feature['geometry'] for feature in geojson['features']]</div><div>    filelike_in # my Geotiff file in-Memory (cStringIO.StringIO)</div><div><br></div><div><br></div><div><br></div><div><div>   # <a href="https://lists.osgeo.org/pipermail/gdal-dev/2010-May/024723.html" target="_blank">https://lists.osgeo.org/pipermail/gdal-dev/2010-May/024723.html</a></div><div>    # Create in-memory file and initialize it with the content</div><div>    gdal.FileFromMemBuffer('/vsimem/tiffinmem', filelike_in.read())</div><div><br></div><div>    # Open the in-memory file</div><div>    ds = gdal.Open('/vsimem/tiffinmem', gdal.GA_Update)</div><div><br></div><div>    # <a href="https://trac.osgeo.org/gdal/browser/trunk/autotest/utilities/test_gdal_rasterize_lib.py?rev=30939" target="_blank">https://trac.osgeo.org/gdal/browser/trunk/autotest/utilities/test_gdal_rasterize_lib.py?rev=30939</a></div><div>    # <a href="https://trac.osgeo.org/gdal/wiki/rfc59.1_utilities_as_a_library" target="_blank">https://trac.osgeo.org/gdal/wiki/rfc59.1_utilities_as_a_library</a></div><div>    # <a href="https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#spatial-filter" target="_blank">https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#spatial-filter</a></div><div>    vector_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0)</div><div>    layer = vector_ds.CreateLayer('layer')</div><div>    layer.GetLayerDefn()</div><div>    feature = ogr.Feature(layer.GetLayerDefn())</div><div>    geojson_str = json.dumps(mask_geojson[0])</div><div>    feature.SetGeometryDirectly(ogr.CreateGeometryFromJson(geojson_str))</div><div>    layer.CreateFeature(feature)</div><div><br></div><div>    ret = gdal.Rasterize(ds,</div><div>                        vector_ds,</div><div>                        bands = [1,2,3,4],</div><div>                        inverse = True,</div><div>                        burnValues = [0])</div><div>    # rasterio.mask.mask burns the RGB channels on top of the Alpha</div><div>    # -> this is not needed but results in a smaller png :</div><div>    # 56.7kB instead of 78.5 kB. Any change in the time required ?</div><div><br></div><div>    # transforming a GDAL-style shape to a standard-style shape</div><div>    np_array = np.rollaxis(ds.ReadAsArray(), 0, 3)</div><div><br></div><div>    filelike_out = cStringIO.StringIO()</div><div>    misc.imsave(filelike_out, np_array, 'png')</div><div><br></div><div>    # Close the dataset</div><div>    ds = None</div><div><br></div><div>    # Release memory associated to the in-memory file</div><div>    gdal.Unlink('/vsimem/tiffinmem')</div></div><div><br></div><div><br></div><div class="gmail_extra">Cheers</div></div>