@@ -184,6 +184,33 @@ def validate_image_result_response(response) -> None:
184184 raise KlingApiError (error_msg )
185185
186186
187+ def validate_input_image (image : torch .Tensor ) -> None :
188+ """
189+ Validates the input image adheres to the expectations of the Kling API:
190+ - The image resolution should not be less than 300*300px
191+ - The aspect ratio of the image should be between 1:2.5 ~ 2.5:1
192+
193+ See: https://app.klingai.com/global/dev/document-api/apiReference/model/imageToVideo
194+ """
195+ if len (image .shape ) == 4 :
196+ height , width = image .shape [1 ], image .shape [2 ]
197+ elif len (image .shape ) == 3 :
198+ height , width = image .shape [0 ], image .shape [1 ]
199+ else :
200+ raise ValueError ("Invalid image tensor shape." )
201+
202+ # Ensure minimum resolution is met
203+ if height < 300 :
204+ raise ValueError ("Image height must be at least 300px" )
205+ if width < 300 :
206+ raise ValueError ("Image width must be at least 300px" )
207+
208+ # Ensure aspect ratio is within acceptable range
209+ aspect_ratio = width / height
210+ if aspect_ratio < 1 / 2.5 or aspect_ratio > 2.5 :
211+ raise ValueError ("Image aspect ratio must be between 1:2.5 and 2.5:1" )
212+
213+
187214def get_camera_control_input_config (
188215 tooltip : str , default : float = 0.0
189216) -> tuple [IO , InputTypeOptions ]:
@@ -530,7 +557,10 @@ def INPUT_TYPES(s):
530557 return {
531558 "required" : {
532559 "start_frame" : model_field_to_node_input (
533- IO .IMAGE , KlingImage2VideoRequest , "image"
560+ IO .IMAGE ,
561+ KlingImage2VideoRequest ,
562+ "image" ,
563+ tooltip = "The reference image used to generate the video." ,
534564 ),
535565 "prompt" : model_field_to_node_input (
536566 IO .STRING , KlingImage2VideoRequest , "prompt" , multiline = True
@@ -607,9 +637,10 @@ def api_call(
607637 auth_token : Optional [str ] = None ,
608638 ) -> tuple [VideoFromFile ]:
609639 validate_prompts (prompt , negative_prompt , MAX_PROMPT_LENGTH_I2V )
640+ validate_input_image (start_frame )
610641
611642 if camera_control is not None :
612- # Camera control type for image 2 video is always simple
643+ # Camera control type for image 2 video is always ` simple`
613644 camera_control .type = KlingCameraControlType .simple
614645
615646 initial_operation = SynchronousOperation (
0 commit comments