Python code to calculate ndvi with esa_snappy

Hello to everybody:

I’m new with the processing with esa_snappy. I want to calculate ndvi and I was following this example: Tutorial
This is my code:

from esa_snappy import Product, ProductIO, ProductData, ProductUtils, jpy, GPF

path_image_zip = <path to your directory> # path to directory with the .zip products
    
    imagenes_zip = glob.glob(os.path.join(path_image_zip, '*.zip')) # read the product with .zip
    
    imagenes_zip[0] # first product, just for test

    producto = ProductIO.readProduct(imagenes_zip[0])
    
    list(producto.getBandNames())
    
    HashMap = jpy.get_type('java.util.HashMap')
    Integer = jpy.get_type('java.lang.Integer') 
    
    parameters = HashMap()
    
    parameters.put('targetResolution', Integer(10))
    
    producto_10m = GPF.createProduct('Resample', parameters, producto)
    
    scene_width = producto.getSceneRasterWidth()  
    scene_height = producto.getSceneRasterHeight() 
    
    
    # bands
    b4 = producto.getBand('B4')
    b8 = producto.getBand('B8')
        
    producto = Product('indice_ndvi', 'indice', scene_width, scene_height)
    
    band_index = producto.addBand('indice_ndvi', ProductData.TYPE_FLOAT32)
    
    writer = ProductIO.getProductWriter('Geotiff')
    
    ProductUtils.copyGeoCoding(producto, producto) 
    
    producto.setProductWriter(writer) 
    
    producto.writeHeader('indice_ndvi')
    
    r4 = np.zeros(scene_width, dtype=np.float32) 
    r8 = np.zeros(scene_width, dtype=np.float32)
    
    for y in range(scene_height):
        
        r4 = b4.readPixels(0, y, scene_width, 1, r4) 
        r8 = b8.readPixels(0, y, scene_width, 1, r8)
        
        indice_calculado =  (r8 - r4) / ( r8 + r4 )
        
        band_index.writePixels(0, y, scene_width, 1, indice_calculado)  
    
    producto.closeIO()

Can somebody help me with the code, if is ok or wrong in some part, even with some recomendations using memory.

Thanks in advance.

I could use this code and the result is the index.

If somebody wants to use this code, it´s gonna work. Al least the main idea.

It woked for me.