2025-05-22 18:59:18 -07:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-05-22 18:59:18 -07:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2025-08-01 21:07:33 +08:00
|
|
|
import pytest
|
2025-05-22 18:59:18 -07:00
|
|
|
from PIL import Image, ImageChops
|
|
|
|
|
|
2026-01-15 19:52:12 +08:00
|
|
|
from vllm.multimodal.image import convert_image_mode
|
2025-05-22 18:59:18 -07:00
|
|
|
|
2025-09-30 09:45:20 -04:00
|
|
|
pytestmark = pytest.mark.cpu_test
|
|
|
|
|
|
2025-05-22 18:59:18 -07:00
|
|
|
ASSETS_DIR = Path(__file__).parent / "assets"
|
|
|
|
|
assert ASSETS_DIR.exists()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_rgb_to_rgb():
|
|
|
|
|
# Start with an RGB image.
|
|
|
|
|
original_image = Image.open(ASSETS_DIR / "image1.png").convert("RGB")
|
|
|
|
|
converted_image = convert_image_mode(original_image, "RGB")
|
|
|
|
|
|
|
|
|
|
# RGB to RGB should be a no-op.
|
|
|
|
|
diff = ImageChops.difference(original_image, converted_image)
|
|
|
|
|
assert diff.getbbox() is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_rgba_to_rgb():
|
|
|
|
|
original_image = Image.open(ASSETS_DIR / "rgba.png")
|
|
|
|
|
original_image_numpy = np.array(original_image)
|
|
|
|
|
|
|
|
|
|
converted_image = convert_image_mode(original_image, "RGB")
|
|
|
|
|
converted_image_numpy = np.array(converted_image)
|
|
|
|
|
|
|
|
|
|
for i in range(original_image_numpy.shape[0]):
|
|
|
|
|
for j in range(original_image_numpy.shape[1]):
|
|
|
|
|
# Verify that all transparent pixels are converted to white.
|
|
|
|
|
if original_image_numpy[i][j][3] == 0:
|
|
|
|
|
assert converted_image_numpy[i][j][0] == 255
|
|
|
|
|
assert converted_image_numpy[i][j][1] == 255
|
|
|
|
|
assert converted_image_numpy[i][j][2] == 255
|