Skip to content

Commit 1846bde

Browse files
refactor: simplify image format dtype validation (#1046)
* refactor: simplify image format dtype validation * DOC: changes.md --------- Co-authored-by: vincentsarago <[email protected]>
1 parent f978c3e commit 1846bde

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
### titiler.core
66

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

1012
### titiler.application
1113

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

1617
## 0.19.2 (2024-11-28)
1718

src/titiler/core/titiler/core/utils.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,20 @@ def render_image(
6969
if not output_format:
7070
output_format = ImageType.jpeg if mask.all() else ImageType.png
7171

72-
if output_format == ImageType.png and data.dtype not in ["uint8", "uint16"]:
72+
# format-specific valid dtypes
73+
format_dtypes = {
74+
ImageType.png: ["uint8", "uint16"],
75+
ImageType.jpeg: ["uint8"],
76+
ImageType.jpg: ["uint8"],
77+
ImageType.webp: ["uint8"],
78+
ImageType.jp2: ["uint8", "int16", "uint16"],
79+
}
80+
81+
valid_dtypes = format_dtypes.get(output_format, [])
82+
if valid_dtypes and data.dtype not in valid_dtypes:
7383
warnings.warn(
74-
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.",
75-
InvalidDatatypeWarning,
76-
stacklevel=1,
77-
)
78-
data = rescale_array(data, mask, in_range=datatype_range)
79-
80-
elif output_format in [
81-
ImageType.jpeg,
82-
ImageType.jpg,
83-
ImageType.webp,
84-
] and data.dtype not in ["uint8"]:
85-
warnings.warn(
86-
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.",
87-
InvalidDatatypeWarning,
88-
stacklevel=1,
89-
)
90-
data = rescale_array(data, mask, in_range=datatype_range)
91-
92-
elif output_format == ImageType.jp2 and data.dtype not in [
93-
"uint8",
94-
"int16",
95-
"uint16",
96-
]:
97-
warnings.warn(
98-
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.",
84+
f"Invalid type: `{data.dtype}` for the `{output_format}` driver. "
85+
"Data will be rescaled using min/max type bounds or dataset_statistics.",
9986
InvalidDatatypeWarning,
10087
stacklevel=1,
10188
)

0 commit comments

Comments
 (0)