vkit.mechanism.distortion.geometric.grid_rendering.visualization

 1# Copyright 2022 vkit-x Administrator. All Rights Reserved.
 2#
 3# This project (vkit-x/vkit) is dual-licensed under commercial and SSPL licenses.
 4#
 5# The commercial license gives you the full rights to create and distribute software
 6# on your own terms without any SSPL license obligations. For more information,
 7# please see the "LICENSE_COMMERCIAL.txt" file.
 8#
 9# This project is also available under Server Side Public License (SSPL).
10# The SSPL licensing is ideal for use cases such as open source projects with
11# SSPL distribution, student/academic purposes, hobby projects, internal research
12# projects without external distribution, or other projects where all SSPL
13# obligations can be met. For more information, please see the "LICENSE_SSPL.txt" file.
14from typing import Optional
15from PIL import ImageDraw as PilImageDraw
16
17from vkit.element import Image, ImageMode
18from .type import ImageGrid
19from .grid_blender import create_image_from_image_grid
20
21
22def visualize_image_grid(
23    image_grid: ImageGrid,
24    image: Optional[Image] = None,
25    line_color: str = 'black',
26    show_index: bool = False,
27    index_color: str = 'red',
28):
29    if not image:
30        image = create_image_from_image_grid(image_grid, ImageMode.RGB)
31        with image.writable_context:
32            image.mat.fill(255)
33
34    pil_image = image.to_pil_image()
35    draw = PilImageDraw.Draw(pil_image)
36
37    for row in range(image_grid.num_rows):
38        for col in range(image_grid.num_cols):
39            point0 = image_grid.points_2d[row][col]
40
41            draw_left_right = False
42            if col < image_grid.num_cols - 1:
43                # Draw left-right line.
44                point1 = image_grid.points_2d[row][col + 1]
45                draw.line([(point0.x, point0.y), (point1.x, point1.y)], fill=line_color)
46                draw_left_right = True
47
48            draw_up_down = False
49            if row < image_grid.num_rows - 1:
50                # Draw up-down line.
51                point1 = image_grid.points_2d[row + 1][col]
52                draw.line([(point0.x, point0.y), (point1.x, point1.y)], fill=line_color)
53                draw_up_down = True
54
55            if draw_left_right and draw_up_down and show_index:
56                draw.text((point0.x, point0.y), f'{row},{col}', fill=index_color)
57
58    return Image.from_pil_image(pil_image)
def visualize_image_grid( image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, image: Union[vkit.element.image.Image, NoneType] = None, line_color: str = 'black', show_index: bool = False, index_color: str = 'red'):
23def visualize_image_grid(
24    image_grid: ImageGrid,
25    image: Optional[Image] = None,
26    line_color: str = 'black',
27    show_index: bool = False,
28    index_color: str = 'red',
29):
30    if not image:
31        image = create_image_from_image_grid(image_grid, ImageMode.RGB)
32        with image.writable_context:
33            image.mat.fill(255)
34
35    pil_image = image.to_pil_image()
36    draw = PilImageDraw.Draw(pil_image)
37
38    for row in range(image_grid.num_rows):
39        for col in range(image_grid.num_cols):
40            point0 = image_grid.points_2d[row][col]
41
42            draw_left_right = False
43            if col < image_grid.num_cols - 1:
44                # Draw left-right line.
45                point1 = image_grid.points_2d[row][col + 1]
46                draw.line([(point0.x, point0.y), (point1.x, point1.y)], fill=line_color)
47                draw_left_right = True
48
49            draw_up_down = False
50            if row < image_grid.num_rows - 1:
51                # Draw up-down line.
52                point1 = image_grid.points_2d[row + 1][col]
53                draw.line([(point0.x, point0.y), (point1.x, point1.y)], fill=line_color)
54                draw_up_down = True
55
56            if draw_left_right and draw_up_down and show_index:
57                draw.text((point0.x, point0.y), f'{row},{col}', fill=index_color)
58
59    return Image.from_pil_image(pil_image)