K230 How to save an image frame from a pipeline as a jpg file

Hello
I am trying to use the PipeLine module and save the frame as an image file.
Here is my code so far:

from libs.PipeLine import PipeLine
from media.sensor  import *
import ulab.numpy as np
display_mode="lcd2_4"
display_size=[640,480]
rgb888p_size=[320,320]
pl=PipeLine(rgb888p_size=rgb888p_size,display_size=display_size)#,display_mode=display_mode)
pl.create(Sensor(width=640, height=480)) 

time.sleep_ms(200)
clock = time.clock()

# Button
fpioa = FPIOA()
fpioa.set_function(21,FPIOA.GPIO21)
KEY=Pin(21,Pin.IN,Pin.PULL_UP)

num = 0
try:
    while True:
        clock.tick()
        img0=pl.get_frame()
        """
        img = sensor.snapshot() # Take photo
        Display.show_image(img) # Display photo
        """
        if KEY.value()==0: # Button pressed
            print(img0.shape)
            time.sleep_ms(10)
            print(f'KEY: photo number {num}')
            while not KEY.value(): # Wait for button release
                pass
            # Save photo
            filename = '/sdcard/' + str(num) + '.jpg'
            print(filename)
            img = image.Image(320, 320, image.JPEG, alloc=image.ALLOC_REF, data=img0)
            print(img)
            img.save(filename)
            num=num+1
            time.sleep(0.3)

except KeyboardInterrupt as e:
    print(f"user stop")
except BaseException as e:
    print(f"Exception '{e}'")

finally:
    pl.destroy()

The output from .get_frame() is an ulab.numpy.ndarray which doesn’t support the save method. This is why I’m trying another way, and create an image to put the array inside. But I obtain an error message:

Exception ‘image invalid size: 0’

How can I save the frame in a jpg file ? Thanks for your help.