|
| 1 | +import numpy as np |
| 2 | +from PIL import Image |
| 3 | +from scripts.random_grid.rg_grayscale_additive_SS import create_random_grid, create_difference_grid |
| 4 | + |
| 5 | + |
| 6 | +# Returns a dictionary containing function mappings and metadata for the algorithm (used by the web interface). |
| 7 | +def get_config(): |
| 8 | + return { |
| 9 | + "name": "RG - Color (RGB) Additive Secret Sharing", |
| 10 | + "description": get_description(), |
| 11 | + "requirements": get_requirements(), |
| 12 | + "encrypt": encrypt, |
| 13 | + "decrypt": decrypt, |
| 14 | + "extension": "png", |
| 15 | + "image_type": "RGB" |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | +# Defines the expected inputs for encryption/decryption (number of images and additional parameters) |
| 20 | +def get_requirements(): |
| 21 | + return { |
| 22 | + "encryption": { |
| 23 | + "num_images": 1, |
| 24 | + "parameters": {} # No extra parameters needed |
| 25 | + }, |
| 26 | + "decryption": { |
| 27 | + "num_images": 2, |
| 28 | + "parameters": {} # No extra parameters needed |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +# Returns a dictionary containing the description and reference links for the algorithm. |
| 34 | +def get_description(): |
| 35 | + return { |
| 36 | + "text": "This (2,2) Visual Secret Sharing Scheme extends the general additive secret sharing scheme to handle RGB color images.", |
| 37 | + "links": [ |
| 38 | + {"text": "Kafri & Keren", |
| 39 | + "url": "https://doi.org/10.1364/ol.12.000377"} |
| 40 | + ] |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +# Function to encrypt an image by generating two shares using random grids and difference grids |
| 45 | +def encrypt(image): |
| 46 | + """ |
| 47 | + Encrypts an image by generating two shares using random grids and difference grids. |
| 48 | +
|
| 49 | + Parameters: |
| 50 | + image (PIL.Image.Image): The input image (PIL Image object) to be encrypted. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + tuple: A tuple containing two PIL.Image.Image objects (grid1_image and grid2_image), |
| 54 | + representing the encrypted shares of the original image. |
| 55 | + """ |
| 56 | + img_array = np.array(image).astype(int) # Convert PIL Image to numpy array and convert to int type |
| 57 | + |
| 58 | + # Create the first random grid for each channel and stack them |
| 59 | + grid1 = np.stack([create_random_grid(img_array.shape[:2]) for _ in range(3)], axis=-1) |
| 60 | + grid1_image = Image.fromarray(grid1) |
| 61 | + |
| 62 | + # Create the second grid using modular subtraction |
| 63 | + grid2 = create_difference_grid(img_array, grid1) |
| 64 | + grid2_image = Image.fromarray(grid2.astype(np.uint8)) |
| 65 | + |
| 66 | + return grid1_image, grid2_image |
| 67 | + |
| 68 | + |
| 69 | +# Function to decrypt two images by overlaying the grids |
| 70 | +def decrypt(image1, image2): |
| 71 | + """ |
| 72 | + Combines two grids by adding the second grid to the first. |
| 73 | +
|
| 74 | + Parameters: |
| 75 | + image1 (PIL.Image.Image): The first image (PIL Image object), to be converted to numpy array and processed. |
| 76 | + image2 (PIL.Image.Image): The second image (PIL Image object), to be converted to numpy array and processed. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + PIL.Image.Image: The resulting image (PIL Image object) after adding the two grids. |
| 80 | + """ |
| 81 | + img1_array = np.array(image1.convert('RGB')) # Convert PIL Image to numpy array (RGB) |
| 82 | + img2_array = np.array(image2.convert('RGB')) # Convert PIL Image to numpy array (RGB) |
| 83 | + |
| 84 | + # Combine the grids using modular addition |
| 85 | + decrypted = img1_array + img2_array |
| 86 | + return Image.fromarray(decrypted.astype(np.uint8)) # Convert numpy array back to PIL Image |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + image_path = '../images/test.png' |
| 91 | + output_path = '../images/output/' |
| 92 | + |
| 93 | + # Load and convert the input image to RGB |
| 94 | + image = Image.open(image_path).convert('RGB') |
| 95 | + |
| 96 | + # ENCRYPT: Generate shares |
| 97 | + share1, share2 = encrypt(image) |
| 98 | + share1.save(output_path + "RG1.png") |
| 99 | + share2.save(output_path + "RG2.png") |
| 100 | + |
| 101 | + # DECRYPT: Overlay the grids |
| 102 | + img_share1 = Image.open(output_path + "RG1.png") # PIL Image (RGB) |
| 103 | + img_share2 = Image.open(output_path + "RG2.png") # PIL Image (RGB) |
| 104 | + |
| 105 | + out = decrypt(img_share1, img_share2) |
| 106 | + out.save(output_path + "decrypted_image.png") |
0 commit comments