- 支持四种图片适配模式:stretch、contain、cover、center - 支持背景色填充功能(contain 和 center 模式) - 支持文档级 DPI 配置(metadata.dpi) - PPTX 渲染器集成 Pillow 实现高质量图片处理 - HTML 渲染器使用 CSS object-fit 实现相同效果 - 添加完整的单元测试、集成测试和端到端测试 - 更新 README 文档和架构文档 - 模块化设计:utils/image_utils.py 图片处理工具模块 - 添加图片配置验证器:validators/image_config.py - 向后兼容:未指定 fit 时默认使用 stretch 模式
121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
"""
|
||
单元测试:图片处理工具函数
|
||
|
||
测试 utils/image_utils.py 中的图片处理函数。
|
||
"""
|
||
|
||
import pytest
|
||
from PIL import Image
|
||
from utils.image_utils import (
|
||
inches_to_pixels,
|
||
pixels_to_inches,
|
||
apply_fit_mode,
|
||
create_canvas_with_background,
|
||
)
|
||
|
||
|
||
class TestUnitConversion:
|
||
"""测试单位转换函数"""
|
||
|
||
def test_inches_to_pixels_default_dpi(self):
|
||
"""测试英寸转像素(默认 96 DPI)"""
|
||
assert inches_to_pixels(1) == 96
|
||
assert inches_to_pixels(2) == 192
|
||
assert inches_to_pixels(0.5) == 48
|
||
|
||
def test_inches_to_pixels_custom_dpi(self):
|
||
"""测试英寸转像素(自定义 DPI)"""
|
||
assert inches_to_pixels(1, dpi=72) == 72
|
||
assert inches_to_pixels(2, dpi=150) == 300
|
||
assert inches_to_pixels(0.5, dpi=300) == 150
|
||
|
||
def test_pixels_to_inches_default_dpi(self):
|
||
"""测试像素转英寸(默认 96 DPI)"""
|
||
assert pixels_to_inches(96) == 1.0
|
||
assert pixels_to_inches(192) == 2.0
|
||
assert pixels_to_inches(48) == 0.5
|
||
|
||
def test_pixels_to_inches_custom_dpi(self):
|
||
"""测试像素转英寸(自定义 DPI)"""
|
||
assert pixels_to_inches(72, dpi=72) == 1.0
|
||
assert pixels_to_inches(300, dpi=150) == 2.0
|
||
|
||
|
||
class TestApplyFitMode:
|
||
"""测试图片适配模式函数"""
|
||
|
||
@pytest.fixture
|
||
def test_image(self):
|
||
"""创建测试图片(100x100)"""
|
||
return Image.new("RGB", (100, 100), color="red")
|
||
|
||
def test_stretch_mode(self, test_image):
|
||
"""测试 stretch 模式"""
|
||
result = apply_fit_mode(test_image, (200, 150), "stretch")
|
||
assert result.size == (200, 150)
|
||
|
||
def test_contain_mode_wider_box(self, test_image):
|
||
"""测试 contain 模式(box 更宽)"""
|
||
result = apply_fit_mode(test_image, (200, 100), "contain")
|
||
# 图片应该适配到高度,宽度保持比例
|
||
assert result.size == (100, 100)
|
||
|
||
def test_contain_mode_taller_box(self, test_image):
|
||
"""测试 contain 模式(box 更高)"""
|
||
result = apply_fit_mode(test_image, (100, 200), "contain")
|
||
# 图片应该适配到宽度,高度保持比例
|
||
assert result.size == (100, 100)
|
||
|
||
def test_cover_mode_wider_box(self, test_image):
|
||
"""测试 cover 模式(box 更宽)"""
|
||
result = apply_fit_mode(test_image, (200, 100), "cover")
|
||
# 图片应该覆盖整个 box
|
||
assert result.size == (200, 100)
|
||
|
||
def test_cover_mode_taller_box(self, test_image):
|
||
"""测试 cover 模式(box 更高)"""
|
||
result = apply_fit_mode(test_image, (100, 200), "cover")
|
||
# 图片应该覆盖整个 box
|
||
assert result.size == (100, 200)
|
||
|
||
def test_center_mode(self, test_image):
|
||
"""测试 center 模式"""
|
||
result = apply_fit_mode(test_image, (200, 150), "center")
|
||
# center 模式保持原始尺寸
|
||
assert result.size == (100, 100)
|
||
|
||
def test_invalid_mode(self, test_image):
|
||
"""测试无效的 fit 模式"""
|
||
with pytest.raises(ValueError, match="不支持的 fit 模式"):
|
||
apply_fit_mode(test_image, (200, 150), "invalid")
|
||
|
||
|
||
class TestCreateCanvas:
|
||
"""测试画布创建函数"""
|
||
|
||
def test_create_canvas_with_white_background(self):
|
||
"""测试创建白色背景画布"""
|
||
canvas = create_canvas_with_background((200, 150), "#ffffff")
|
||
assert canvas.size == (200, 150)
|
||
assert canvas.mode == "RGB"
|
||
# 检查中心像素是白色
|
||
assert canvas.getpixel((100, 75)) == (255, 255, 255)
|
||
|
||
def test_create_canvas_with_colored_background(self):
|
||
"""测试创建彩色背景画布"""
|
||
canvas = create_canvas_with_background((200, 150), "#ff0000")
|
||
assert canvas.size == (200, 150)
|
||
# 检查中心像素是红色
|
||
assert canvas.getpixel((100, 75)) == (255, 0, 0)
|
||
|
||
def test_create_canvas_with_short_hex(self):
|
||
"""测试创建画布(短格式十六进制颜色)"""
|
||
canvas = create_canvas_with_background((100, 100), "#f00")
|
||
# 检查中心像素是红色
|
||
assert canvas.getpixel((50, 50)) == (255, 0, 0)
|
||
|
||
def test_create_canvas_with_invalid_color(self):
|
||
"""测试无效颜色格式"""
|
||
with pytest.raises(ValueError, match="无效的颜色格式"):
|
||
create_canvas_with_background((100, 100), "invalid")
|