File size: 1,626 Bytes
c9705bd
77f347a
 
 
4efd868
0a7de9a
77f347a
 
 
 
c9705bd
4efd868
0a7de9a
a8d1f41
 
 
 
 
 
0a7de9a
a8d1f41
 
c9705bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
from PIL import Image, ImageFilter,ImageDraw
import numpy as np
from logger import rich_logger as l
from ultralytics import YOLO
import cv2
import PIL.ImageOps






def invert_mask(mask_image: Image) -> np.ndarray:
    """Method to invert mask
    Args:
        mask_image (np.ndarray): input mask image
    Returns:
        np.ndarray: inverted mask image
    """
    inverted_mask_image =PIL.ImageOps.invert(mask_image)
    return inverted_mask_image





def extend_image(image_path, target_width, target_height, roi_scale=0.5):
    # Open the original image
    original_image = Image.open(image_path)

    # Get the dimensions of the original image
    original_width, original_height = original_image.size

    # Calculate the scale to fit the target resolution while keeping the aspect ratio
    scale = min(target_width / original_width, target_height / original_height)

    # Calculate the new dimensions of the image
    new_width = int(original_width * scale * roi_scale)
    new_height = int(original_height * scale * roi_scale)

    # Resize the original image with keeping the aspect ratio
    original_image_resized = original_image.resize((new_width, new_height))

    # Create a new image with white background
    extended_image = Image.new("RGB", (target_width, target_height), "white")

    # Calculate the position to paste the resized image at the center
    paste_x = (target_width - new_width) // 2
    paste_y = (target_height - new_height) // 2

    # Paste the resized image onto the new image
    extended_image.paste(original_image_resized, (paste_x, paste_y))

    return extended_image