draw-point.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import annotations
  2. import sys
  3. from datetime import datetime
  4. from pathlib import Path
  5. from typing import Iterable, Sequence
  6. import cv2
  7. _REPOSITORY_ROOT_DIRECTORY = Path(__file__).resolve().parent.parent
  8. _FULL_SCREEN_SCREENSHOT_PNG_FILE_PATH = _REPOSITORY_ROOT_DIRECTORY / "output" / "screenshot.png"
  9. _POINT_DRAWING_OUTPUT_PARENT_DIRECTORY_PATH = _REPOSITORY_ROOT_DIRECTORY / "output"
  10. _GREEN_POINT_COLOR_BGR_TUPLE = (0, 255, 0)
  11. _POINT_CIRCLE_RADIUS_PIXELS_INTEGER = 6
  12. _POINT_CIRCLE_OUTLINE_THICKNESS_PIXELS_INTEGER = -1
  13. def draw_green_points_on_image_file_and_save_to_output_path(
  14. points_xy_coordinate_sequence: Sequence[Sequence[float]],
  15. input_image_file_path: str | Path,
  16. output_image_file_path: str | Path,
  17. ) -> Path:
  18. input_image_file_path_resolved = Path(input_image_file_path).resolve()
  19. output_image_file_path_resolved = Path(output_image_file_path).resolve()
  20. output_image_file_path_resolved.parent.mkdir(parents=True, exist_ok=True)
  21. input_image_bgr_numpy_array = cv2.imread(
  22. str(input_image_file_path_resolved),
  23. cv2.IMREAD_COLOR,
  24. )
  25. output_image_bgr_numpy_array = input_image_bgr_numpy_array.copy()
  26. points_iterable: Iterable[Sequence[float]] = points_xy_coordinate_sequence
  27. for point_xy_pair in points_iterable:
  28. center_x_integer = int(round(float(point_xy_pair[0])))
  29. center_y_integer = int(round(float(point_xy_pair[1])))
  30. cv2.circle(
  31. output_image_bgr_numpy_array,
  32. (center_x_integer, center_y_integer),
  33. _POINT_CIRCLE_RADIUS_PIXELS_INTEGER,
  34. _GREEN_POINT_COLOR_BGR_TUPLE,
  35. _POINT_CIRCLE_OUTLINE_THICKNESS_PIXELS_INTEGER,
  36. )
  37. cv2.imwrite(str(output_image_file_path_resolved), output_image_bgr_numpy_array)
  38. return output_image_file_path_resolved
  39. def draw_point_xy_on_current_screenshot_png_and_save_timestamped_png_file(
  40. points_xy_coordinate_sequence: Sequence[Sequence[float]],
  41. ) -> Path:
  42. timestamp_string_for_output_filename = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
  43. keypoints_preview_png_file_path = (
  44. _POINT_DRAWING_OUTPUT_PARENT_DIRECTORY_PATH
  45. / f"keypoints-{timestamp_string_for_output_filename}.png"
  46. )
  47. return draw_green_points_on_image_file_and_save_to_output_path(
  48. points_xy_coordinate_sequence,
  49. _FULL_SCREEN_SCREENSHOT_PNG_FILE_PATH,
  50. keypoints_preview_png_file_path,
  51. )
  52. def start(command_line_argument_string_list: list[str] | None = None) -> int:
  53. command_line_arguments = (
  54. command_line_argument_string_list
  55. if command_line_argument_string_list is not None
  56. else sys.argv[1:]
  57. )
  58. input_image_file_path_string = command_line_arguments[0].strip()
  59. output_image_file_path_string = command_line_arguments[1].strip()
  60. coordinate_float_string_list = command_line_arguments[2:]
  61. points_xy_coordinate_pair_list: list[list[float]] = []
  62. pair_index_integer = 0
  63. while pair_index_integer + 1 < len(coordinate_float_string_list):
  64. x_coordinate_float = float(coordinate_float_string_list[pair_index_integer].strip())
  65. y_coordinate_float = float(
  66. coordinate_float_string_list[pair_index_integer + 1].strip()
  67. )
  68. points_xy_coordinate_pair_list.append([x_coordinate_float, y_coordinate_float])
  69. pair_index_integer += 2
  70. draw_green_points_on_image_file_and_save_to_output_path(
  71. points_xy_coordinate_pair_list,
  72. input_image_file_path_string,
  73. output_image_file_path_string,
  74. )
  75. return 0
  76. def main() -> int:
  77. return start()
  78. __all__ = [
  79. "draw_green_points_on_image_file_and_save_to_output_path",
  80. "draw_point_xy_on_current_screenshot_png_and_save_timestamped_png_file",
  81. "start",
  82. "main",
  83. ]
  84. if __name__ == "__main__":
  85. raise SystemExit(main())