I get the following error: Processing D:\Bilder\Python\1. Input\01.png
Failed to process D:\Bilder\Python\1. Input\01.png: image has wrong mode   Programm: import os
import numpy as np
from PIL import Image
from PIL import ImageOps

# Input and output directories
input_dir = r'D:\Bilder\Python\1. Input'
output_dir = r'D:\Bilder\Python\2. Output'

# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)

def process_image(image_path, output_path):
    # Open the image and convert to RGB (if not already)
    img = Image.open(image_path).convert('RGB')
    
    # Ensure image is in 16-bit per channel
    img = img.convert('I;16')

    # Convert the image to a numpy array
    img_np = np.array(img, dtype=np.float64)

    # Normalize the pixel values to [0, 1]
    img_np /= np.iinfo(np.uint16).max

    # Get the original dimensions
    orig_height, orig_width = img_np.shape[0], img_np.shape[1]
    
    # Desired height
    desired_height = 1600

    # Determine if upscaling or downscaling
    if orig_height < desired_height:
        # Upscaling - use Lanczos with sigmoidal light
        # Apply sigmoidal contrast mapping before resizing
        contrast = 7.5  # You can adjust this value
        img_np = sigmoidize(img_np, contrast)
        
        # Calculate new dimensions while maintaining aspect ratio
        scale_factor = desired_height / orig_height
        new_width = int(orig_width * scale_factor)
        new_size = (new_width, desired_height)
        
        # Resize using Lanczos filter
        img_resized = Image.fromarray((img_np * np.iinfo(np.uint16).max).astype(np.uint16), mode='I;16')
        img_resized = img_resized.resize(new_size, resample=Image.LANCZOS)
        
        # Convert back to numpy array
        img_np = np.array(img_resized, dtype=np.float64)
        img_np /= np.iinfo(np.uint16).max

        # Apply inverse sigmoidal contrast mapping after resizing
        img_np = inverse_sigmoidize(img_np, contrast)
    else:
        # Downscaling - use Lanczos in linear light
        # Convert to linear light (gamma correction with gamma=2.2)
        img_np = gamma_decode(img_np)
        
        # Calculate new dimensions while maintaining aspect ratio
        scale_factor = desired_height / orig_height
        new_width = int(orig_width * scale_factor)
        new_size = (new_width, desired_height)
        
        # Resize using Lanczos filter
        img_resized = Image.fromarray((img_np * np.iinfo(np.uint16).max).astype(np.uint16), mode='I;16')
        img_resized = img_resized.resize(new_size, resample=Image.LANCZOS)
        
        # Convert back to numpy array
        img_np = np.array(img_resized, dtype=np.float64)
        img_np /= np.iinfo(np.uint16).max

        # Convert back to sRGB (gamma encode with gamma=1/2.2)
        img_np = gamma_encode(img_np)
    
    # Clip values to [0, 1] and convert back to uint16
    img_np = np.clip(img_np, 0, 1)
    img_np = (img_np * np.iinfo(np.uint16).max).astype(np.uint16)
    
    # Convert numpy array back to image
    img_out = Image.fromarray(img_np, mode='I;16')
    
    # Save the image in PNG format to preserve 16-bit depth
    img_out.save(output_path, 'PNG')

def sigmoidize(img_np, contrast):
    """Apply sigmoidal contrast mapping to numpy array."""
    return 1 / (1 + np.exp(-contrast * (img_np - 0.5)))

def inverse_sigmoidize(img_np, contrast):
    """Apply inverse sigmoidal contrast mapping to numpy array."""
    return 0.5 - (1 / contrast) * np.log((1 / img_np) - 1)

def gamma_decode(img_np, gamma=2.2):
    """Convert sRGB to linear RGB."""
    return img_np ** gamma

def gamma_encode(img_np, gamma=2.2):
    """Convert linear RGB to sRGB."""
    return img_np ** (1 / gamma)

def process_directory(input_dir, output_dir):
    for root, dirs, files in os.walk(input_dir):
        # Calculate the relative path
        rel_path = os.path.relpath(root, input_dir)
        # Create corresponding directory in output
        output_subdir = os.path.join(output_dir, rel_path)
        os.makedirs(output_subdir, exist_ok=True)
        
        for file in files:
            if file.lower().endswith(('.jpg', '.jpeg', '.png', '.tif', '.tiff', '.bmp', '.gif')):
                input_path = os.path.join(root, file)
                output_path = os.path.join(output_subdir, file)
                try:
                    print(f"Processing {input_path}")
                    process_image(input_path, output_path)
                except Exception as e:
                    print(f"Failed to process {input_path}: {e}")

# Run the script
process_directory(input_dir, output_dir)