Skip to content

Commit 8f80957

Browse files
taalbrechtjsbroks
authored andcommitted
Improve the loading speed for image cards (#279)
* Use smaller thumbnails for dataset page to improve the loading speed of image cards * Simplified thumbnail generation code * Corrected error in PIL.Image.thumbnail usage
1 parent a4db2b2 commit 8f80957

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

backend/database/images.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class ImageModel(DynamicDocument):
1818
THUMBNAIL_DIRECTORY = '.thumbnail'
1919
PATTERN = (".gif", ".png", ".jpg", ".jpeg", ".bmp", ".GIF", ".PNG", ".JPG", ".JPEG", ".BMP")
2020

21+
# Set maximum thumbnail size (h x w) to use on dataset page
22+
MAX_THUMBNAIL_DIM = (1024, 1024)
23+
2124
# -- Private
2225
_dataset = None
2326

@@ -89,9 +92,6 @@ def thumbnail(self):
8992
"""
9093
Generates (if required) and returns thumbnail
9194
"""
92-
if not self.annotated:
93-
self.thumbnail_delete()
94-
return Image.open(self.path)
9595

9696
thumbnail_path = self.thumbnail_path()
9797

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

103103
pil_image = self.generate_thumbnail()
104104
pil_image = pil_image.convert("RGB")
105-
pil_image.save(thumbnail_path)
105+
106+
# Resize image to fit in MAX_THUMBNAIL_DIM envelope as necessary
107+
pil_image.thumbnail((self.MAX_THUMBNAIL_DIM[1], self.MAX_THUMBNAIL_DIM[0]))
108+
109+
# Save as a jpeg to improve loading time
110+
# (note file extension will not match but allows for backwards compatibility)
111+
pil_image.save(thumbnail_path, "JPEG", quality=80, optimize=True, progressive=True)
106112

107113
self.update(is_modified=False)
108114
return pil_image

backend/webserver/api/annotator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def post(self):
128128
set__metadata=image.get('metadata', {}),
129129
set__annotated=annotated,
130130
set__category_ids=image.get('category_ids', []),
131-
set__regenerate_thumbnail=annotated,
131+
set__regenerate_thumbnail=True,
132132
set__num_annotations=annotations\
133133
.filter(deleted=False, area__gt=0).count()
134134
)

client/src/components/cards/ImageCard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default {
146146
computed: {
147147
imageUrl() {
148148
let d = new Date();
149-
if (this.image.annotated && this.showAnnotations) {
149+
if (this.showAnnotations) {
150150
return `/api/image/${
151151
this.image.id
152152
}?width=250&thumbnail=true&dummy=${d.getTime()}`;

0 commit comments

Comments
 (0)