lyimo commited on
Commit
915a80d
·
verified ·
1 Parent(s): 2032cc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -16
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 add_noise(image, mean, var):
 
 
 
 
 
 
 
 
 
 
 
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(image):
14
- image = image.convert('L')
 
 
 
 
 
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
- interface = gr.Interface(
24
- fn=add_noise,
25
- inputs=[
26
- gr.Image(label="Original Image"),
27
- gr.Slider(0, 0.05, step=0.001, value=0, label="Mean of Gaussian Noise"),
28
- gr.Slider(0, 0.01, step=0.0001, value=0.001, label="Variance of Gaussian Noise")
29
- ],
30
- outputs=gr.Image(label="Processed Image"),
31
- title="Image Noise Addition and Removal",
32
- description="Add Gaussian noise to an image and attempt to remove it. Adjust the mean and variance for different effects."
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()