lyimo commited on
Commit
2acd67e
·
verified ·
1 Parent(s): b89d6f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -21
app.py CHANGED
@@ -17,14 +17,10 @@ def add_noise(image, password):
17
  np.random.seed(seed)
18
  mean = np.random.uniform(-0.1, 0.1) # Wider range for mean
19
  var = np.random.uniform(0.05, 0.1) # Higher variance for stronger noise
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
-
26
- from skimage import img_as_float
27
-
28
  def remove_noise(noisy_image, password):
29
  """Attempt to remove noise from the image using the same password."""
30
  if isinstance(noisy_image, np.ndarray):
@@ -32,36 +28,26 @@ def remove_noise(noisy_image, password):
32
 
33
  seed = hash_password(password) % (2**32)
34
  np.random.seed(seed) # Reset the seed to generate the same noise parameters
35
- mean = np.random.uniform(-0.05, 0.05)
36
- var = np.random.uniform(0.001, 0.01)
37
- image = noisy_image.convert('L') # Ensure image is in grayscale for simplicity
38
- image_np = np.array(image)
39
 
40
  # Convert image to floating point type needed for denoise_nl_means
41
- image_float = img_as_float(image_np)
42
 
43
- try:
44
- # For newer skimage versions or if the image is colored
45
- sigma_est = np.mean(estimate_sigma(image_float, channel_axis=None))
46
- except TypeError:
47
- # For older skimage versions
48
- sigma_est = np.mean(estimate_sigma(image_float))
49
 
50
  denoised_image = denoise_nl_means(image_float, h=1.15 * sigma_est, fast_mode=True,
51
- patch_size=5, patch_distance=6, channel_axis=None)
52
 
53
  denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
54
  return Image.fromarray(denoised_image)
55
 
56
-
57
-
58
  # Define Gradio interface with tabs for adding and removing noise
59
  with gr.Blocks() as interface:
60
  gr.Markdown("### Image Noise Encryption and Decryption App")
61
  with gr.Tab("Encrypt"):
62
  with gr.Row():
63
  image_input = gr.Image(label="Original Image")
64
- password_input = gr.Textbox(label="Password for Encryption", placeholder="Enter a strong password", type="password")
65
  encrypt_button = gr.Button("Encrypt Image")
66
  image_output = gr.Image(label="Encrypted Image")
67
  encrypt_button.click(add_noise, inputs=[image_input, password_input], outputs=image_output)
@@ -69,7 +55,7 @@ with gr.Blocks() as interface:
69
  with gr.Tab("Decrypt"):
70
  with gr.Row():
71
  image_input_decrypt = gr.Image(label="Encrypted Image")
72
- password_input_decrypt = gr.Textbox(label="Password for Decryption", placeholder="Enter the same password used for encryption", type="password")
73
  decrypt_button = gr.Button("Decrypt Image")
74
  image_output_decrypt = gr.Image(label="Decrypted Image")
75
  decrypt_button.click(remove_noise, inputs=[image_input_decrypt, password_input_decrypt], outputs=image_output_decrypt)
 
17
  np.random.seed(seed)
18
  mean = np.random.uniform(-0.1, 0.1) # Wider range for mean
19
  var = np.random.uniform(0.05, 0.1) # Higher variance for stronger noise
20
+ noisy_image = random_noise(np.array(image), mode='gaussian', mean=mean, var=var, clip=True)
 
21
  noisy_image = (255 * noisy_image).astype(np.uint8) # Scale back to 0-255
22
  return Image.fromarray(noisy_image)
23
 
 
 
 
24
  def remove_noise(noisy_image, password):
25
  """Attempt to remove noise from the image using the same password."""
26
  if isinstance(noisy_image, np.ndarray):
 
28
 
29
  seed = hash_password(password) % (2**32)
30
  np.random.seed(seed) # Reset the seed to generate the same noise parameters
31
+ image = np.array(noisy_image)
 
 
 
32
 
33
  # Convert image to floating point type needed for denoise_nl_means
34
+ image_float = img_as_float(image)
35
 
36
+ sigma_est = np.mean(estimate_sigma(image_float, channel_axis=-1)) # Use channel_axis=-1 for color images
 
 
 
 
 
37
 
38
  denoised_image = denoise_nl_means(image_float, h=1.15 * sigma_est, fast_mode=True,
39
+ patch_size=5, patch_distance=6, channel_axis=-1)
40
 
41
  denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
42
  return Image.fromarray(denoised_image)
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")
47
  with gr.Tab("Encrypt"):
48
  with gr.Row():
49
  image_input = gr.Image(label="Original Image")
50
+ password_input = gr.Textbox(label="Password for Encryption", type="password")
51
  encrypt_button = gr.Button("Encrypt Image")
52
  image_output = gr.Image(label="Encrypted Image")
53
  encrypt_button.click(add_noise, inputs=[image_input, password_input], outputs=image_output)
 
55
  with gr.Tab("Decrypt"):
56
  with gr.Row():
57
  image_input_decrypt = gr.Image(label="Encrypted Image")
58
+ password_input_decrypt = gr.Textbox(label="Password for Decryption", type="password")
59
  decrypt_button = gr.Button("Decrypt Image")
60
  image_output_decrypt = gr.Image(label="Decrypted Image")
61
  decrypt_button.click(remove_noise, inputs=[image_input_decrypt, password_input_decrypt], outputs=image_output_decrypt)