Issue
I had problem with pulling video stills from IP camera. To explain a bit further:
- CCTV server is requesting video still as .jpeg image from specific path
- IP camera has different path for file in question
- I've made simple Flask app which pulls .jpg file from camera and serves it on "correct" path for CCTV server
Drawback is that when I pull .jpg file from camera using urllib, I have to save it do disk, and on next step I'm opening same file for serving on Flask endpoint. I'd like to save .jpg file to variable or virtual memory (not on physical drive).
working example:
@app.route('/jpeg', methods=['GET','POST'])
def jpeg():
urllib.request.urlretrieve(url, 'static\images\image.jpg')
filename="static\images\image.jpg"
return send_file(filename, mimetype='image/gif')
I'd like to do something like this:
@app.route('/jpeg', methods=['GET','POST'])
def jpeg():
file=urllib.request.urlretrieve()
return send_file(file)
I have tryed all I could think of using urllib, but apparently, only option for urllib is to save file to physical drive. I haven't found any other working solution for this problem.
Solution
That's not possible using urlretrieve()
.
You can use the requests
library:
response = requests.get(url)
if response.status_code == 200:
image_data = response.content
Additionally you can simplify your current notation like:
@app.route('/jpeg', methods=['GET','POST'])
def jpeg():
local_filename, headers = urllib.request.urlretrieve(url)
return send_file(local_filename, mimetype='image/gif')
In this case, urllib will create a temp file for you request.
If you are concerned about cleanup you can use:
urllib.request.urlcleanup()
The function cleans up temporary files that may have been left behind by previous calls to urlretrieve()
.
Also, this a good candidate for a context manager :)
Answered By - mighty_mike
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.