from pathlib import Path import cv2 as cv import numpy as np input_path = Path("input/scene.png") output_path = Path("output/scene-affine.png") image = cv.imread(str(input_path)) if image is None: raise SystemExit(f"Could not read {input_path}") height, width = image.shape[:2] source_points = np.float32([ [0, 0], [width - 1, 0], [0, height - 1], ]) target_points = np.float32([ [35, 20], [width - 45, 8], [75, height - 42], ]) matrix = cv.getAffineTransform(source_points, target_points) warped = cv.warpAffine( image, matrix, (width, height), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_REFLECT, ) output_path.parent.mkdir(parents=True, exist_ok=True) if not cv.imwrite(str(output_path), warped): raise SystemExit(f"Could not write {output_path}") print(f"source shape: {image.shape}") print("matrix:") print(np.array2string(matrix, precision=3, suppress_small=True)) print(f"output shape: {warped.shape}") print(f"wrote: {output_path}")