Skip to content

Commit

Permalink
refactor: simplify image format dtype validation (#1046)
Browse files Browse the repository at this point in the history
* refactor: simplify image format dtype validation

* DOC: changes.md

---------

Co-authored-by: vincentsarago <[email protected]>
  • Loading branch information
pratapvardhan and vincentsarago authored Dec 20, 2024
1 parent f978c3e commit 1846bde
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
5 changes: 3 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

### titiler.core

* Add layer control to map viewer template (author @hrodmn, https://github.com/developmentseed/titiler/pull/1051)
* add layer control to map viewer template (author @hrodmn, https://github.com/developmentseed/titiler/pull/1051)
* improve query string handling in LowerCaseQueryStringMiddleware using urlencode (author @pratapvardhan, https://github.com/developmentseed/titiler/pull/1050)
* add `titiler.core.utils.bounds_to_geometry` and reduce code duplication in factories (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1047)
* simplify image format dtype validation in `render_image` (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1046)

### titiler.application

* update `/healthz` endpoint to return dependencies versions (titiler, rasterio, gdal, ...) (author @scottyhq, https://github.com/developmentseed/titiler/pull/1056)
* migrate `templates/index.html` to bootstrap5, remove unused css, reuse bs classes (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1048)
* add `titiler.core.utils.bounds_to_geometry` and reduce code duplication in factories (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1047)

## 0.19.2 (2024-11-28)

Expand Down
39 changes: 13 additions & 26 deletions src/titiler/core/titiler/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,20 @@ def render_image(
if not output_format:
output_format = ImageType.jpeg if mask.all() else ImageType.png

if output_format == ImageType.png and data.dtype not in ["uint8", "uint16"]:
# format-specific valid dtypes
format_dtypes = {
ImageType.png: ["uint8", "uint16"],
ImageType.jpeg: ["uint8"],
ImageType.jpg: ["uint8"],
ImageType.webp: ["uint8"],
ImageType.jp2: ["uint8", "int16", "uint16"],
}

valid_dtypes = format_dtypes.get(output_format, [])
if valid_dtypes and data.dtype not in valid_dtypes:
warnings.warn(
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.",
InvalidDatatypeWarning,
stacklevel=1,
)
data = rescale_array(data, mask, in_range=datatype_range)

elif output_format in [
ImageType.jpeg,
ImageType.jpg,
ImageType.webp,
] and data.dtype not in ["uint8"]:
warnings.warn(
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.",
InvalidDatatypeWarning,
stacklevel=1,
)
data = rescale_array(data, mask, in_range=datatype_range)

elif output_format == ImageType.jp2 and data.dtype not in [
"uint8",
"int16",
"uint16",
]:
warnings.warn(
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.",
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. "
"Data will be rescaled using min/max type bounds or dataset_statistics.",
InvalidDatatypeWarning,
stacklevel=1,
)
Expand Down

0 comments on commit 1846bde

Please sign in to comment.