Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from PIL import Image
|
|
| 2 |
import numpy as np
|
| 3 |
import streamlit as st
|
| 4 |
import io
|
|
|
|
| 5 |
import random
|
| 6 |
|
| 7 |
# Resize image to prevent memory issues with large images
|
|
@@ -38,13 +39,6 @@ def shuffle_pixels(img_array, seed):
|
|
| 38 |
shuffled = flattened[indices]
|
| 39 |
return shuffled.reshape(h, w, c), indices
|
| 40 |
|
| 41 |
-
# Unshuffle pixels (for decryption, if needed)
|
| 42 |
-
def unshuffle_pixels(shuffled_array, indices, original_shape):
|
| 43 |
-
flattened = shuffled_array.reshape(-1, shuffled_array.shape[-1])
|
| 44 |
-
unshuffled = np.zeros_like(flattened)
|
| 45 |
-
unshuffled[indices] = flattened
|
| 46 |
-
return unshuffled.reshape(original_shape)
|
| 47 |
-
|
| 48 |
# Multi-layer encryption using logistic map and pixel shuffling
|
| 49 |
def encrypt_image(img_array, seed):
|
| 50 |
h, w, c = img_array.shape
|
|
@@ -75,6 +69,13 @@ def main():
|
|
| 75 |
|
| 76 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 77 |
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Load the uploaded image
|
| 79 |
input_image = Image.open(uploaded_file)
|
| 80 |
|
|
@@ -91,25 +92,32 @@ def main():
|
|
| 91 |
key_seed = st.slider("Set the encryption key seed (0 < key < 1)", min_value=0.001, max_value=0.999, step=0.001)
|
| 92 |
|
| 93 |
if st.button("Encrypt Image"):
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
main()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import streamlit as st
|
| 4 |
import io
|
| 5 |
+
import time
|
| 6 |
import random
|
| 7 |
|
| 8 |
# Resize image to prevent memory issues with large images
|
|
|
|
| 39 |
shuffled = flattened[indices]
|
| 40 |
return shuffled.reshape(h, w, c), indices
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Multi-layer encryption using logistic map and pixel shuffling
|
| 43 |
def encrypt_image(img_array, seed):
|
| 44 |
h, w, c = img_array.shape
|
|
|
|
| 69 |
|
| 70 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 71 |
if uploaded_file is not None:
|
| 72 |
+
with st.spinner("Processing upload..."):
|
| 73 |
+
# Simulate a loading delay for upload progress
|
| 74 |
+
progress_bar = st.progress(0)
|
| 75 |
+
for percent_complete in range(0, 101, 20):
|
| 76 |
+
time.sleep(0.1) # Simulate progress
|
| 77 |
+
progress_bar.progress(percent_complete)
|
| 78 |
+
|
| 79 |
# Load the uploaded image
|
| 80 |
input_image = Image.open(uploaded_file)
|
| 81 |
|
|
|
|
| 92 |
key_seed = st.slider("Set the encryption key seed (0 < key < 1)", min_value=0.001, max_value=0.999, step=0.001)
|
| 93 |
|
| 94 |
if st.button("Encrypt Image"):
|
| 95 |
+
with st.spinner("Encrypting image..."):
|
| 96 |
+
# Simulate a loading delay for encryption progress
|
| 97 |
+
progress_bar = st.progress(0)
|
| 98 |
+
for percent_complete in range(0, 101, 20):
|
| 99 |
+
time.sleep(0.1) # Simulate progress
|
| 100 |
+
progress_bar.progress(percent_complete)
|
| 101 |
+
|
| 102 |
+
# Encrypt the image
|
| 103 |
+
encrypted_array = encrypt_image(img_array, key_seed)
|
| 104 |
+
encrypted_image = Image.fromarray(encrypted_array)
|
| 105 |
+
|
| 106 |
+
# Display encrypted image
|
| 107 |
+
st.image(encrypted_image, caption="Encrypted Image", use_container_width=True)
|
| 108 |
+
|
| 109 |
+
# Save encrypted image to a buffer
|
| 110 |
+
buffer = io.BytesIO()
|
| 111 |
+
encrypted_image.save(buffer, format="PNG")
|
| 112 |
+
buffer.seek(0)
|
| 113 |
+
|
| 114 |
+
# Download button for encrypted image
|
| 115 |
+
st.download_button(
|
| 116 |
+
label="Download Encrypted Image",
|
| 117 |
+
data=buffer,
|
| 118 |
+
file_name="encrypted_image.png",
|
| 119 |
+
mime="image/png"
|
| 120 |
+
)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
main()
|