How to correctly upload image and use it to generate video?

Hello, I am confused about file, uri and what else. I just want to feed the model with the first frame and the last frame. Do I need to manually upload the images to the cloud firstly? Thanks,

Lei

Here is my code:

import time

from google import genai

from google.genai.types import GenerateVideosConfig, Image

client = genai.Client(api_key=“xxx”)

prompt = “”“Based on the first and last keyframes, Generate a high-quality, photorealistic video of a woman using that product to cleanse her face in a luxurious bathroom setting. Ensure the generated video is a new, creative scene based on this product.”“”

# Load images using the correct method

first_frame = client.files.upload(file=“/Users/path/1.png”)

last_frame = client.files.upload(file=“/Users/path/video_generation_project/2.png”)

print(first_frame.uri)

operation = client.models.generate_videos(

model=“veo-3.1-generate-001”,

prompt=“a hand reaches in and places a glass of milk next to the plate of cookies”,

image=Image(

uri=first_frame.uri,

mime_type=“image/png”,

),

config=GenerateVideosConfig(

aspect_ratio=“16:9”,

last_frame=Image(

uri=last_frame.uri,

mime_type=“image/png”,

),

),

)

# 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)

# Download the generated video.

generated_video = operation.response.generated_videos[0]

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

generated_video.video.save(“my_video.mp4”)

print(“Generated video saved to my_video.mp4”)

Hii @lserlohn
Welcome to the Google AI Forum!!!

Thank you for reaching out to us.
If you would like to manually upload an image and then generate a video using that image, please try the following code. I have tested it on my end, and it works successfully.

from google import genai
from google.genai import types
import time

from google.colab import userdata
API_KEY = userdata.get('API_KEY')
client = genai.Client(api_key=API_KEY)


import pathlib
file_path = pathlib.Path('YOUR_IMAGE_PATH')
sample_file = client.files.upload(
  file=file_path,
)

with open(file_path, "rb") as f:
    image_bytes = f.read()

operation = client.models.generate_videos(
    model="veo-3.1-generate-preview"
    image={"image_bytes": image_bytes, "mime_type": "image/jpeg"})



# 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)

# Download the video.
video = operation.response.generated_videos[0]
client.files.download(file=video.video)
video.video.save("veo3_with_image_input.mp4")
print("Generated video saved to veo3_with_image_input.mp4")

If you wish to generate an image using a custom prompt, please refer this documentation for detailed instructions with code.

Thanks for your help. What if I want to generate a video, using two reference images as the first and last key frame of the video, and using a custom prompt to describe what to generate in between?

Hello,

I have shared a document with you, where you can see how to create videos using the first and last frames.

Thanks