import pathlib import numpy as np from PIL import Image root = pathlib.Path.cwd() image_path = root / 'transformed_image.png' report_path = root / 'transform_report.txt' assert image_path.exists() and image_path.stat().st_size > 0, 'missing transformed_image.png' assert report_path.exists() and report_path.stat().st_size > 0, 'missing transform_report.txt' img = Image.open(image_path) assert img.width > 0 and img.height > 0, 'transformed image has invalid dimensions' def box_blur_3x3(img): padded = np.pad(img.astype(np.float32), ((1, 1), (1, 1), (0, 0)), mode='edge') out = np.zeros_like(img, dtype=np.float32) for y in range(img.shape[0]): for x in range(img.shape[1]): out[y, x] = padded[y:y+3, x:x+3].mean(axis=(0, 1)) return np.rint(out).astype(np.uint8) src = np.array(Image.open(root / 'input.ppm').convert('RGB')) top, left = (src.shape[0] - 4) // 2, (src.shape[1] - 4) // 2 crop = src[top:top+4, left:left+4, :] flipped = crop[:, ::-1, :] blurred = box_blur_3x3(flipped) gray = np.rint(0.299 * blurred[:, :, 0] + 0.587 * blurred[:, :, 1] + 0.114 * blurred[:, :, 2]).astype(np.uint8) expected = np.repeat(gray[:, :, None], 3, axis=2) out = np.array(img.convert('RGB')) assert out.shape == expected.shape, f'expected 4x4 RGB output, got {out.shape}' diff = np.abs(out.astype(np.int16) - expected.astype(np.int16)) assert diff.max() <= 3, f'output pixels differ from expected pipeline; max diff {diff.max()}' report = report_path.read_text(encoding='utf-8', errors='ignore').lower() for alternatives in [('centercrop', 'center crop', '中心裁剪'), ('horizontalflip', 'horizontal_flip', 'horizontal flip', '水平翻转'), ('blur', '模糊'), ('gray', 'grey', '灰度')]: assert any(token in report for token in alternatives), f'report missing operation {alternatives}'