Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added dynamic brightness feature #199

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BRIGHTNESS SAMPLES/information.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This situation could have also been addressed by adjusting the global white threshold. However, if the images in the input folder vary in brightness—some being bright (high brightness) and others dark (low brightness)—changing the white threshold globally would affect all images in the input folder, not just the dark ones.
Binary file added BRIGHTNESS SAMPLES/result_after_code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BRIGHTNESS SAMPLES/result_before_code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ numpy>=1.25.0
pandas>=2.0.2
rich>=13.4.2
screeninfo>=0.8.1
Pillow>=9.5.0
71 changes: 70 additions & 1 deletion src/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import cv2
import pandas as pd
from PIL import Image ,ImageEnhance
from rich.table import Table

from src import constants
Expand Down Expand Up @@ -70,6 +71,72 @@ def print_config_summary(
console.print(table, justify="center")



# Function to split the image in quadrants
def split_into_quadrants(image):
width, height = image.size
half_width = width // 2
half_height = height // 2
quadrants = [
(0, 0, half_width, half_height),
(half_width, 0, width, half_height),
(0, half_height, half_width, height),
(half_width, half_height, width, height)
]
return [image.crop(box) for box in quadrants]
# Function to combine the quadrantss of the image
def combine_quadrants(quadrants):
width, height = quadrants[0].size
combined_image = Image.new('RGB', (2 * width, 2 * height))
combined_image.paste(quadrants[0], (0, 0))
combined_image.paste(quadrants[1], (width, 0))
combined_image.paste(quadrants[2], (0, height))
combined_image.paste(quadrants[3], (width, height))
return combined_image

# Function to calculate the brightness of the image
def calculate_brightness(image):
# Convert image to grayscale
grayscale_image = image.convert('L')
# Calculate average brightness
histogram = grayscale_image.histogram()
pixels = sum(histogram)
brightness = sum(index * count for index, count in enumerate(histogram)) / pixels
return brightness


# Function to change the brightness of an image
def adjust_brightness(image, target_brightness):
current_brightness = calculate_brightness(image)
global new_brightness
new_brightness = current_brightness
if current_brightness < target_brightness:
enhancer = ImageEnhance.Brightness(image)
factor = target_brightness / current_brightness
image = enhancer.enhance(factor)
return image

# Function to check and change the brightness
def check_and_change_brightness(files):
global prev_brightness
global new_brightness
original_image = Image.open(files)
brightness = calculate_brightness(original_image)
if(brightness<200):
prev_brightness = brightness
quadrants = split_into_quadrants(original_image)
adjusted_quadrants = []
for quadrant in quadrants:
adjusted_quadrant = adjust_brightness(quadrant, 200) # Adjust to brightness threshold 200
adjusted_quadrants.append(adjusted_quadrant)
final_image = combine_quadrants(adjusted_quadrants)
final_image.save(files)
else:
prev_brightness = brightness
new_brightness = brightness



def process_dir(
root_dir,
curr_dir,
Expand Down Expand Up @@ -100,7 +167,9 @@ def process_dir(
# look for images in current dir to process
exts = ("*.[pP][nN][gG]", "*.[jJ][pP][gG]", "*.[jJ][pP][eE][gG]")
omr_files = sorted([f for ext in exts for f in curr_dir.glob(ext)])

# checking brightness of dull images and changing accordingly
for files in omr_files:
check_and_change_brightness(files)
# Exclude images (take union over all pre_processors)
excluded_files = []
if template:
Expand Down