Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,32 @@ import numpy as np
|
|
| 3 |
from PIL import Image
|
| 4 |
from skimage.util import random_noise
|
| 5 |
from skimage.restoration import denoise_nl_means, estimate_sigma
|
|
|
|
| 6 |
|
| 7 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
sigma = var ** 0.5
|
| 9 |
noisy_image = random_noise(np.array(image), mode='gaussian', mean=mean, var=var)
|
| 10 |
noisy_image = (255 * noisy_image).astype(np.uint8) # Scale back to 0-255
|
| 11 |
return Image.fromarray(noisy_image)
|
| 12 |
|
| 13 |
-
def remove_noise(
|
| 14 |
-
image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
image_np = np.array(image)
|
| 16 |
sigma_est = np.mean(estimate_sigma(image_np, multichannel=False))
|
| 17 |
denoised_image = denoise_nl_means(image_np, h=1.15 * sigma_est, fast_mode=True,
|
|
@@ -19,18 +36,24 @@ def remove_noise(image):
|
|
| 19 |
denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
|
| 20 |
return Image.fromarray(denoised_image)
|
| 21 |
|
| 22 |
-
# Define Gradio interface
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
gr.
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Launch the app
|
| 36 |
-
interface.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from skimage.util import random_noise
|
| 5 |
from skimage.restoration import denoise_nl_means, estimate_sigma
|
| 6 |
+
import hashlib
|
| 7 |
|
| 8 |
+
def hash_password(password):
|
| 9 |
+
"""Hash the password to generate noise parameters."""
|
| 10 |
+
hash_object = hashlib.sha256(password.encode())
|
| 11 |
+
hex_dig = hash_object.hexdigest()
|
| 12 |
+
return int(hex_dig, 16)
|
| 13 |
+
|
| 14 |
+
def add_noise(image, password):
|
| 15 |
+
"""Add Gaussian noise to the image based on the password."""
|
| 16 |
+
seed = hash_password(password) % (2**32)
|
| 17 |
+
np.random.seed(seed)
|
| 18 |
+
mean = np.random.uniform(-0.05, 0.05)
|
| 19 |
+
var = np.random.uniform(0.001, 0.01)
|
| 20 |
sigma = var ** 0.5
|
| 21 |
noisy_image = random_noise(np.array(image), mode='gaussian', mean=mean, var=var)
|
| 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 |
+
seed = hash_password(password) % (2**32)
|
| 28 |
+
np.random.seed(seed) # Reset the seed to generate the same noise parameters
|
| 29 |
+
mean = np.random.uniform(-0.05, 0.05)
|
| 30 |
+
var = np.random.uniform(0.001, 0.01)
|
| 31 |
+
image = noisy_image.convert('L')
|
| 32 |
image_np = np.array(image)
|
| 33 |
sigma_est = np.mean(estimate_sigma(image_np, multichannel=False))
|
| 34 |
denoised_image = denoise_nl_means(image_np, h=1.15 * sigma_est, fast_mode=True,
|
|
|
|
| 36 |
denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
|
| 37 |
return Image.fromarray(denoised_image)
|
| 38 |
|
| 39 |
+
# Define Gradio interface with tabs for adding and removing noise
|
| 40 |
+
with gr.Blocks() as interface:
|
| 41 |
+
gr.Markdown("### Image Noise Encryption and Decryption App")
|
| 42 |
+
with gr.Tab("Encrypt"):
|
| 43 |
+
with gr.Row():
|
| 44 |
+
image_input = gr.Image(label="Original Image")
|
| 45 |
+
password_input = gr.Textbox(label="Password for Encryption", placeholder="Enter a strong password", type="password")
|
| 46 |
+
encrypt_button = gr.Button("Encrypt Image")
|
| 47 |
+
image_output = gr.Image(label="Encrypted Image")
|
| 48 |
+
encrypt_button.click(add_noise, inputs=[image_input, password_input], outputs=image_output)
|
| 49 |
+
|
| 50 |
+
with gr.Tab("Decrypt"):
|
| 51 |
+
with gr.Row():
|
| 52 |
+
image_input_decrypt = gr.Image(label="Encrypted Image")
|
| 53 |
+
password_input_decrypt = gr.Textbox(label="Password for Decryption", placeholder="Enter the same password used for encryption", type="password")
|
| 54 |
+
decrypt_button = gr.Button("Decrypt Image")
|
| 55 |
+
image_output_decrypt = gr.Image(label="Decrypted Image")
|
| 56 |
+
decrypt_button.click(remove_noise, inputs=[image_input_decrypt, password_input_decrypt], outputs=image_output_decrypt)
|
| 57 |
|
| 58 |
# Launch the app
|
| 59 |
+
interface.launch()
|