Added capture and capture previews to web UI

This commit is contained in:
Joel Collins 2018-11-12 16:22:49 +00:00
parent 49331d36a8
commit d75efa630b
5 changed files with 252 additions and 30 deletions

View file

@ -4,6 +4,10 @@ import os
import datetime
import copy
import logging
from PIL import Image
pil_formats = ['JPG', 'JPEG', 'PNG', 'TIF', 'TIFF']
thumbnail_size = (60, 60)
class StreamObject(object):
@ -19,6 +23,9 @@ class StreamObject(object):
self.id = uuid.uuid4().hex
logging.info("Created {}".format(self.id))
# Store file format
self.format = fmt
# Create file name
iterator = 0
f_path, f_name = self.build_file_path(filename, folder, fmt)
@ -55,6 +62,9 @@ class StreamObject(object):
# Object lock
self.locked = False
# Thumbnail (populated only for JPEG captures)
self.thumb_bytes = None
def __enter__(self):
"""Create StreamObject in context, to auto-clean disk data."""
logging.debug("Entering context for {}.\
@ -190,6 +200,25 @@ class StreamObject(object):
"""Return a byte string of the capture data."""
return self.data.getvalue()
@property
def thumbnail(self) -> io.BytesIO:
# If no thumbnail exists, try and make one
if not self.thumb_bytes:
print("Building thumbnail")
if self.format.upper() in pil_formats:
im = Image.open(self.data)
im.thumbnail(thumbnail_size)
self.thumb_bytes = io.BytesIO()
im.save(self.thumb_bytes, self.format)
self.thumb_bytes.seek(0)
else:
self.thumb_bytes.seek(0)
# Copy the buffer, to avoid closing the file
data = io.BytesIO(self.thumb_bytes.getbuffer())
return data
def load_file(self) -> bool:
"""Load data stored on disk to the in-memory stream."""
if self.file_exists: # If data file exists