Skip to content

Commit

Permalink
adjust filters
Browse files Browse the repository at this point in the history
  • Loading branch information
HokageM committed Apr 2, 2024
1 parent 4a1cba1 commit d767e8b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 36 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ It simplifies the process of identifying disparities between images, making it i
assurance. Additionally, it offers options for customization, which can be helpful for color-blind users.


## Installation

```bash
pip install byakuganvisualizer
```

## Usage

```
Expand Down
23 changes: 18 additions & 5 deletions src/byakuganvisualizer/ImageFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def apply_red_filter(image_data):
:return:
"""
red_filtered = np.zeros_like(image_data)
red_filtered[:, :, 0] = image_data[:, :, 0] * 2
red_filtered[:, :, 0] = image_data[:, :, 0]
return red_filtered

@staticmethod
Expand All @@ -24,7 +24,7 @@ def apply_green_filter(image_data):
:param image_data: The image data to apply the filter to.
"""
green_filtered = np.zeros_like(image_data)
green_filtered[:, :, 1] = image_data[:, :, 1] * 2
green_filtered[:, :, 1] = image_data[:, :, 1]
return green_filtered

@staticmethod
Expand All @@ -35,7 +35,7 @@ def apply_blue_filter(image_data):
:return:
"""
blue_filtered = np.zeros_like(image_data)
blue_filtered[:, :, 2] = image_data[:, :, 2] * 2
blue_filtered[:, :, 2] = image_data[:, :, 2]
return blue_filtered

@staticmethod
Expand All @@ -46,6 +46,19 @@ def apply_yellow_filter(image_data):
:return:
"""
yellow_filtered = np.zeros_like(image_data)
yellow_filtered[:, :, 0] = image_data[:, :, 0] * 2
yellow_filtered[:, :, 1] = image_data[:, :, 1] * 2
yellow_filtered[:, :, 0] = image_data[:, :, 0]
yellow_filtered[:, :, 1] = image_data[:, :, 1]
return yellow_filtered

@staticmethod
def adjust_for_deuteranomaly(image_array):
# Define color transformation matrix for Deuteranomaly
correction_matrix = np.array([[1.0, 0.0, 0.0],
[0.494207, 0.0, 1.24827],
[0.0, 0.0, 1.0]])

# Apply the color correction matrix
adjusted_array = np.dot(image_array[..., :3], correction_matrix.T)
adjusted_array = np.clip(adjusted_array, 0, 255).astype(np.uint8)

return adjusted_array
93 changes: 62 additions & 31 deletions src/byakuganvisualizer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ def parse_args(args):
parser.add_argument(
'--diff',
type=parse_tuples,
required=True,
help='String containing a list of tuples "Path_To_Image1a,Path_To_Image2a;Path_To_Image1b,Path_To_Image2b...". '
'Each tuple contains two paths to images to be compared.'
)
parser.add_argument(
'--images',
type=str,
help='List of image names to be manipulated by a filter. E.g.: A,B,C,D'
)
parser.add_argument(
'--filter',
choices=['red', 'blue', 'green', 'yellow'],
help='Filter type (red, blue, green, yellow)'
choices=['red', 'blue', 'green', 'yellow', 'deuteranomaly'],
help='Filter type (red, blue, green, yellow, deuteranomaly)'
)
parser.add_argument(
'--out_dir',
Expand All @@ -77,34 +81,61 @@ def main(args):
os.makedirs(args.out_dir)
print(f"Output Directory: '{args.out_dir}' created.")

for pair in args.diff:
pair_name = os.path.basename(pair[0]).split('.')[0] + '_' + os.path.basename(pair[1]).split('.')[0]

image1 = Image.open(pair[0])
image2 = Image.open(pair[1])

array1 = np.array(image1)
array2 = np.array(image2)

# Calculate the absolute difference between the two arrays
difference_array = np.abs(array1 - array2)

if args.filter == 'red':
difference_array = ImageFilter.apply_red_filter(difference_array)
pair_name += '_red'
if args.filter == 'blue':
difference_array = ImageFilter.apply_blue_filter(difference_array)
pair_name += '_blue'
if args.filter == 'green':
difference_array = ImageFilter.apply_green_filter(difference_array)
pair_name += '_green'
if args.filter == 'yellow':
difference_array = ImageFilter.apply_yellow_filter(difference_array)
pair_name += '_yellow'

difference_image = Image.fromarray(difference_array.astype('uint8'))
difference_image.save(f'{args.out_dir}/Diff_{pair_name}.jpg')

if args.diff:
for pair in args.diff:
pair_name = os.path.basename(pair[0]).split('.')[0] + '_' + os.path.basename(pair[1]).split('.')[0]

image1 = Image.open(pair[0])
image2 = Image.open(pair[1])

array1 = np.array(image1)
array2 = np.array(image2)

# Calculate the absolute difference between the two arrays
difference_array = np.abs(array1 - array2)

if args.filter == 'red':
difference_array = ImageFilter.apply_red_filter(difference_array)
pair_name += '_red'
if args.filter == 'blue':
difference_array = ImageFilter.apply_blue_filter(difference_array)
pair_name += '_blue'
if args.filter == 'green':
difference_array = ImageFilter.apply_green_filter(difference_array)
pair_name += '_green'
if args.filter == 'yellow':
difference_array = ImageFilter.apply_yellow_filter(difference_array)
pair_name += '_yellow'

difference_image = Image.fromarray(difference_array.astype('uint8'))
difference_image.save(f'{args.out_dir}/Diff_{pair_name}.jpg')

if args.images:
args.images = args.images.split(',')
for img in args.images:
image_name = os.path.basename(img).split('.')[0]

image = Image.open(img)
rgb_array = np.array(image)

if args.filter == 'red':
rgb_array = ImageFilter.apply_red_filter(rgb_array)
image_name += '_red'
if args.filter == 'blue':
rgb_array = ImageFilter.apply_blue_filter(rgb_array)
image_name += '_blue'
if args.filter == 'green':
rgb_array = ImageFilter.apply_green_filter(rgb_array)
image_name += '_green'
if args.filter == 'yellow':
rgb_array = ImageFilter.apply_yellow_filter(rgb_array)
image_name += '_yellow'
if args.filter == 'deuteranomaly':
rgb_array = ImageFilter.adjust_for_deuteranomaly(rgb_array)
image_name += '_deuteranomaly'

filtered_image = Image.fromarray(rgb_array.astype('uint8'))
filtered_image.save(f'{args.out_dir}/Filtered_{image_name}.jpg')

def run():
main(sys.argv[1:])
Expand Down
Binary file modified tests/test_images/diff/Diff_naruto_naruto_modified_blue.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_images/diff/Diff_naruto_naruto_modified_green.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_images/diff/Diff_naruto_naruto_modified_red.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/test_images/diff/Diff_naruto_naruto_modified_yellow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d767e8b

Please sign in to comment.