feat: 添加图片适配模式支持
- 支持四种图片适配模式: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 模式
This commit is contained in:
@@ -89,6 +89,19 @@ class TestImageElement:
|
||||
assert elem.type == 'image'
|
||||
assert elem.src == "test.png"
|
||||
assert elem.box == [1, 1, 4, 3]
|
||||
assert elem.fit is None
|
||||
assert elem.background is None
|
||||
|
||||
def test_create_with_fit_and_background(self):
|
||||
"""测试创建带 fit 和 background 的 ImageElement"""
|
||||
elem = ImageElement(
|
||||
src="test.png",
|
||||
box=[1, 1, 4, 3],
|
||||
fit="contain",
|
||||
background="#ffffff"
|
||||
)
|
||||
assert elem.fit == "contain"
|
||||
assert elem.background == "#ffffff"
|
||||
|
||||
def test_empty_src_raises_error(self):
|
||||
"""测试空 src 会引发错误"""
|
||||
|
||||
120
tests/unit/test_image_utils.py
Normal file
120
tests/unit/test_image_utils.py
Normal file
@@ -0,0 +1,120 @@
|
||||
"""
|
||||
单元测试:图片处理工具函数
|
||||
|
||||
测试 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")
|
||||
136
tests/unit/test_validators/test_image_config.py
Normal file
136
tests/unit/test_validators/test_image_config.py
Normal file
@@ -0,0 +1,136 @@
|
||||
"""
|
||||
单元测试:图片配置验证器
|
||||
|
||||
测试 validators/image_config.py 中的验证函数。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from validators.image_config import (
|
||||
validate_fit_value,
|
||||
validate_background_color,
|
||||
validate_dpi_value,
|
||||
)
|
||||
|
||||
|
||||
class TestValidateFitValue:
|
||||
"""测试 fit 值验证"""
|
||||
|
||||
def test_valid_fit_values(self):
|
||||
"""测试有效的 fit 值"""
|
||||
assert validate_fit_value("stretch") == []
|
||||
assert validate_fit_value("contain") == []
|
||||
assert validate_fit_value("cover") == []
|
||||
assert validate_fit_value("center") == []
|
||||
|
||||
def test_none_fit_value(self):
|
||||
"""测试 None 值(默认)"""
|
||||
# None 是无效的,应该返回错误
|
||||
issues = validate_fit_value(None)
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_invalid_fit_value(self):
|
||||
"""测试无效的 fit 值"""
|
||||
issues = validate_fit_value("invalid")
|
||||
assert len(issues) > 0
|
||||
assert issues[0].level == "ERROR"
|
||||
|
||||
issues = validate_fit_value("fill")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_fit_value("scale")
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_case_sensitive(self):
|
||||
"""测试大小写敏感"""
|
||||
issues = validate_fit_value("STRETCH")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_fit_value("Contain")
|
||||
assert len(issues) > 0
|
||||
|
||||
|
||||
class TestValidateBackgroundColor:
|
||||
"""测试背景色验证"""
|
||||
|
||||
def test_valid_hex_colors(self):
|
||||
"""测试有效的十六进制颜色"""
|
||||
assert validate_background_color("#ffffff") == []
|
||||
assert validate_background_color("#000000") == []
|
||||
assert validate_background_color("#ff0000") == []
|
||||
assert validate_background_color("#4a90e2") == []
|
||||
|
||||
def test_valid_short_hex_colors(self):
|
||||
"""测试有效的短格式十六进制颜色"""
|
||||
assert validate_background_color("#fff") == []
|
||||
assert validate_background_color("#000") == []
|
||||
assert validate_background_color("#f00") == []
|
||||
|
||||
def test_none_background(self):
|
||||
"""测试 None 值(透明)"""
|
||||
# None 会导致 TypeError,应该返回错误
|
||||
issues = validate_background_color(None)
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_invalid_colors(self):
|
||||
"""测试无效的颜色格式"""
|
||||
issues = validate_background_color("white")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("rgb(255,255,255)")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("#gggggg")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("#ff")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("ffffff")
|
||||
assert len(issues) > 0
|
||||
|
||||
|
||||
class TestValidateDpiValue:
|
||||
"""测试 DPI 值验证"""
|
||||
|
||||
def test_valid_dpi_values(self):
|
||||
"""测试有效的 DPI 值"""
|
||||
assert validate_dpi_value(72) == []
|
||||
assert validate_dpi_value(96) == []
|
||||
assert validate_dpi_value(150) == []
|
||||
assert validate_dpi_value(300) == []
|
||||
|
||||
def test_boundary_dpi_values(self):
|
||||
"""测试边界 DPI 值"""
|
||||
# 1 和 1200 超出建议范围,会返回 WARNING
|
||||
issues = validate_dpi_value(1)
|
||||
assert len(issues) > 0
|
||||
assert issues[0].level == "WARNING"
|
||||
|
||||
issues = validate_dpi_value(1200)
|
||||
assert len(issues) > 0
|
||||
assert issues[0].level == "WARNING"
|
||||
|
||||
def test_invalid_dpi_values(self):
|
||||
"""测试无效的 DPI 值"""
|
||||
# 0 和负数会返回 WARNING
|
||||
issues = validate_dpi_value(0)
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(-1)
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(1201)
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(2000)
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_non_integer_dpi(self):
|
||||
"""测试非整数 DPI 值"""
|
||||
# 浮点数 DPI 可能被接受(取决于实现)
|
||||
# 字符串和 None 应该返回错误
|
||||
issues = validate_dpi_value("96")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(None)
|
||||
assert len(issues) > 0
|
||||
Reference in New Issue
Block a user