Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,14 @@ from PIL import Image
|
|
| 2 |
import numpy as np
|
| 3 |
import streamlit as st
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
# Resize image to prevent memory issues with large images
|
| 7 |
def resize_image(img, max_size=(3000, 3000)):
|
| 8 |
"""
|
| 9 |
Resize image to fit within max_size while maintaining aspect ratio.
|
| 10 |
-
:param img: PIL Image object
|
| 11 |
-
:param max_size: Maximum dimensions (width, height)
|
| 12 |
-
:return: Resized PIL Image object
|
| 13 |
"""
|
| 14 |
-
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
| 15 |
return img
|
| 16 |
|
| 17 |
# Logistic map function
|
|
@@ -27,19 +25,53 @@ def generate_key(seed, n):
|
|
| 27 |
key.append(int(x * 255) % 256) # Map to 0-255
|
| 28 |
return np.array(key, dtype=np.uint8)
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def encrypt_image(img_array, seed):
|
| 32 |
h, w, c = img_array.shape
|
| 33 |
flat_image = img_array.flatten()
|
|
|
|
|
|
|
| 34 |
chaotic_key = generate_key(seed, len(flat_image))
|
|
|
|
|
|
|
| 35 |
encrypted_flat = [pixel ^ chaotic_key[i] for i, pixel in enumerate(flat_image)]
|
| 36 |
encrypted_array = np.array(encrypted_flat, dtype=np.uint8).reshape(h, w, c)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Streamlit App
|
| 40 |
def main():
|
| 41 |
-
st.title("Chaotic Logistic Map Image Encryption")
|
| 42 |
-
st.write("Upload an image to encrypt using
|
| 43 |
|
| 44 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 45 |
if uploaded_file is not None:
|
|
|
|
| 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
|
| 8 |
def resize_image(img, max_size=(3000, 3000)):
|
| 9 |
"""
|
| 10 |
Resize image to fit within max_size while maintaining aspect ratio.
|
|
|
|
|
|
|
|
|
|
| 11 |
"""
|
| 12 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
| 13 |
return img
|
| 14 |
|
| 15 |
# Logistic map function
|
|
|
|
| 25 |
key.append(int(x * 255) % 256) # Map to 0-255
|
| 26 |
return np.array(key, dtype=np.uint8)
|
| 27 |
|
| 28 |
+
# Shuffle pixels using a chaotic sequence
|
| 29 |
+
def shuffle_pixels(img_array, seed):
|
| 30 |
+
h, w, c = img_array.shape
|
| 31 |
+
num_pixels = h * w
|
| 32 |
+
flattened = img_array.reshape(-1, c)
|
| 33 |
+
indices = np.arange(num_pixels)
|
| 34 |
+
|
| 35 |
+
random.seed(seed)
|
| 36 |
+
random.shuffle(indices) # Shuffle indices
|
| 37 |
+
|
| 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
|
| 51 |
flat_image = img_array.flatten()
|
| 52 |
+
|
| 53 |
+
# Generate chaotic key
|
| 54 |
chaotic_key = generate_key(seed, len(flat_image))
|
| 55 |
+
|
| 56 |
+
# XOR-based encryption
|
| 57 |
encrypted_flat = [pixel ^ chaotic_key[i] for i, pixel in enumerate(flat_image)]
|
| 58 |
encrypted_array = np.array(encrypted_flat, dtype=np.uint8).reshape(h, w, c)
|
| 59 |
+
|
| 60 |
+
# Pixel shuffling
|
| 61 |
+
shuffled_array, indices = shuffle_pixels(encrypted_array, seed)
|
| 62 |
+
|
| 63 |
+
# Second layer of logistic map encryption
|
| 64 |
+
chaotic_key_2 = generate_key(seed * 1.1, len(flat_image))
|
| 65 |
+
shuffled_flat = shuffled_array.flatten()
|
| 66 |
+
doubly_encrypted_flat = [pixel ^ chaotic_key_2[i] for i, pixel in enumerate(shuffled_flat)]
|
| 67 |
+
doubly_encrypted_array = np.array(doubly_encrypted_flat, dtype=np.uint8).reshape(h, w, c)
|
| 68 |
+
|
| 69 |
+
return doubly_encrypted_array
|
| 70 |
|
| 71 |
# Streamlit App
|
| 72 |
def main():
|
| 73 |
+
st.title("Enhanced Chaotic Logistic Map Image Encryption")
|
| 74 |
+
st.write("Upload an image to encrypt using advanced chaotic logistic map methods.")
|
| 75 |
|
| 76 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 77 |
if uploaded_file is not None:
|