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