From 541b9184bda3b1906e8595d9f6452b1d182e9a5c Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Tue, 30 Jul 2019 20:34:33 +1000 Subject: [PATCH] First draft of API in Python --- app.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 2 files changed, 64 insertions(+) create mode 100644 app.py create mode 100644 requirements.txt diff --git a/app.py b/app.py new file mode 100644 index 0000000..a6cdaef --- /dev/null +++ b/app.py @@ -0,0 +1,61 @@ +from flask import Flask, request, send_file +#import numpy +from PIL import Image, ImageChops +from io import BytesIO +import numpy + +app = Flask(__name__) + +usage = """ +Post image Data to /desteg and receive a copy of that image with the least significant bits XORed with system entropy + +Example: curl -H "Content-Type: application/png" --data-binary @image.png http://localhost:5002/desteg -o image_destegged.png +""" + +@app.route("/desteg", methods=["GET"]) +def get_desteg(): + return usage + +@app.route("/desteg", methods=["POST"]) +def post_desteg(): + + image_string = BytesIO(request.data) + output_buffer = BytesIO() + + try: + input_image = Image.open(image_string) + except IOError: + # insert relevant error return here + pass + + + + # Create an array the same size + # Stuff it full of entropy + if (input_image.mode == "RGBA"): + entropy_array = numpy.random.rand(input_image.size[1],input_image.size[0],4)*3 + entropy_image = Image.fromarray(entropy_array.astype('uint8'), mode="RGBA") + elif (input_image.mode == "RGB"): + entropy_array = numpy.random.rand(input_image.size[1],input_image.size[0],3)*3 + entropy_image = Image.fromarray(entropy_array.astype('uint8'), mode="RGB") + else: + # Insert relevant error return here + pass + + # Add the entropy to input + output_image = ImageChops.add(input_image, entropy_image) + + # save with same format as output + output_image.save(output_buffer,format=input_image.format) + + #return buffer to start! + output_buffer.seek(0) + return send_file(output_buffer, + mimetype="image/" + input_image.format.lower()) + +@app.route("/") +def root(): + return usage + +if __name__ == '__main__': + app.run(port='5002') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..838c2ed --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +numpy==1.16.2 +Flask==1.0.2 +Pillow==6.1.0