import base64 import io from PIL import Image from flask import Flask, request, jsonify import matplotlib.image as mpimg import numpy as np import numba app = Flask(__name__) @numba.njit def optimized_mandelbrot(n_rows, n_columns, iterations, cx, cy): x_cor = np.linspace(-2, 2, n_rows) y_cor = np.linspace(-2, 2, n_columns) output = np.zeros((n_rows,n_columns)) c = cx + 1j * cy for i in range(n_rows): for j in range(n_columns): z = x_cor[i] + y_cor[j] * 1j count = 0 for k in range(iterations): z = (z*z) + c count += 1 if np.abs(z) > 4: break output[i, j] = count return output.T def open_image_as_array(path): return mpimg.imread(path) def get_encoded_img(arr): img = Image.fromarray(arr).convert("L") img_byte_arr = io.BytesIO() img.save(img_byte_arr, format='PNG') encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii') return encoded_img @app.route('/getImage', methods=['GET']) def get_image(): name = request.args.get('name') fractal = optimized_mandelbrot(1000, 1000, np.random.randint(2, 251), np.random.uniform(-1, 1), np.random.uniform(-1, 1)) img = get_encoded_img(fractal) return jsonify({ "code": 0, "image": img, "first_time": 1 }) def start_app(): app.run() if __name__ == '__main__': start_app()