| import gradio as gr |
| import numpy as np |
| from PIL import Image |
| from skimage.util import random_noise |
| from skimage.restoration import denoise_nl_means, estimate_sigma |
|
|
| def add_noise(image, mean, var): |
| sigma = var ** 0.5 |
| noisy_image = random_noise(np.array(image), mode='gaussian', mean=mean, var=var) |
| noisy_image = (255 * noisy_image).astype(np.uint8) |
| return Image.fromarray(noisy_image) |
|
|
| def remove_noise(image): |
| image = image.convert('L') |
| image_np = np.array(image) |
| sigma_est = np.mean(estimate_sigma(image_np, multichannel=False)) |
| denoised_image = denoise_nl_means(image_np, h=1.15 * sigma_est, fast_mode=True, |
| patch_size=5, patch_distance=6, multichannel=False) |
| denoised_image = (255 * denoised_image).astype(np.uint8) |
| return Image.fromarray(denoised_image) |
|
|
| |
| interface = gr.Interface( |
| fn=add_noise, |
| inputs=[ |
| gr.Image(label="Original Image"), |
| gr.Slider(0, 0.05, step=0.001, value=0, label="Mean of Gaussian Noise"), |
| gr.Slider(0, 0.01, step=0.0001, value=0.001, label="Variance of Gaussian Noise") |
| ], |
| outputs=gr.Image(label="Processed Image"), |
| title="Image Noise Addition and Removal", |
| description="Add Gaussian noise to an image and attempt to remove it. Adjust the mean and variance for different effects." |
| ) |
|
|
| |
| interface.launch() |