Files
2026-09-29 00:17:58 +03:00

74 lines
2.9 KiB
Python
Raw Permalink 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.
"""Синтетические изображения для тестов (docs/08 §8.2, §8.6).
Реальных фото в git нет — только детерминированная генерация OpenCV/NumPy.
"""
from __future__ import annotations
import cv2
import numpy as np
def make_facade(seed: int = 7, width: int = 1400, height: int = 900) -> np.ndarray:
"""Текстурированный «фасад»: сетка окон со случайными цветами + швы + шум."""
rng = np.random.default_rng(seed)
img = np.full((height, width, 3), 118, np.uint8)
for y in range(0, height, 28): # кладка
cv2.line(img, (0, y), (width, y), (132, 128, 122), 1)
for y in range(40, height - 160, 140):
for x in range(60, width - 200, 190):
col = tuple(int(c) for c in rng.integers(28, 225, 3))
cv2.rectangle(img, (x, y), (x + 110, y + 95), col, -1)
cv2.rectangle(img, (x, y), (x + 110, y + 95), (205, 198, 170), 3)
cv2.circle(img, (x + 55, y + 47), 8, (240, 240, 235), 2)
return cv2.add(img, rng.integers(0, 16, img.shape, dtype=np.uint8))
def encode_jpg(img: np.ndarray, quality: int = 92) -> bytes:
ok, buf = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, quality])
if not ok:
raise RuntimeError("не удалось закодировать JPEG")
return buf.tobytes()
def warp_perspective(img: np.ndarray) -> np.ndarray:
"""Проективное искажение — имитация смены ракурса (UT-02)."""
h, w = img.shape[:2]
src = np.float32([[0, 0], [w, 0], [w, h], [0, h]])
dst = np.float32(
[[w * 0.12, h * 0.10], [w * 0.95, h * 0.04], [w * 0.90, h * 0.97], [w * 0.05, h * 0.90]]
)
m = cv2.getPerspectiveTransform(src, dst)
return cv2.warpPerspective(img, m, (w, h), flags=cv2.INTER_LINEAR)
def scale_half(img: np.ndarray) -> np.ndarray:
h, w = img.shape[:2]
return cv2.resize(img, (w // 2, h // 2), interpolation=cv2.INTER_AREA)
def crop_center(img: np.ndarray, fraction: float = 0.45) -> np.ndarray:
"""Фрагмент фасада — частичная видимость (UT-06)."""
h, w = img.shape[:2]
ch, cw = int(h * fraction), int(w * fraction)
y0, x0 = (h - ch) // 2, (w - cw) // 2
return img[y0 : y0 + ch, x0 : x0 + cw]
def make_flat(width: int = 700, height: int = 520) -> np.ndarray:
"""Однотонная заливка — «бедная» текстура (UT-07)."""
return np.full((height, width, 3), 128, np.uint8)
def encode_exif_oriented(img: np.ndarray, orientation: int = 6) -> bytes:
"""JPEG с EXIF-ориентацией (UT-09): 6 = поворот на 90° по часовой."""
import io
from PIL import Image
pil = Image.fromarray(img)
exif = Image.Exif()
exif[274] = orientation # Orientation
buffer = io.BytesIO()
pil.save(buffer, format="JPEG", exif=exif)
return buffer.getvalue()