Skip to content

Instantly share code, notes, and snippets.

@csachs
Created October 18, 2017 09:02
Show Gist options
  • Save csachs/027b353a96f4a82e5cadf906b2ec73a4 to your computer and use it in GitHub Desktop.
Save csachs/027b353a96f4a82e5cadf906b2ec73a4 to your computer and use it in GitHub Desktop.
mandelbrot image feeder
import numpy
from flask import Blueprint, Flask, make_response, request
# MIT Licensed
bp = Blueprint('SimulatedCameraImageFeeder', __name__)
# basing upon https://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial/Mandelbrot_Set_Example.html
def mandelbrot(h, w, maxit=20, x1=0, y1=0, size=0.1):
span = 3
sps = (span*size)
y, x = numpy.ogrid[(-1.5+x1-sps/2):(-1.5+x1+sps):h*1j, (-2+y1-sps/2):(-2+y1+sps):w*1j]
c = x+y*1j
z = c
divtime = maxit + numpy.zeros(z.shape, dtype=int)
for i in range(maxit):
z = z**2 + c
diverge = z*numpy.conj(z) > 2**2
div_now = diverge & (divtime == maxit)
divtime[div_now] = i
z[diverge] = 2
return divtime
@bp.route('/')
def image():
width = int(request.args.get('width'))
height = int(request.args.get('height'))
depth = int(request.args.get('depth'))
channel = int(request.args.get('channel'))
x = float(request.args.get('x'))
y = float(request.args.get('y'))
z = float(request.args.get('z'))
depth_map = {
1: numpy.uint8,
2: numpy.uint16
}
factor = 100.0
result = mandelbrot(height, width,
maxit=25, y1=x/factor, x1=y/factor,
size=1-numpy.exp(-10*numpy.exp(-0.05*z))).astype(depth_map[depth])
return make_response(result.tostring())
def main():
app = Flask(__name__)
app.register_blueprint(bp)
app.run(debug=True, port=8555)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment