Image to Video Veo3 API

HI ,
I’m currently building a web app in Visual Studio that uses the Veo 3 API to generate videos from images. However, I’m running into an issue—every time I try to pass an uploaded image to the API, I get an error. I’m new to this field and would really appreciate any guidance or help from someone more experienced.
def generate_video(prompt,Image_path):

client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))

uploaded_file = genai.upload_file (

    Image_path=Path(Image_path),

    mime_type="image/png",

    display_name="renderinput"  # Valid name

   \# converting the image to bytes



)





operation = client.models.generate_videos(

    model="veo-3.0-generate-001",

    prompt=prompt,

    image= uploaded_file,

    config={

        "aspect_ratio": "16:9",

        "negative_prompt": "cartoon, drawing, low quality",

        "resolution":720,

    },

)



\# Poll the operation status until the video is ready.

while not operation.done:

    print("Waiting for video generation to complete...")

    time.sleep(10)

    operation = client.operations.get(operation)

    print(operation)



\# Download the generated video.

generated_video = operation.response.generated_videos\[0\]

client.files.download(file=generated_video.video)

generated_video.video.save("example.mp4")

print("Generated video saved to example.mp4")

return "example.mp4"  # Return the path of the generated video

if _name_ == “_main_”:

prompt = (

    "camera Pan around the building "

    

)

with gr.Blocks(theme=gr.themes.Soft()) as demo:

with gr.Row():

    input_img = gr.Image(label="Upload Your Sketch", type="pil")



gr.Markdown("# Veo Video Generator\\nEnter your prompt and generate a video!")

prompt = gr.Textbox(

    label="Prompt",

    value="camera Pan around the building.",



    

)

generate_btn = gr.Button("Generate Video")

video_output = gr.Video(label="Generated Video")



def on_generate(prompt):

    video_path = generate_video(prompt)

    if video_path:

        return gr.update(value=video_path)

    else:

        return gr.update(value=None)



generate_btn.click(on_generate, inputs=prompt, outputs=video_output)

if _name_ == “_main_”:

demo.launch(

    

)   

2 Likes

Hi @yahia_Omar
The issue is with the logic and structure of the code . You can fix it by doing these below changes.
1.Modify on_generate to accept the uploaded image from gr.Image.

2. Save the received PIL Image to a temporary file.

3. Pass the path of this temporary file to generate_video.

4. Correct the argument name for genai.upload_file to path

Please try these changes and let me know if it works . Thank you

1 Like

Thank you .appreciate

I think you should load the image like this

with open(local_image, “rb”) as f:
    img_bytes = f.read()

image = types.Part.from_bytes(
data=img_bytes,
mime_type=“image/jpeg”,  # or “image/png”
).as_image()