Stream image directly from memory instead of buffering to disk

master
Gina Häußge 2013-01-02 18:24:56 +01:00
parent f080468a4d
commit 8dc5e249d5
2 changed files with 23 additions and 4 deletions

View File

@ -2,7 +2,7 @@
__author__ = "Gina Häußge <osd@foosel.net>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
from flask import Flask, request, render_template, jsonify, send_from_directory, abort
from flask import Flask, request, render_template, jsonify, send_file, abort
from werkzeug import secure_filename
from printer_webui.printer import Printer, getConnectionOptions
@ -12,6 +12,7 @@ from printer_webui.webcam import hasWebcamSupport, Webcam
import sys
import os
import fnmatch
import StringIO
BASEURL="/ajax/"
SUCCESS={}
@ -267,9 +268,11 @@ def getImage():
if webcam is None:
abort(404)
filename = "current.jpg"
webcam.save(os.path.join(WEBCAM_FOLDER, filename))
return send_from_directory(WEBCAM_FOLDER, filename)
image = webcam.get()
strIO = StringIO.StringIO()
image.save(strIO, "JPEG", quality=80)
strIO.seek(0)
return send_file(strIO, mimetype="image/jpeg")
#~~ helper functions

View File

@ -13,6 +13,8 @@ try:
except:
win32vidcap = None
import PIL
def hasWebcamSupport():
if cv == None and win32vidcap == None:
return False
@ -30,6 +32,20 @@ class Webcam(object):
except:
pass
def get(self):
if self._cam is None:
return None
if cv is not None:
frame = cv.QueryFrame(self._cam)
image = PIL.Image.fromstring("L", frame.GetSize(), frame.tostring())
return image
elif win32vidcap is not None:
image = self._cam.getImage()
return image
else:
return None
def save(self, filename):
if self._cam is None:
return