File size: 5,214 Bytes
ece449a
 
 
e377cdc
4dbf90f
 
5406cd8
9a7adef
 
 
 
 
 
 
 
 
ece449a
5406cd8
ece449a
 
5406cd8
ece449a
 
5406cd8
ece449a
 
5406cd8
ece449a
 
 
 
 
 
 
 
e377cdc
9a7adef
 
 
 
 
 
 
 
 
ece449a
 
 
e377cdc
9a7adef
 
 
 
 
 
 
 
 
ece449a
 
 
 
 
 
e377cdc
9a7adef
 
 
 
 
 
 
 
 
ece449a
 
 
e377cdc
9a7adef
 
 
 
 
 
 
 
 
ece449a
 
 
e377cdc
9a7adef
 
 
 
 
 
 
 
 
 
 
 
ece449a
 
 
e377cdc
9a7adef
 
 
 
 
 
 
 
 
ece449a
 
 
 
 
 
 
 
 
 
 
e377cdc
4989364
 
 
 
 
 
 
 
 
 
 
 
 
e377cdc
 
 
 
 
 
 
 
 
 
 
ece449a
 
fa24889
ece449a
 
 
 
 
4dbf90f
ece449a
4dbf90f
ece449a
4dbf90f
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import cv2
import numpy as np
from PIL import Image
from smolagents import tool
import tempfile
import os

def apply_filters(image: np.ndarray) -> list[np.ndarray]:
    """Applies a series of filters to the input image.

    Args:
        image (np.ndarray): Input image in BGR format.

    Returns:
        list[np.ndarray]: List of filtered images in BGR format.
    """
    filtered_images = []

    # Filter 1: Contrast adjustment
    filtered_images.append(adjust_contrast(image))

    # Filter 2: Saturation boost
    filtered_images.append(adjust_saturation(image))

    # Filter 3: Exposure adjustment
    filtered_images.append(adjust_exposure(image))

    # Filter 4: Denoised
    filtered_images.append(denoise_image(image))

    # Filter 5: Vignette effect
    filtered_images.append(apply_vignette(image))

    return filtered_images

@tool
def adjust_contrast(image: np.ndarray, alpha: float = 1.5) -> np.ndarray:
    """Adjusts the contrast of the image.

    Args:
        image (np.ndarray): Input image in BGR format.
        alpha (float, optional): Contrast control (1.0-3.0). 1.0 means no change. Defaults to 1.5.

    Returns:
        np.ndarray: Contrast adjusted image in BGR format.
    """
    return cv2.convertScaleAbs(image, alpha=alpha, beta=0)

@tool
def adjust_saturation(image: np.ndarray, saturation_scale: float = 1.0) -> np.ndarray:
    """Adjusts the saturation of the image.

    Args:
        image (np.ndarray): Input image in BGR format.
        saturation_scale (float, optional): Saturation scale factor. 1.0 means no change. Defaults to 1.0.

    Returns:
        np.ndarray: Saturation adjusted image in BGR format.
    """
    hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV).astype(np.float32)
    hsv_img[:, :, 1] *= saturation_scale
    hsv_img[:, :, 1] = np.clip(hsv_img[:, :, 1], 0, 255)
    return cv2.cvtColor(hsv_img.astype(np.uint8), cv2.COLOR_HSV2BGR)

@tool
def adjust_exposure(image: np.ndarray, beta: int = 50) -> np.ndarray:
    """Adjusts the exposure (brightness) of the image.

    Args:
        image (np.ndarray): Input image in BGR format.
        beta (int, optional): Brightness control. Positive values increase brightness, negative decrease. Defaults to 50.

    Returns:
        np.ndarray: Exposure adjusted image in BGR format.
    """
    return cv2.convertScaleAbs(image, alpha=1.0, beta=beta)

@tool
def denoise_image(image: np.ndarray, h: int = 10) -> np.ndarray:
    """Denoises the image using Non-local Means Denoising algorithm.

    Args:
        image (np.ndarray): Input image in BGR format.
        h (int, optional): Filter strength. Higher h value removes noise better but removes details. Defaults to 10.

    Returns:
        np.ndarray: Denoised image in BGR format.
    """
    return cv2.fastNlMeansDenoisingColored(image, None, h, h, 7, 21)

@tool
def crop_image(image: np.ndarray, x: int, y: int, width: int, height: int) -> np.ndarray:
    """Crops the image to the specified rectangle.

    Args:
        image (np.ndarray): Input image in BGR format.
        x (int): Top-left x-coordinate.
        y (int): Top-left y-coordinate.
        width (int): Width of the crop rectangle.
        height (int): Height of the crop rectangle.

    Returns:
        np.ndarray: Cropped image in BGR format.
    """
    return image[y:y+height, x:x+width]

@tool
def apply_vignette(image: np.ndarray, level: int = 2) -> np.ndarray:
    """Applies a vignette effect to the image.

    Args:
        image (np.ndarray): Input image in BGR format.
        level (int, optional): Intensity of the vignette effect. Defaults to 2.

    Returns:
        np.ndarray: Image with vignette effect applied in BGR format.
    """
    rows, cols = image.shape[:2]
    kernel_x = cv2.getGaussianKernel(cols, cols/level)
    kernel_y = cv2.getGaussianKernel(rows, rows/level)
    kernel = kernel_y * kernel_x.T
    mask = kernel / kernel.max()
    vignette = np.copy(image)
    for i in range(3):
        vignette[:, :, i] = vignette[:, :, i] * mask
    return vignette

@tool
def load_image_as_bgr(image_path: str) -> np.ndarray:
    """Loads an image from path and converts it to BGR format for OpenCV.
    
    Args:
        image_path (str): Path to the image file.
        
    Returns:
        np.ndarray: Image in BGR format as numpy array.
    """
    image = Image.open(image_path)
    image_np = np.array(image)
    return cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)

@tool 
def save_image(image: np.ndarray, image_path: str) -> None:
    """Saves an image to the specified path.

    Args:
        image (np.ndarray): Image to save.
        image_path (str): Path to save the image.
    """
    cv2.imwrite(image_path, image)


if __name__ == "__main__":
    # Load a test image
    test_image_np = load_image_as_bgr("test_image.jpg")
    
    # Apply all filters
    filtered_images = apply_filters(test_image_np)
    
    # Save results
    dir = tempfile.mkdtemp()
    for i, filtered_img in enumerate(filtered_images):
        output_path = os.path.join(dir, f"filter_{i+1}.jpg")
        rgb_img = cv2.cvtColor(filtered_img, cv2.COLOR_BGR2RGB)
        Image.fromarray(rgb_img).save(output_path)
        print(f"Saved {output_path}")