RafidMehda commited on
Commit
36e3dbb
·
verified ·
1 Parent(s): 9c3522c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -30
app.py CHANGED
@@ -1,48 +1,53 @@
1
- import streamlit as st
2
- import numpy as np
3
  from PIL import Image
 
 
4
  import io
5
 
6
  # Logistic map function
7
- def logistic_map(size, key):
8
- x = key
9
- sequence = []
10
- for _ in range(size):
11
- x = 4 * x * (1 - x) # Logistic map equation
12
- sequence.append(int(x * 255) % 256) # Map to 0-255
13
- return np.array(sequence, dtype=np.uint8)
14
-
15
- # Encrypt image using logistic map
16
- def encrypt_image(image, key):
17
- h, w, c = image.shape
18
- size = h * w * c
19
- chaotic_sequence = logistic_map(size, key).reshape(h, w, c)
20
- encrypted_image = np.mod(image + chaotic_sequence, 256) # Element-wise encryption
21
- return encrypted_image.astype(np.uint8)
22
-
23
- # Streamlit app
 
 
 
 
 
24
  def main():
25
  st.title("Chaotic Logistic Map Image Encryption")
26
- st.write("Upload an image to encrypt using chaotic logistic map encryption.")
27
 
28
- # File uploader
29
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
30
  if uploaded_file is not None:
31
- # Display the uploaded image
32
  input_image = Image.open(uploaded_file)
33
- st.image(input_image, caption="Uploaded Image", use_container_width=True)
34
 
35
- # Convert the image to numpy array
36
- input_image_np = np.array(input_image)
37
 
38
  # Key input
39
- key = st.slider("Set the encryption key (0 < key < 1)", min_value=0.001, max_value=0.999, step=0.001)
 
40
  if st.button("Encrypt Image"):
41
  # Encrypt the image
42
- encrypted_image_np = encrypt_image(input_image_np, key)
43
- encrypted_image = Image.fromarray(encrypted_image_np)
44
 
45
- # Display the encrypted image
46
  st.image(encrypted_image, caption="Encrypted Image", use_container_width=True)
47
 
48
  # Save encrypted image to a buffer
@@ -50,7 +55,7 @@ def main():
50
  encrypted_image.save(buffer, format="PNG")
51
  buffer.seek(0)
52
 
53
- # Provide download link for the encrypted image
54
  st.download_button(
55
  label="Download Encrypted Image",
56
  data=buffer,
 
 
 
1
  from PIL import Image
2
+ import numpy as np
3
+ import streamlit as st
4
  import io
5
 
6
  # Logistic map function
7
+ def logistic_map(r, x):
8
+ return r * x * (1 - x)
9
+
10
+ # Generate chaotic key based on the logistic map
11
+ def generate_key(seed, n):
12
+ key = []
13
+ x = seed
14
+ for _ in range(n):
15
+ x = logistic_map(3.9, x)
16
+ key.append(int(x * 255) % 256) # Map to 0-255
17
+ return np.array(key, dtype=np.uint8)
18
+
19
+ # Encrypt the image using chaotic logistic map
20
+ def encrypt_image(img_array, seed):
21
+ h, w, c = img_array.shape
22
+ flat_image = img_array.flatten()
23
+ chaotic_key = generate_key(seed, len(flat_image))
24
+ encrypted_flat = [pixel ^ chaotic_key[i] for i, pixel in enumerate(flat_image)]
25
+ encrypted_array = np.array(encrypted_flat, dtype=np.uint8).reshape(h, w, c)
26
+ return encrypted_array
27
+
28
+ # Streamlit App
29
  def main():
30
  st.title("Chaotic Logistic Map Image Encryption")
31
+ st.write("Upload an image to encrypt using a chaotic logistic map.")
32
 
 
33
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
34
  if uploaded_file is not None:
35
+ # Load the uploaded image
36
  input_image = Image.open(uploaded_file)
37
+ img_array = np.array(input_image)
38
 
39
+ # Display original image
40
+ st.image(input_image, caption="Uploaded Image", use_container_width=True)
41
 
42
  # Key input
43
+ key_seed = st.slider("Set the encryption key seed (0 < key < 1)", min_value=0.001, max_value=0.999, step=0.001)
44
+
45
  if st.button("Encrypt Image"):
46
  # Encrypt the image
47
+ encrypted_array = encrypt_image(img_array, key_seed)
48
+ encrypted_image = Image.fromarray(encrypted_array)
49
 
50
+ # Display encrypted image
51
  st.image(encrypted_image, caption="Encrypted Image", use_container_width=True)
52
 
53
  # Save encrypted image to a buffer
 
55
  encrypted_image.save(buffer, format="PNG")
56
  buffer.seek(0)
57
 
58
+ # Download button for encrypted image
59
  st.download_button(
60
  label="Download Encrypted Image",
61
  data=buffer,