python - How do you serve a dynamically downloaded image to a browser using flask? -
i'm working ip camera. can use url such 1 grab static image off camera:
http://username:password@ip_of_camera:port/streaming/channels/1/picture
what want have python/flask download image url when client loads page, , embed image page using img tag.
if have template looks this:
<html> <head> <title>test</title> </head> <body> <img src="{{ image }}"> </body> </html>
how replace {{ image }} downloaded image?
would use urllib/requests download image flask's static folder, replace {{ image }} {{ url_for('static', filename="temp_image.png") }}
, , delete image static folder when page loads? download someplace else instead (other static folder)? or there other way keeps image in memory?
ps. know it's possible replace {{ image }} url directly, reveals username/password/ip/port of camera client.
i add masking route on flask fetches , serves image directly. lets domain.com/image/user1/cam1
your server typically make http request camera , once receives response, can straight serve response
object appropriate mimetype.
in case, image fetched camera resides in ram.
@app.route('image/<userid>/<camid>') def fun(userid,camid): # fetch picture appropriate cam pic = requests.get('http://'+ 'username:password'+ # dynamically replace user id / password/ auth '@ip_of_camera:port'+ #dynamically replace port / ip '/streaming/channels/1/picture') # processing of pic here.. return response(pic,mimetype="image/png")
however, if image needs served on , on again, might wanna cache it. in case, pick closer approach.
if want stream camera images, whole different ballgame.
Comments
Post a Comment