File size: 1,581 Bytes
c640bc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Author: ishwor subedi
Date: 2023-12-27

"""
import os

from services import main_sys_logger
import cv2 as cv

main_sys_logger = main_sys_logger()


def get_frame_save_dir():
    """
    Create a directory for saving images from a video.

    :return:
    str: The path of the created or existing directory.
    """
    root_dir = 'images/cam_images'

    frame_dir = os.path.join(root_dir)
    if not os.path.exists(frame_dir):
        main_sys_logger.info(f"<<<<<<<<<<<<<<<<<< Folder created {frame_dir} >>>>>>>>>>>>>>>>>>>>")
        os.makedirs(frame_dir)
    return frame_dir


def imutil(image_path, output_path, new_size=None):
    """
    Resize an image using OpenCV.

    :param image_path: str, path to the input image file.
    :param output_path: str, path to save the resized image.
    :param new_size: tuple, (width, height) of the desired size.
    """
    try:
        image = cv.imread(image_path)

        if new_size is not None:
            image = cv.resize(image, new_size)

        cv.imwrite(output_path, image)
        main_sys_logger.info(f"Image saved to: {output_path}")
    except Exception as e:
        main_sys_logger.error(f"Error in imutil: {e}")


# Example usage:
if __name__ == "__main__":
    # Get the directory for saving frames
    frame_dir = get_frame_save_dir()

    # Example image path and output path
    input_image_path = 'path/to/your/input/image.jpg'
    output_image_path = os.path.join(frame_dir, 'resized_image.jpg')

    # Resize the image and save it
    imutil(input_image_path, output_image_path, new_size=(800, 600))