Update app.py
Browse files
app.py
CHANGED
|
@@ -22,9 +22,10 @@ def add_noise(image, password):
|
|
| 22 |
noisy_image = (255 * noisy_image).astype(np.uint8) # Scale back to 0-255
|
| 23 |
return Image.fromarray(noisy_image)
|
| 24 |
|
|
|
|
|
|
|
| 25 |
def remove_noise(noisy_image, password):
|
| 26 |
"""Attempt to remove noise from the image using the same password."""
|
| 27 |
-
# Check if the noisy_image is a numpy array and convert it to PIL Image if so
|
| 28 |
if isinstance(noisy_image, np.ndarray):
|
| 29 |
noisy_image = Image.fromarray(noisy_image.astype('uint8'))
|
| 30 |
|
|
@@ -32,15 +33,26 @@ def remove_noise(noisy_image, password):
|
|
| 32 |
np.random.seed(seed) # Reset the seed to generate the same noise parameters
|
| 33 |
mean = np.random.uniform(-0.05, 0.05)
|
| 34 |
var = np.random.uniform(0.001, 0.01)
|
| 35 |
-
image = noisy_image.convert('L') # Ensure image is in grayscale
|
| 36 |
image_np = np.array(image)
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
patch_size=5, patch_distance=6, multichannel=False)
|
| 40 |
denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
|
| 41 |
return Image.fromarray(denoised_image)
|
| 42 |
|
| 43 |
|
|
|
|
| 44 |
# Define Gradio interface with tabs for adding and removing noise
|
| 45 |
with gr.Blocks() as interface:
|
| 46 |
gr.Markdown("### Image Noise Encryption and Decryption App")
|
|
|
|
| 22 |
noisy_image = (255 * noisy_image).astype(np.uint8) # Scale back to 0-255
|
| 23 |
return Image.fromarray(noisy_image)
|
| 24 |
|
| 25 |
+
from skimage import img_as_float
|
| 26 |
+
|
| 27 |
def remove_noise(noisy_image, password):
|
| 28 |
"""Attempt to remove noise from the image using the same password."""
|
|
|
|
| 29 |
if isinstance(noisy_image, np.ndarray):
|
| 30 |
noisy_image = Image.fromarray(noisy_image.astype('uint8'))
|
| 31 |
|
|
|
|
| 33 |
np.random.seed(seed) # Reset the seed to generate the same noise parameters
|
| 34 |
mean = np.random.uniform(-0.05, 0.05)
|
| 35 |
var = np.random.uniform(0.001, 0.01)
|
| 36 |
+
image = noisy_image.convert('L') # Ensure image is in grayscale for simplicity
|
| 37 |
image_np = np.array(image)
|
| 38 |
+
|
| 39 |
+
# Convert image to floating point type needed for denoise_nl_means
|
| 40 |
+
image_float = img_as_float(image_np)
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# For newer skimage versions or if the image is colored
|
| 44 |
+
sigma_est = np.mean(estimate_sigma(image_float, channel_axis=None))
|
| 45 |
+
except TypeError:
|
| 46 |
+
# For older skimage versions
|
| 47 |
+
sigma_est = np.mean(estimate_sigma(image_float))
|
| 48 |
+
|
| 49 |
+
denoised_image = denoise_nl_means(image_float, h=1.15 * sigma_est, fast_mode=True,
|
| 50 |
patch_size=5, patch_distance=6, multichannel=False)
|
| 51 |
denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
|
| 52 |
return Image.fromarray(denoised_image)
|
| 53 |
|
| 54 |
|
| 55 |
+
|
| 56 |
# Define Gradio interface with tabs for adding and removing noise
|
| 57 |
with gr.Blocks() as interface:
|
| 58 |
gr.Markdown("### Image Noise Encryption and Decryption App")
|