Files
Neo-ZQYY/scripts/ops/resize_and_compare.py

78 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
统一两张截图宽度后进行像素级对比。
H5 截图: 1290px 宽 (430×3 DPR)
小程序截图: 宽度待检测,缩放到 1290px 后对比
"""
import sys
from pathlib import Path
from PIL import Image
SCREENSHOTS_DIR = Path(__file__).resolve().parents[2] / "docs" / "h5_ui" / "screenshots"
TARGET_WIDTH = 1290 # H5 截图宽度
def get_image_info(path: Path) -> dict:
img = Image.open(path)
return {"width": img.width, "height": img.height, "path": str(path)}
def resize_to_width(src: Path, dst: Path, target_width: int) -> dict:
"""等比缩放到目标宽度"""
img = Image.open(src)
if img.width == target_width:
img.save(dst)
return {"width": img.width, "height": img.height, "scaled": False}
ratio = target_width / img.width
new_height = int(img.height * ratio)
resized = img.resize((target_width, new_height), Image.LANCZOS)
resized.save(dst)
return {"width": target_width, "height": new_height, "scaled": True, "ratio": ratio}
def crop_to_same_height(img1_path: Path, img2_path: Path):
"""裁剪两张图到相同高度(取较小值)"""
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
min_h = min(img1.height, img2.height)
if img1.height != min_h:
img1.crop((0, 0, img1.width, min_h)).save(img1_path)
if img2.height != min_h:
img2.crop((0, 0, img2.width, min_h)).save(img2_path)
return min_h
def main():
h5_path = SCREENSHOTS_DIR / "board-finance.png"
mp_path = SCREENSHOTS_DIR / "mp-board-finance.png"
# 检查文件存在
for p in [h5_path, mp_path]:
if not p.exists():
print(f"❌ 文件不存在: {p}")
sys.exit(1)
# 获取尺寸信息
h5_info = get_image_info(h5_path)
mp_info = get_image_info(mp_path)
print(f"H5 截图: {h5_info['width']}×{h5_info['height']}")
print(f"小程序截图: {mp_info['width']}×{mp_info['height']}")
# 缩放小程序截图到 H5 宽度
mp_resized_path = SCREENSHOTS_DIR / "mp-board-finance--resized.png"
result = resize_to_width(mp_path, mp_resized_path, TARGET_WIDTH)
print(f"\n缩放后小程序截图: {result['width']}×{result['height']}")
if result.get('scaled'):
print(f" 缩放比例: {result['ratio']:.4f}")
# H5 也复制一份(确保宽度一致)
h5_resized_path = SCREENSHOTS_DIR / "h5-board-finance--resized.png"
h5_result = resize_to_width(h5_path, h5_resized_path, TARGET_WIDTH)
print(f"H5 截图确认: {h5_result['width']}×{h5_result['height']}")
# 裁剪到相同高度
min_h = crop_to_same_height(h5_resized_path, mp_resized_path)
print(f"\n统一高度: {min_h}px取较小值")
print(f"\n✅ 准备完成,可用 image-compare 对比:")
print(f" 图1: {h5_resized_path}")
print(f" 图2: {mp_resized_path}")
if __name__ == "__main__":
main()