Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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(
|
| 8 |
-
x
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def main():
|
| 25 |
st.title("Chaotic Logistic Map Image Encryption")
|
| 26 |
-
st.write("Upload an image to encrypt using chaotic logistic map
|
| 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 |
-
#
|
| 32 |
input_image = Image.open(uploaded_file)
|
| 33 |
-
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
|
| 38 |
# Key input
|
| 39 |
-
|
|
|
|
| 40 |
if st.button("Encrypt Image"):
|
| 41 |
# Encrypt the image
|
| 42 |
-
|
| 43 |
-
encrypted_image = Image.fromarray(
|
| 44 |
|
| 45 |
-
# Display
|
| 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 |
-
#
|
| 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,
|