-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
89 lines (65 loc) · 3.13 KB
/
API.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
import os
import matplotlib.pyplot as plt
from flask import Flask, request, send_file, send_from_directory, jsonify
from flask import Flask, request, send_file
from flask_cors import CORS
import ImageProcessing
app = Flask(__name__)
CORS(app)
# Endpoint to check the status of the server
@app.route('/status', methods=['GET'])
def status():
return '', 200
@app.route('/embed_secret', methods=['GET', 'POST'])
def embed_secret():
data = request.json
# with open('embed_secret.json', 'r') as embed_file:
# data = json.load(embed_file)
# Extract relevant information from the JSON data
cover_image_base64 = data['cover_image_path']
secret_text = data['secret_text']
output_path = 'imageWithHiddenText/ImageWithHiddenText.png'
# Convert base64-encoded image to NumPy array
imm_array = ImageProcessing.base64_to_image(cover_image_base64)
# Write the image to a file
ImageProcessing.write_image(imm_array, 'imagesInput/image.png')
# Embed the secret text in the image
ImageProcessing.embed_secret('imagesInput/image.png', secret_text, output_path)
# Read the resulting image array
read_img_arr = ImageProcessing.read_image(output_path)
# Convert the image array to base64
base64_extracted_image = ImageProcessing.image_to_base64(read_img_arr)
# layer_folder = 'layers'
# list_layer_base64 = []
# for filename in os.listdir(layer_folder):
# layer_path = os.path.join(layer_folder, filename)
# list_layer_base64.append(ImageProcessing.image_to_base64(ImageProcessing.read_image(layer_path)))
# return jsonify({'status': 'done', 'extracted_image': base64_extracted_image, 'layers': list_layer_base64})
return jsonify({'status': 'done', 'extracted_image': base64_extracted_image})
@app.route('/extract_secret', methods=['GET', 'POST'])
def extract_secret():
'''Endpoint to extract a secret from a image'''
# Extract data from the incoming JSON request
data = request.json
stego_image = data['stego_image_path']
output_path = 'output/extractedText.png'
# Convert base64-encoded image to NumPy array
imm_array = ImageProcessing.base64_to_image(stego_image)
# Write the image to a file
ImageProcessing.write_image(imm_array, 'imageWithHiddenText/ImageWithHiddenText.png')
# Extract the secret text from the steganographic image
ImageProcessing.extract_secret('imageWithHiddenText/ImageWithHiddenText.png', output_path)
# Read the resulting image array
read_image_arr = ImageProcessing.read_image(output_path)
# Convert the image array to base64
base64_extracted_image = ImageProcessing.image_to_base64(read_image_arr)
# Collect base64 representations of image layers in a list
layer_folder = 'layers'
list_layer_base64 = []
for filename in os.listdir(layer_folder):
layer_path = os.path.join(layer_folder, filename)
list_layer_base64.append(ImageProcessing.image_to_base64(ImageProcessing.read_image(layer_path)))
return jsonify({'status': 'done', 'extracted_image': base64_extracted_image, 'layers': list_layer_base64})
if __name__ == '__main__':
app.run(debug=True)