Record only from left camera using opencv

I am trying to record video from ZED left camera using openCV, so far I can only capture left image. But I need it to record live video either in mp4 or avi.
My script works if I use my webcam but it won’t work with zed camera. Below is my script, kindly note that I am still new to opencv and might need a detailed explanation on how to fix this

import sys
import as sl
import numpy as np
import cv2
import numpy
from pathlib import Path
import enum

def main():
# Create a Camera object
zed = sl.Camera()

# Create a InitParameters object and set configuration parameters
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD1080  # Use HD1080 video mode
init_params.camera_fps = 30  # Set fps at 30

init_params.svo_real_time_mode = False  # Don't convert in realtime

# Open the camera
err =
if err != sl.ERROR_CODE.SUCCESS:

runtime_parameters = sl.RuntimeParameters()

# Prepare new image size to retrieve half-resolution images
image_size = zed.get_camera_information().camera_resolution
image_size.width = image_size.width /2
image_size.height = image_size.height /2

width = image_size.width
height = image_size.height

i = 0

image = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)

left_image = sl.Mat()
img_rgba = np.zeros((height, width, 4), dtype=np.uint8)

#writer = cv2.VideoWriter('basicvideo.mp4', cv2.VideoWriter_fourcc(*'DIVX'), 20, (image_size.width, image_size.height))

video_writer = cv2.VideoWriter('basicvideo.avi',
                               cv2.VideoWriter_fourcc('M', '4', 'S', '2'),
                               max(zed.get_camera_information().camera_fps, 25),
                               (image_size.width, height))

while True:
    # Grab an image, a RuntimeParameters object must be given to grab()
    if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
        # A new image is available if grab() returns SUCCESS
        zed.retrieve_image(image, sl.VIEW.LEFT)
        img = image.get_data()
        cv2.imshow("Image", img)

        k = cv2.waitKey(1)

        img_out = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)

        if k % 256 == 27:
            # ESC pressed
            print("Close app")
        elif k % 256 == 32:
            # SPACE pressed
            img_name = 'img_cap_{}.jpg'.format(i)
            img = image.write(img_name)
            print("{} written!".format(img_name))
            i += 1

# Close the camera

if name == “main”:

Hello and thank you for reaching out to us,

You approach seems right. I can’t tell what’s wrong. You should try to add some logs, for example, to check that the data of img is right before you write it.

Antoine Lassagne
Senior Developer - ZED SDK
Stereolabs Support