Fix ltx2 i2av some input image failed#1111
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors image decoding in decode_image to convert images to RGB format and simplifies the array conversion. It also adds input validation and conversion logic in encode_single_frame to handle 2D grayscale images and enforce an RGB shape requirement. The review feedback suggests using a context manager with Image.open to prevent potential resource leaks and extending the grayscale handling to support 3D single-channel arrays of shape (H, W, 1).
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| image = Image.open(image_path).convert("RGB") | ||
| return np.array(image) |
There was a problem hiding this comment.
Using Image.open without a context manager can leave the file handle open until the image object is garbage collected, which may lead to resource leaks (e.g., 'Too many open files' error) when processing many images. It is recommended to use a with statement to ensure the file is closed immediately after loading.
| image = Image.open(image_path).convert("RGB") | |
| return np.array(image) | |
| with Image.open(image_path) as image: | |
| return np.array(image.convert('RGB')) |
| if image_array.ndim == 2: | ||
| image_array = np.stack([image_array, image_array, image_array], axis=-1) | ||
| elif image_array.ndim != 3 or image_array.shape[-1] != 3: | ||
| raise ValueError(f"Expected HxWx3 RGB image; got shape {image_array.shape}.") |
There was a problem hiding this comment.
Grayscale images can sometimes be represented with a single channel dimension (e.g., shape (H, W, 1)). To make this function more robust, we can handle both 2D grayscale arrays and 3D single-channel arrays by squeezing the channel dimension before stacking.
| if image_array.ndim == 2: | |
| image_array = np.stack([image_array, image_array, image_array], axis=-1) | |
| elif image_array.ndim != 3 or image_array.shape[-1] != 3: | |
| raise ValueError(f"Expected HxWx3 RGB image; got shape {image_array.shape}.") | |
| if image_array.ndim == 3 and image_array.shape[-1] == 1: | |
| image_array = np.squeeze(image_array, axis=-1) | |
| if image_array.ndim == 2: | |
| image_array = np.stack([image_array, image_array, image_array], axis=-1) | |
| elif image_array.ndim != 3 or image_array.shape[-1] != 3: | |
| raise ValueError(f'Expected HxWx3 RGB image; got shape {image_array.shape}.') |
No description provided.