Alexandros Popov commited on
Commit
ece449a
·
1 Parent(s): 365224b

first set of filters.

Browse files
Files changed (1) hide show
  1. filters.py +102 -9
filters.py CHANGED
@@ -1,15 +1,108 @@
1
- from PIL import ImageEnhance
 
 
2
 
3
  def apply_filters(image):
4
- filters = []
5
 
6
- # Filter 1: Brightness boost
7
- filters.append(ImageEnhance.Brightness(image).enhance(1.3))
8
 
9
- # Filter 2: Contrast boost
10
- filters.append(ImageEnhance.Contrast(image).enhance(1.5))
11
 
12
- # Filter 3: Color boost
13
- filters.append(ImageEnhance.Color(image).enhance(1.8))
14
 
15
- return filters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
 
5
  def apply_filters(image):
6
+ filtered_images = []
7
 
8
+ # Filter 1: Contrast adjustment
9
+ filtered_images.append(adjust_contrast(image))
10
 
11
+ # Filter 2: Saturation boost
12
+ filtered_images.append(adjust_saturation(image))
13
 
14
+ # Filter 3: Exposure adjustment
15
+ filtered_images.append(adjust_exposure(image))
16
 
17
+ # Filter 4: Denoised
18
+ filtered_images.append(denoise_image(image))
19
+
20
+ # Filter 5: Vignette effect
21
+ filtered_images.append(apply_vignette(image))
22
+
23
+ return filtered_images
24
+
25
+ def adjust_contrast(image, alpha=1.5):
26
+ """
27
+ Adjusts the contrast of the image.
28
+ :param image: Input image (numpy array).
29
+ :param alpha: Contrast control (1.0-3.0). 1.0 means no change.
30
+ :return: Contrast adjusted image.
31
+ """
32
+ return cv2.convertScaleAbs(image, alpha=alpha, beta=0)
33
+
34
+ def adjust_saturation(image, saturation_scale=1.0):
35
+ """
36
+ Adjusts the saturation of the image.
37
+ :param image: Input image (numpy array).
38
+ :param saturation_scale: Saturation scale factor. 1.0 means no change.
39
+ :return: Saturation adjusted image.
40
+ """
41
+ hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV).astype(np.float32)
42
+ hsv_img[:, :, 1] *= saturation_scale
43
+ hsv_img[:, :, 1] = np.clip(hsv_img[:, :, 1], 0, 255)
44
+ return cv2.cvtColor(hsv_img.astype(np.uint8), cv2.COLOR_HSV2BGR)
45
+
46
+ def adjust_exposure(image, beta=50):
47
+ """
48
+ Adjusts the exposure (brightness) of the image.
49
+ :param image: Input image (numpy array).
50
+ :param beta: Brightness control. Positive values increase brightness, negative decrease.
51
+ :return: Exposure adjusted image.
52
+ """
53
+ return cv2.convertScaleAbs(image, alpha=1.0, beta=beta)
54
+
55
+ def denoise_image(image, h=10):
56
+ """
57
+ Denoises the image using Non-local Means Denoising algorithm.
58
+ :param image: Input image (numpy array).
59
+ :param h: Filter strength. Higher h value removes noise better but removes details.
60
+ :return: Denoised image.
61
+ """
62
+ return cv2.fastNlMeansDenoisingColored(image, None, h, h, 7, 21)
63
+
64
+ def crop_image(image, x, y, width, height):
65
+ """
66
+ Crops the image to the specified rectangle.
67
+ :param image: Input image (numpy array).
68
+ :param x: Top-left x-coordinate.
69
+ :param y: Top-left y-coordinate.
70
+ :param width: Width of the crop rectangle.
71
+ :param height: Height of the crop rectangle.
72
+ :return: Cropped image.
73
+ """
74
+ return image[y:y+height, x:x+width]
75
+
76
+ def apply_vignette(image, level=2):
77
+ """
78
+ Applies a vignette effect to the image.
79
+ :param image: Input image (numpy array).
80
+ :param level: Intensity of the vignette effect.
81
+ :return: Image with vignette effect applied.
82
+ """
83
+ rows, cols = image.shape[:2]
84
+ kernel_x = cv2.getGaussianKernel(cols, cols/level)
85
+ kernel_y = cv2.getGaussianKernel(rows, rows/level)
86
+ kernel = kernel_y * kernel_x.T
87
+ mask = kernel / kernel.max()
88
+ vignette = np.copy(image)
89
+ for i in range(3):
90
+ vignette[:, :, i] = vignette[:, :, i] * mask
91
+ return vignette
92
+
93
+ if __name__ == "__main__":
94
+ # Load a test image
95
+ test_image = Image.open("toa-heftiba-Xmn-QXsVL4k-unsplash.jpg")
96
+ # Convert PIL Image to numpy array (BGR format for OpenCV)
97
+ test_image_np = np.array(test_image)
98
+ test_image_np = cv2.cvtColor(test_image_np, cv2.COLOR_RGB2BGR)
99
+
100
+ # Apply all filters
101
+ filtered_images = apply_filters(test_image_np)
102
+
103
+ # Save results
104
+ for i, filtered_img in enumerate(filtered_images):
105
+ # Convert back to RGB for saving
106
+ rgb_img = cv2.cvtColor(filtered_img, cv2.COLOR_BGR2RGB)
107
+ Image.fromarray(rgb_img).save(f"filter_{i+1}_result.jpg")
108
+ print(f"Saved filter_{i+1}_result.jpg")