feat(multimodal): Add customizable background color for RGBA to RGB conversion (#22052)

Signed-off-by: Jinheng Li <ahengljh@gmail.com>
Co-authored-by: Jinheng Li <ahengljh@gmail.com>
This commit is contained in:
Gamhang
2025-08-01 06:07:33 -07:00
committed by GitHub
co-authored by Jinheng Li
parent f81c1bb055
commit 0a6d305e0f
3 changed files with 190 additions and 6 deletions
+32 -5
View File
@@ -3,6 +3,7 @@
from io import BytesIO
from pathlib import Path
from typing import Union
import pybase64
import torch
@@ -23,9 +24,10 @@ def rescale_image_size(image: Image.Image,
return image
# TODO: Support customizable background color to fill in.
def rgba_to_rgb(
image: Image.Image, background_color=(255, 255, 255)) -> Image.Image:
image: Image.Image,
background_color: Union[tuple[int, int, int], list[int]] = (255, 255, 255)
) -> Image.Image:
"""Convert an RGBA image to RGB with filled background color."""
assert image.mode == "RGBA"
converted = Image.new("RGB", image.size, background_color)
@@ -55,10 +57,35 @@ class ImageMediaIO(MediaIO[Image.Image]):
# for flexible control.
self.kwargs = kwargs
# Extract RGBA background color from kwargs if provided
# Default to white background for backward compatibility
rgba_bg = kwargs.get('rgba_background_color', (255, 255, 255))
# Convert list to tuple for consistency
if isinstance(rgba_bg, list):
rgba_bg = tuple(rgba_bg)
# Validate rgba_background_color format
if not (isinstance(rgba_bg, tuple) and len(rgba_bg) == 3
and all(isinstance(c, int) and 0 <= c <= 255
for c in rgba_bg)):
raise ValueError(
"rgba_background_color must be a list or tuple of 3 integers "
"in the range [0, 255].")
self.rgba_background_color = rgba_bg
def _convert_image_mode(self, image: Image.Image) -> Image.Image:
"""Convert image mode with custom background color."""
if image.mode == self.image_mode:
return image
elif image.mode == "RGBA" and self.image_mode == "RGB":
return rgba_to_rgb(image, self.rgba_background_color)
else:
return convert_image_mode(image, self.image_mode)
def load_bytes(self, data: bytes) -> Image.Image:
image = Image.open(BytesIO(data))
image.load()
return convert_image_mode(image, self.image_mode)
return self._convert_image_mode(image)
def load_base64(self, media_type: str, data: str) -> Image.Image:
return self.load_bytes(pybase64.b64decode(data, validate=True))
@@ -66,7 +93,7 @@ class ImageMediaIO(MediaIO[Image.Image]):
def load_file(self, filepath: Path) -> Image.Image:
image = Image.open(filepath)
image.load()
return convert_image_mode(image, self.image_mode)
return self._convert_image_mode(image)
def encode_base64(
self,
@@ -77,7 +104,7 @@ class ImageMediaIO(MediaIO[Image.Image]):
image = media
with BytesIO() as buffer:
image = convert_image_mode(image, self.image_mode)
image = self._convert_image_mode(image)
image.save(buffer, image_format)
data = buffer.getvalue()