from PIL import Image import numpy as np def add_noise(image_path, output_path, sigma=50): # Load the clean image clean_image = Image.open(image_path).convert('RGB') clean_image_np = np.array(clean_image) # Generate Gaussian noise noise = np.random.normal(0, sigma, clean_image_np.shape).astype(np.float32) # Add noise to the image noisy_image_np = clean_image_np + noise # Clip the values to be in the valid range [0, 255] noisy_image_np = np.clip(noisy_image_np, 0, 255).astype(np.uint8) # Convert the noisy image back to a PIL image noisy_image = Image.fromarray(noisy_image_np) # Save the noisy image noisy_image.save(output_path) # Example usage input_image_path = '/home/jiachen/MyGradio/test_images/0059.png' output_image_path = '/home/jiachen/MyGradio/test_images/noisy_0059.png' add_noise(input_image_path, output_image_path, sigma=15)