-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimage_enhancer.py
More file actions
85 lines (64 loc) · 2.86 KB
/
image_enhancer.py
File metadata and controls
85 lines (64 loc) · 2.86 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
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
# -----------------------------------------------------------------------
# image_enhancer.py
# Version: 1.0
# Author: Vell Void
# GitHub: https://github.com/VellVoid
# Twitter: https://twitter.com/VellVoid
#
# This Python script enhances images using the OpenCV and PIL libraries.
# -----------------------------------------------------------------------
import os
import cv2
import numpy as np
from PIL import Image, ImageEnhance
from tqdm import tqdm
# Function to enhance image sharpness, contrast and apply Gaussian blur
def enhance_image(image_path, output_path, sharpness=4, contrast=1.3, blur=3):
"""Enhance image sharpness, contrast, and blur.
Args:
image_path (str): Path to the input image.
output_path (str): Path to save the enhanced image.
sharpness (float, optional): Sharpness level. Defaults to 4.
contrast (float, optional): Contrast level. Defaults to 1.3.
blur (int, optional): Blur level. Defaults to 3.
"""
# Load the image
img = cv2.imread(image_path)
# Convert the image to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Convert the image to PIL Image
pil_img = Image.fromarray(img)
# Enhance the sharpness
enhancer = ImageEnhance.Sharpness(pil_img)
img_enhanced = enhancer.enhance(sharpness)
# Enhance the contrast
enhancer = ImageEnhance.Contrast(img_enhanced)
img_enhanced = enhancer.enhance(contrast)
# Convert back to OpenCV image (numpy array)
img_enhanced = np.array(img_enhanced)
# Apply a small amount of Gaussian blur
img_enhanced = cv2.GaussianBlur(img_enhanced, (blur, blur), 0)
# Convert back to PIL Image and save
img_enhanced = Image.fromarray(img_enhanced)
img_enhanced.save(output_path)
def process_directory(input_dir, output_dir_name, sharpness=1.2, contrast=1.1):
"""Process all images in a directory, enhancing them.
Args:
input_dir (str): Path to the input directory with images.
output_dir_name (str): Name of output directory to save enhanced images.
sharpness (float, optional): Sharpness level. Defaults to 1.2.
contrast (float, optional): Contrast level. Defaults to 1.1.
"""
# Create the output directory inside the input directory
output_dir = os.path.join(input_dir, output_dir_name)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Get a list of all images in the input directory
image_files = [f for f in os.listdir(input_dir) if f.endswith(".jpg") or f.endswith(".png")]
# Process all images in the input directory
for filename in tqdm(image_files, desc="Processing images"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
enhance_image(input_path, output_path, sharpness, contrast)
# Example usage
process_directory('example/path', 'example/path/newfolder')