From 16fca870869624b902a7999d5e0da3d0042715eb Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 18 Feb 2025 20:28:19 +0100 Subject: [PATCH] Handle thumb errors, normalize non-int8 --- app/api/tasks.py | 14 ++++++++++++++ app/static/app/js/components/TaskListItem.jsx | 9 +++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/api/tasks.py b/app/api/tasks.py index c4118390..29bb6232 100644 --- a/app/api/tasks.py +++ b/app/api/tasks.py @@ -8,6 +8,7 @@ import rasterio from rasterio.enums import ColorInterp from PIL import Image import io +import numpy as np from shutil import copyfileobj, move from django.core.exceptions import ObjectDoesNotExist, SuspiciousFileOperation, ValidationError @@ -453,7 +454,20 @@ class TaskThumbnail(TaskNestedView): thumb_size, thumb_size, ), resampling=rasterio.enums.Resampling.nearest).transpose((1, 2, 0)) + + if img.dtype != np.uint8: + img = img.astype(np.float32) + # Ignore alpha values + minval = img[:,:,:3].min() + maxval = img[:,:,:3].max() + + if minval != maxval: + img[:,:,:3] -= minval + img[:,:,:3] *= (255.0/(maxval-minval)) + + img = img.astype(np.uint8) + img = Image.fromarray(img) output = io.BytesIO() img.save(output, format='PNG', quality=quality) diff --git a/app/static/app/js/components/TaskListItem.jsx b/app/static/app/js/components/TaskListItem.jsx index 2397115a..fcbe477d 100644 --- a/app/static/app/js/components/TaskListItem.jsx +++ b/app/static/app/js/components/TaskListItem.jsx @@ -48,6 +48,7 @@ class TaskListItem extends React.Component { view: "basic", showMoveDialog: false, actionLoading: false, + thumbLoadFailed: false } for (let k in props.data){ @@ -429,6 +430,10 @@ class TaskListItem extends React.Component { } } + handleThumbError = e => { + this.setState({thumbLoadFailed: true}); + } + render() { const task = this.state.task; const name = task.name !== null ? task.name : interpolate(_("Task #%(number)s"), { number: task.id }); @@ -610,10 +615,10 @@ class TaskListItem extends React.Component { - {task.status === statusCodes.COMPLETED ? + {!this.state.thumbLoadFailed && task.status === statusCodes.COMPLETED ?
- {_("Thumbnail")}/ + {_("Thumbnail")}/
: ""}