|
| 1 | +from pathlib import Path |
| 2 | +import math |
| 3 | +from typing import Callable |
| 4 | + |
| 5 | +from PIL import Image, ImageDraw, ImageFont |
| 6 | +from geocompy.data import Coordinate, Angle, Vector |
| 7 | + |
| 8 | + |
| 9 | +def read_points( |
| 10 | + path: Path, |
| 11 | + skip: int = 0, |
| 12 | + delimiter: str = ";" |
| 13 | +) -> list[tuple[str, Coordinate]]: |
| 14 | + points: list[tuple[str, Coordinate]] = [] |
| 15 | + with path.open("rt", encoding="utf8") as file: |
| 16 | + for i in range(skip): |
| 17 | + next(file) |
| 18 | + |
| 19 | + for line in file: |
| 20 | + pt, x, y, z = line.strip().split(delimiter) |
| 21 | + points.append( |
| 22 | + ( |
| 23 | + pt, |
| 24 | + Coordinate( |
| 25 | + float(x), |
| 26 | + float(y), |
| 27 | + float(z) |
| 28 | + ) |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | + return points |
| 33 | + |
| 34 | + |
| 35 | +def read_metadata( |
| 36 | + path: Path |
| 37 | +) -> dict[str, tuple[Angle, Angle, Coordinate, Vector]]: |
| 38 | + pictures: dict[str, tuple[Angle, Angle, Coordinate, Vector]] = {} |
| 39 | + with path.open("rt", encoding="utf8") as file: |
| 40 | + next(file) |
| 41 | + for line in file: |
| 42 | + ( |
| 43 | + img, |
| 44 | + fov_hz, |
| 45 | + fov_v, |
| 46 | + pos_x, |
| 47 | + pos_y, |
| 48 | + pos_z, |
| 49 | + dir_x, |
| 50 | + dir_y, |
| 51 | + dir_z |
| 52 | + ) = line.strip().split(",") |
| 53 | + |
| 54 | + pictures[img] = ( |
| 55 | + Angle.parse(fov_hz), |
| 56 | + Angle.parse(fov_v), |
| 57 | + Coordinate( |
| 58 | + float(pos_x), |
| 59 | + float(pos_y), |
| 60 | + float(pos_z) |
| 61 | + ), |
| 62 | + Vector( |
| 63 | + float(dir_x), |
| 64 | + float(dir_y), |
| 65 | + float(dir_z) |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + return pictures |
| 70 | + |
| 71 | + |
| 72 | +def draw_marker_dot( |
| 73 | + draw: ImageDraw.ImageDraw, |
| 74 | + x: float, |
| 75 | + y: float, |
| 76 | + text: str, |
| 77 | + font: ImageFont.FreeTypeFont, |
| 78 | + markersize: float, |
| 79 | + rgb: tuple[int, int, int] |
| 80 | +) -> None: |
| 81 | + draw.circle((x, y), markersize / 2, rgb) |
| 82 | + draw.text( |
| 83 | + ( |
| 84 | + x + markersize / 3, |
| 85 | + y + markersize / 3 |
| 86 | + ), text, rgb, font, anchor="la" |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def draw_marker_cross( |
| 91 | + draw: ImageDraw.ImageDraw, |
| 92 | + x: float, |
| 93 | + y: float, |
| 94 | + text: str, |
| 95 | + font: ImageFont.FreeTypeFont, |
| 96 | + markersize: float, |
| 97 | + rgb: tuple[int, int, int] |
| 98 | +) -> None: |
| 99 | + leg = markersize / 2 |
| 100 | + draw.line( |
| 101 | + ( |
| 102 | + x - leg, y, |
| 103 | + x + leg, y |
| 104 | + ), |
| 105 | + rgb, |
| 106 | + round(markersize / 10) |
| 107 | + ) |
| 108 | + draw.line( |
| 109 | + ( |
| 110 | + x, y - leg, |
| 111 | + x, y + leg |
| 112 | + ), |
| 113 | + rgb, |
| 114 | + round(markersize / 10) |
| 115 | + ) |
| 116 | + draw.text( |
| 117 | + ( |
| 118 | + x + markersize / 4, |
| 119 | + y + markersize / 4 |
| 120 | + ), text, rgb, font, anchor="la" |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def annotate_image( |
| 125 | + imgpath: Path, |
| 126 | + info: tuple[Angle, Angle, Coordinate, Vector], |
| 127 | + points: list[tuple[str, Coordinate]], |
| 128 | + markerdrawer: Callable[[ImageDraw.ImageDraw, float, float, str], None] |
| 129 | +) -> None: |
| 130 | + image = Image.open(imgpath) |
| 131 | + draw = ImageDraw.Draw(image) |
| 132 | + fov_hz, fov_v, pos, vec = info |
| 133 | + half_width = image.width / 2 |
| 134 | + half_height = image.height / 2 |
| 135 | + half_fov_hz = fov_hz / 2 |
| 136 | + half_fov_v = fov_v / 2 |
| 137 | + |
| 138 | + img_hz, img_v, _ = (Coordinate.from_vector(vec) - pos).to_polar() |
| 139 | + |
| 140 | + for pt, coord in points: |
| 141 | + pt_hz, pt_v, _ = (coord - pos).to_polar() |
| 142 | + alpha = pt_hz.relative_to(img_hz) |
| 143 | + beta = pt_v.relative_to(img_v) |
| 144 | + |
| 145 | + x = round(half_width + math.tan(alpha) * |
| 146 | + half_width / math.tan(half_fov_hz)) |
| 147 | + y = round(half_height + math.tan(beta) * |
| 148 | + half_height / math.tan(half_fov_v)) |
| 149 | + |
| 150 | + markerdrawer(draw, x, y, pt) |
| 151 | + |
| 152 | + image.save( |
| 153 | + imgpath.parent.joinpath( |
| 154 | + imgpath.stem + "_annotated" + imgpath.suffix |
| 155 | + ) |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def run_annotate( |
| 160 | + meta: dict[str, tuple[Angle, Angle, Coordinate, Vector]], |
| 161 | + images: tuple[Path], |
| 162 | + points: list[tuple[str, Coordinate]], |
| 163 | + rgb: tuple[int, int, int] = (0, 0, 0), |
| 164 | + fontsize: int = 50, |
| 165 | + marker: str = "cross", |
| 166 | + markersize: int = 50 |
| 167 | +) -> None: |
| 168 | + font = ImageFont.truetype("arial.ttf", fontsize) |
| 169 | + match marker: |
| 170 | + case "cross": |
| 171 | + markerdrawer = draw_marker_cross |
| 172 | + case "dot": |
| 173 | + markerdrawer = draw_marker_dot |
| 174 | + |
| 175 | + for path in images: |
| 176 | + info = meta.get(path.stem + path.suffix) |
| 177 | + if info is None: |
| 178 | + continue |
| 179 | + |
| 180 | + annotate_image( |
| 181 | + path, |
| 182 | + info, |
| 183 | + points, |
| 184 | + lambda draw, x, y, text: markerdrawer( |
| 185 | + draw, |
| 186 | + x, |
| 187 | + y, |
| 188 | + text, |
| 189 | + font, |
| 190 | + markersize, |
| 191 | + rgb |
| 192 | + ) |
| 193 | + ) |
| 194 | + |
| 195 | + |
| 196 | +def main( |
| 197 | + metadata: Path, |
| 198 | + image: tuple[Path], |
| 199 | + action: str = "annotate", |
| 200 | + points: Path | None = None, |
| 201 | + skip: int = 0, |
| 202 | + delimiter: str = ",", |
| 203 | + rgb: tuple[int, int, int] = (0, 0, 0), |
| 204 | + fontsize: int = 50, |
| 205 | + marker: str = "cross", |
| 206 | + markersize: int = 50 |
| 207 | +) -> None: |
| 208 | + meta = read_metadata(metadata) |
| 209 | + match action: |
| 210 | + case "annotate" if points is not None: |
| 211 | + point = read_points(points, skip, delimiter) |
| 212 | + run_annotate( |
| 213 | + meta, |
| 214 | + image, |
| 215 | + point, |
| 216 | + rgb, |
| 217 | + fontsize, |
| 218 | + marker, |
| 219 | + markersize |
| 220 | + ) |
| 221 | + case _: |
| 222 | + raise ValueError(f"Unknown action '{action}'") |
0 commit comments