lyimo commited on
Commit
caf264c
·
verified ·
1 Parent(s): 3a417d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -52
app.py CHANGED
@@ -1,65 +1,76 @@
1
  import gradio as gr
2
- 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
  import hashlib
7
- from skimage import img_as_float
 
 
8
 
9
- def hash_password(password):
10
- """Hash the password to generate noise parameters."""
11
- hash_object = hashlib.sha256(password.encode())
12
- hex_dig = hash_object.hexdigest()
13
- return int(hex_dig, 16)
14
 
15
- def add_noise(image, password):
16
- """Add Gaussian noise to the image based on the password."""
17
- seed = hash_password(password) % (2**32)
18
- np.random.seed(seed)
19
- mean = np.random.uniform(-0.001, 0.1) # Wider range for mean
20
- var = np.random.uniform(0.0005, 0.1) # Higher variance for stronger noise
21
- noisy_image = random_noise(np.array(image), mode='gaussian', mean=mean, var=var, clip=True)
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
- if isinstance(noisy_image, np.ndarray):
28
- noisy_image = Image.fromarray(noisy_image.astype('uint8'))
29
 
30
- seed = hash_password(password) % (2**32)
31
- np.random.seed(seed) # Reset the seed to generate the same noise parameters
32
- image = np.array(noisy_image)
 
33
 
34
- # Convert image to floating point type needed for denoise_nl_means
35
- image_float = img_as_float(image)
 
 
 
 
 
 
 
 
36
 
37
- sigma_est = np.mean(estimate_sigma(image_float, channel_axis=-1)) # Use channel_axis=-1 for color images
 
 
 
 
 
 
 
 
 
38
 
39
- denoised_image = denoise_nl_means(image_float, h=1.15 * sigma_est, fast_mode=True,
40
- patch_size=5, patch_distance=6, channel_axis=-1)
41
-
42
- denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
43
- return Image.fromarray(denoised_image)
44
-
45
- # Define Gradio interface with tabs for adding and removing noise
46
- with gr.Blocks() as interface:
47
- gr.Markdown("### Image Noise Encryption and Decryption App")
48
- with gr.Tab("Encrypt"):
49
  with gr.Row():
50
- image_input = gr.Image(label="Original Image")
51
- password_input = gr.Textbox(label="Password for Encryption", type="password")
52
- encrypt_button = gr.Button("Encrypt Image")
53
- image_output = gr.Image(label="Encrypted Image")
54
- encrypt_button.click(add_noise, inputs=[image_input, password_input], outputs=image_output)
55
 
56
- with gr.Tab("Decrypt"):
57
  with gr.Row():
58
- image_input_decrypt = gr.Image(label="Encrypted Image")
59
- password_input_decrypt = gr.Textbox(label="Password for Decryption", type="password")
60
- decrypt_button = gr.Button("Decrypt Image")
61
- image_output_decrypt = gr.Image(label="Decrypted Image")
62
- decrypt_button.click(remove_noise, inputs=[image_input_decrypt, password_input_decrypt], outputs=image_output_decrypt)
63
 
64
- # Launch the app
65
- interface.launch()
 
1
  import gradio as gr
 
 
 
 
2
  import hashlib
3
+ import qrcode
4
+ from PIL import Image, ImageDraw
5
+ import os
6
 
7
+ # Function to hash image data
8
+ def hash_image(image):
9
+ image_data = image.tobytes()
10
+ image_hash = hashlib.sha256(image_data).hexdigest()
11
+ return image_hash
12
 
13
+ # Function to generate QR code
14
+ def generate_qr_code(data):
15
+ qr = qrcode.QRCode(
16
+ version=1,
17
+ error_correction=qrcode.constants.ERROR_CORRECT_L,
18
+ box_size=10,
19
+ border=4,
20
+ )
21
+ qr.add_data(data)
22
+ qr.make(fit=True)
23
+ qr_img = qr.make_image(fill='black', back_color='white')
24
+ return qr_img
25
 
26
+ # Function to embed QR code into image
27
+ def embed_qr_code(image, qr_img):
28
+ image.paste(qr_img, (10, 10)) # Adjust position as needed
29
+ return image
30
 
31
+ # Function to save hash to file
32
+ def save_hash(hash_code, description=""):
33
+ with open("hash.txt", "a") as file:
34
+ file.write(f"{hash_code}: {description}\n")
35
 
36
+ # Function to check image authenticity
37
+ def check_authenticity(image):
38
+ hash_code = hash_image(image)
39
+ with open("hash.txt", "r") as file:
40
+ hashes = file.readlines()
41
+ for line in hashes:
42
+ saved_hash, description = line.strip().split(': ', 1)
43
+ if saved_hash == hash_code:
44
+ return f"Image is authentic. Description: {description}"
45
+ return "Image is new or modified."
46
 
47
+ # Main processing function
48
+ def process_image(image, description):
49
+ hash_code1 = hash_image(image)
50
+ qr_img = generate_qr_code(hash_code1)
51
+ qr_img = qr_img.resize((100, 100)) # Resize QR code as needed
52
+ image_with_qr = embed_qr_code(image, qr_img)
53
+ save_hash(hash_code1, description)
54
+ hash_code2 = hash_image(image_with_qr)
55
+ save_hash(hash_code2)
56
+ return image_with_qr, "Image processed and hashes stored."
57
 
58
+ # Gradio interface setup
59
+ with gr.Blocks() as app:
60
+ with gr.Tab("Upload and Process Image"):
 
 
 
 
 
 
 
61
  with gr.Row():
62
+ image_input = gr.Image(label="Upload Image")
63
+ description_input = gr.Textbox(label="Description")
64
+ submit_button = gr.Button("Process Image")
65
+ image_output = gr.Image(label="Processed Image")
66
+ submit_button.click(process_image, inputs=[image_input, description_input], outputs=image_output)
67
 
68
+ with gr.Tab("Check Image Authenticity"):
69
  with gr.Row():
70
+ image_check_input = gr.Image(label="Upload Image to Verify")
71
+ check_button = gr.Button("Check Authenticity")
72
+ authenticity_output = gr.Textbox(label="Result")
73
+ check_button.click(check_authenticity, inputs=[image_check_input], outputs=authenticity_output)
 
74
 
75
+ # Launch the application
76
+ app.launch()