Modify the code so that instead of Images with more std dev above mean in 40-55 range than in 0-40 it shows images where the modal value between 0 - 40 is 2 standart derivations above the mean and the the modal value bewteen 40 - 55 is also 2 standart derivations above the mean. Code: import os
from collections import defaultdict
from PIL import Image
import numpy as np

def calculate_modal_value_and_std_dev(histogram, start, end, mean, std_dev):
    range_histogram = histogram[start:end+1]
    if len(range_histogram) > 0:
        modal_value = np.argmax(range_histogram) + start
        if std_dev != 0:
            std_dev_above_mean = (range_histogram[modal_value - start] - mean) / std_dev
        else:
            std_dev_above_mean = 0
        return round(modal_value), std_dev_above_mean
    return 0, 0

def analyze_image(image_path):
    try:
        with Image.open(image_path) as img:
            gray_img = img.convert('L')
            img_array = np.array(gray_img)
        
        histogram, _ = np.histogram(img_array, bins=256, range=(0, 256))
        
        # Calculate mean and std dev for this image in the range 0-55
        range_histogram = histogram[0:56]
        mean = np.mean(range_histogram)
        std_dev = np.std(range_histogram)
        
        modal_value_0_40, std_dev_0_40 = calculate_modal_value_and_std_dev(histogram, 0, 40, mean, std_dev)
        # modal_value_0_55, std_dev_0_55 = calculate_modal_value_and_std_dev(histogram, 0, 55, mean, std_dev)  # Removed
        modal_value_40_55, std_dev_40_55 = calculate_modal_value_and_std_dev(histogram, 40, 55, mean, std_dev)

        return image_path, modal_value_0_40, std_dev_0_40, modal_value_40_55, std_dev_40_55
    except Exception as e:
        print(f"Error processing {image_path}: {str(e)}")
        return None

def process_directory(root_directory):
    results = defaultdict(list)
    more_40_55_than_0_40 = []

    for dirpath, _, filenames in os.walk(root_directory):
        for file in filenames:
            if file.lower().endswith(('png', 'jpg', 'jpeg', 'bmp', 'gif')):
                image_path = os.path.join(dirpath, file)
                result = analyze_image(image_path)
                if result:
                    subfolder = os.path.relpath(dirpath, root_directory)
                    results[subfolder].append(result)
                    if result[4] > result[2]:  # if std_dev_40_55 > std_dev_0_40
                        more_40_55_than_0_40.append(result)
    
    return results, more_40_55_than_0_40

def display_image_info(image):
    print(f"  Image: {image[0]}")
    print(f"    Modal Value 0-40: {image[1]}, Std Dev above mean 0-40: {image[2]:.2f}")
    # Removed Modal Value 0-55 display
    print(f"    Modal Value 40-55: {image[3]}, Std Dev above mean 40-55: {image[4]:.2f}")

def display_results(results, more_40_55_than_0_40):
    for subfolder, images in results.items():
        print(f"Subfolder: {subfolder}")
        for image in sorted(images, key=lambda x: x[4], reverse=True):  # sort by std_dev_40_55
            display_image_info(image)
        print()

    if more_40_55_than_0_40:
        print("Images with more std dev above mean in 40-55 range than in 0-40:")
        for image in more_40_55_than_0_40:
            display_image_info(image)

def main():
    root_dir = r'D:\Bilder\Python\1. Input'
    results, more_40_55_than_0_40 = process_directory(root_dir)
    display_results(results, more_40_55_than_0_40)

if __name__ == "__main__":
    main()