-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgrid.py
More file actions
53 lines (39 loc) · 1.79 KB
/
grid.py
File metadata and controls
53 lines (39 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging
import cv2 as cv2
from PIL import Image, ImageDraw, ImageFont
logger = logging.getLogger("stitcher")
def create_grid(images, output_file: str, stitched_scale: int):
logger.debug("Creating Grid")
# Calculate width and height based on the first image
width = images[0][0].shape[1] // stitched_scale
height = images[0][0].shape[0] // stitched_scale
# Calculate the total width and height of the final grid
total_width = width * len(images[0])
total_height = height * len(images)
# Create a new image with the calculated total dimensions
image = Image.new("RGB", (total_width, total_height))
# Loop through the rows and columns to place images
for y_num, row in enumerate(images):
for x_num, captured in enumerate(row):
if captured is None:
continue
# Calculate the position of the image in the final grid
image_pos = (x_num * width, y_num * height)
# Resize and convert the captured image to match the grid
segment = cv2.resize(
cv2.cvtColor(captured, cv2.COLOR_BGR2RGB), (width, height)
)
segment = Image.fromarray(segment, mode="RGB")
image.paste(segment, image_pos)
# Set up the drawing for adding text annotations
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", height // 20
)
for y_num, row in enumerate(images):
for x_num, captured in enumerate(row):
image_pos = (x_num * width, y_num * height)
# Determine if we should add custom machine coordinates
draw.text(image_pos, f"{y_num}, {x_num}", (255, 255, 255), font=font)
logger.debug("Saving Grid")
image.save(output_file)