Skip to content

Improve loading speed for image cards #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions backend/database/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ImageModel(DynamicDocument):
THUMBNAIL_DIRECTORY = '.thumbnail'
PATTERN = (".gif", ".png", ".jpg", ".jpeg", ".bmp", ".GIF", ".PNG", ".JPG", ".JPEG", ".BMP")

# Set maximum thumbnail size (h x w) to use on dataset page
MAX_THUMBNAIL_DIM = (1024, 1024)

# -- Private
_dataset = None

Expand Down Expand Up @@ -89,9 +92,6 @@ def thumbnail(self):
"""
Generates (if required) and returns thumbnail
"""
if not self.annotated:
self.thumbnail_delete()
return Image.open(self.path)

thumbnail_path = self.thumbnail_path()

Expand All @@ -102,7 +102,13 @@ def thumbnail(self):

pil_image = self.generate_thumbnail()
pil_image = pil_image.convert("RGB")
pil_image.save(thumbnail_path)

# Resize image to fit in MAX_THUMBNAIL_DIM envelope as necessary
pil_image.thumbnail((self.MAX_THUMBNAIL_DIM[1], self.MAX_THUMBNAIL_DIM[0]))

# Save as a jpeg to improve loading time
# (note file extension will not match but allows for backwards compatibility)
pil_image.save(thumbnail_path, "JPEG", quality=80, optimize=True, progressive=True)

self.update(is_modified=False)
return pil_image
Expand Down
2 changes: 1 addition & 1 deletion backend/webserver/api/annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def post(self):
set__metadata=image.get('metadata', {}),
set__annotated=annotated,
set__category_ids=image.get('category_ids', []),
set__regenerate_thumbnail=annotated,
set__regenerate_thumbnail=True,
set__num_annotations=annotations\
.filter(deleted=False, area__gt=0).count()
)
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/cards/ImageCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default {
computed: {
imageUrl() {
let d = new Date();
if (this.image.annotated && this.showAnnotations) {
if (this.showAnnotations) {
return `/api/image/${
this.image.id
}?width=250&thumbnail=true&dummy=${d.getTime()}`;
Expand Down