Skip to content

Commit

Permalink
Avoided division by zero (for rare cases)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinkya-kulkarni authored Mar 26, 2023
1 parent db27e17 commit 4a33705
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def make_vxvy(input_image, eigenvectors, threshold_value):

########################################################################################

def convert_to_8bit_grayscale(filename):
def convert_to_8bit_grayscale(filename, epsilon = 1e-6):
"""
Read an image from a file using Pillow and return the 8-bit grayscale version of the image using NumPy.
If the image is already 8-bit grayscale, return the image without modification.
Expand All @@ -457,8 +457,9 @@ def convert_to_8bit_grayscale(filename):
if img.ndim != 2:
raise ValueError("Input image must be 2D grayscale.")

# Normalize the image
img = (img - np.min(img)) * (255 / (np.max(img) - np.min(img)))
# Normalize the image, where epsilon is a small constant to prevent division by zero
img = (img - np.min(img)) * (255.0 / (np.max(img) - np.min(img) + epsilon))

img = img.astype(np.uint8)

return img
Expand Down

0 comments on commit 4a33705

Please sign in to comment.