File size: 2,938 Bytes
f1cb997
664f2dc
 
 
 
915a80d
801582b
664f2dc
915a80d
 
 
 
 
 
 
 
 
 
c046d34
 
2acd67e
2032cc0
664f2dc
 
915a80d
 
4064d1f
 
 
915a80d
 
2acd67e
4acc6af
 
2acd67e
4acc6af
2acd67e
4acc6af
 
2acd67e
7c55952
2032cc0
664f2dc
 
915a80d
 
 
 
 
 
2acd67e
915a80d
 
 
 
 
 
 
2acd67e
915a80d
 
 
f1cb997
2032cc0
915a80d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
import hashlib
from skimage import img_as_float 

def hash_password(password):
    """Hash the password to generate noise parameters."""
    hash_object = hashlib.sha256(password.encode())
    hex_dig = hash_object.hexdigest()
    return int(hex_dig, 16)

def add_noise(image, password):
    """Add Gaussian noise to the image based on the password."""
    seed = hash_password(password) % (2**32)
    np.random.seed(seed)
    mean = np.random.uniform(-0.001, 0.1)  # Wider range for mean
    var = np.random.uniform(0.0005, 0.1)   # Higher variance for stronger noise
    noisy_image = random_noise(np.array(image), mode='gaussian', mean=mean, var=var, clip=True)
    noisy_image = (255 * noisy_image).astype(np.uint8)  # Scale back to 0-255
    return Image.fromarray(noisy_image)

def remove_noise(noisy_image, password):
    """Attempt to remove noise from the image using the same password."""
    if isinstance(noisy_image, np.ndarray):
        noisy_image = Image.fromarray(noisy_image.astype('uint8'))

    seed = hash_password(password) % (2**32)
    np.random.seed(seed)  # Reset the seed to generate the same noise parameters
    image = np.array(noisy_image)

    # Convert image to floating point type needed for denoise_nl_means
    image_float = img_as_float(image)

    sigma_est = np.mean(estimate_sigma(image_float, channel_axis=-1))  # Use channel_axis=-1 for color images

    denoised_image = denoise_nl_means(image_float, h=1.15 * sigma_est, fast_mode=True,
                                      patch_size=5, patch_distance=6, channel_axis=-1)

    denoised_image = (255 * denoised_image).astype(np.uint8)  # Scale back to 0-255
    return Image.fromarray(denoised_image)

# Define Gradio interface with tabs for adding and removing noise
with gr.Blocks() as interface:
    gr.Markdown("### Image Noise Encryption and Decryption App")
    with gr.Tab("Encrypt"):
        with gr.Row():
            image_input = gr.Image(label="Original Image")
            password_input = gr.Textbox(label="Password for Encryption", type="password")
            encrypt_button = gr.Button("Encrypt Image")
        image_output = gr.Image(label="Encrypted Image")
        encrypt_button.click(add_noise, inputs=[image_input, password_input], outputs=image_output)

    with gr.Tab("Decrypt"):
        with gr.Row():
            image_input_decrypt = gr.Image(label="Encrypted Image")
            password_input_decrypt = gr.Textbox(label="Password for Decryption", type="password")
            decrypt_button = gr.Button("Decrypt Image")
        image_output_decrypt = gr.Image(label="Decrypted Image")
        decrypt_button.click(remove_noise, inputs=[image_input_decrypt, password_input_decrypt], outputs=image_output_decrypt)

# Launch the app
interface.launch()