Skip to content

Commit 21d462a

Browse files
add alpha_mask attribute to ImageData and refactor mask (#814)
* feature: add alpha-mask to ImageData * Apply suggestions from code review
1 parent acb6869 commit 21d462a

File tree

15 files changed

+532
-225
lines changed

15 files changed

+532
-225
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
# Unreleased
33

4+
* only cast data to `uint8` if colormap values are of type `uint8`
5+
* add `alpha_mask` attribute to `ImageData` class
6+
* allow partial alpha values from alpha band
7+
* better handle non-uint8 alpha band
8+
* remove deprecated `force_binary_mask` option in `reader.read` function **breaking change**
9+
410
# 7.8.1 (2025-06-16)
511

612
* apply scale/offset to dataset statistics in ImageData object (used for automatic rescaling)

docs/src/models.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This class has helper methods like `render` which forward internal data and mask
1515
- **band_names**: image band's names
1616
- **dataset_statistics**: Dataset's min/max values (list of (min,max), optional)
1717
- **cutline_mask**: array representing the mask for `feature` methods
18+
- **alpha_nask**: array reprsenting the alpha mask (allowing partial transparency)
1819

1920
```python
2021
import numpy

rio_tiler/colormap.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""rio-tiler colormap functions and classes."""
22

3+
import itertools
34
import json
45
import os
56
import pathlib
@@ -166,9 +167,10 @@ def apply_discrete_cmap(
166167

167168
data = numpy.transpose(res, [2, 0, 1])
168169

169-
# If the output data has values between 0-255
170+
# If colormap values are between 0-255
170171
# we cast the output array to Uint8
171-
if data.min() >= 0 and data.max() <= 255:
172+
cmap_v = list(itertools.chain(*colormap.values()))
173+
if min(cmap_v) >= 0 and max(cmap_v) <= 255:
172174
data = data.astype("uint8")
173175

174176
return data[:-1], data[-1]
@@ -206,9 +208,10 @@ def apply_intervals_cmap(
206208

207209
data = numpy.transpose(res, [2, 0, 1])
208210

209-
# If the output data has values between 0-255
211+
# If colormap values are between 0-255
210212
# we cast the output array to Uint8
211-
if data.min() >= 0 and data.max() <= 255:
213+
cmap_v = list(itertools.chain(*[v for k, v in colormap]))
214+
if min(cmap_v) >= 0 and max(cmap_v) <= 255:
212215
data = data.astype("uint8")
213216

214217
return data[:-1], data[-1]

rio_tiler/io/rasterio.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ def tile( # type: ignore
637637
tilesize: int = 256,
638638
indexes: Optional[Indexes] = None,
639639
expression: Optional[str] = None,
640-
force_binary_mask: bool = True,
641640
out_dtype: Optional[Union[str, numpy.dtype]] = None,
642641
resampling_method: RIOResampling = "nearest",
643642
unscale: bool = False,
@@ -654,7 +653,6 @@ def tile( # type: ignore
654653
tilesize (int, optional): Output image size. Defaults to `256`.
655654
indexes (int or sequence of int, optional): Band indexes.
656655
expression (str, optional): rio-tiler expression (e.g. b1/b2+b3).
657-
force_binary_mask (bool, optional): Cast returned mask to binary values (0 or 255). Defaults to `True`.
658656
resampling_method (RIOResampling, optional): RasterIO resampling algorithm. Defaults to `nearest`.
659657
unscale (bool, optional): Apply 'scales' and 'offsets' on output data value. Defaults to `False`.
660658
post_process (callable, optional): Function to apply on output data and mask values.
@@ -675,7 +673,6 @@ def tile( # type: ignore
675673
max_size=None,
676674
indexes=indexes,
677675
expression=expression,
678-
force_binary_mask=force_binary_mask,
679676
out_dtype=out_dtype,
680677
resampling_method=resampling_method,
681678
unscale=unscale,
@@ -690,7 +687,6 @@ def part( # type: ignore
690687
max_size: Optional[int] = None,
691688
height: Optional[int] = None,
692689
width: Optional[int] = None,
693-
force_binary_mask: bool = True,
694690
out_dtype: Optional[Union[str, numpy.dtype]] = None,
695691
resampling_method: RIOResampling = "nearest",
696692
unscale: bool = False,
@@ -707,7 +703,6 @@ def part( # type: ignore
707703
max_size (int, optional): Limit the size of the longest dimension of the dataset read, respecting bounds X/Y aspect ratio.
708704
height (int, optional): Output height of the array.
709705
width (int, optional): Output width of the array.
710-
force_binary_mask (bool, optional): Cast returned mask to binary values (0 or 255). Defaults to `True`.
711706
resampling_method (RIOResampling, optional): RasterIO resampling algorithm. Defaults to `nearest`.
712707
unscale (bool, optional): Apply 'scales' and 'offsets' on output data value. Defaults to `False`.
713708
post_process (callable, optional): Function to apply on output data and mask values.
@@ -733,7 +728,6 @@ def part( # type: ignore
733728
width=width,
734729
height=height,
735730
indexes=indexes,
736-
force_binary_mask=force_binary_mask,
737731
out_dtype=out_dtype,
738732
resampling_method=resampling_method,
739733
unscale=unscale,
@@ -804,7 +798,6 @@ def feature( # type: ignore
804798
max_size: Optional[int] = None,
805799
height: Optional[int] = None,
806800
width: Optional[int] = None,
807-
force_binary_mask: bool = True,
808801
out_dtype: Optional[Union[str, numpy.dtype]] = None,
809802
resampling_method: RIOResampling = "nearest",
810803
unscale: bool = False,
@@ -824,7 +817,6 @@ def feature( # type: ignore
824817
max_size=max_size,
825818
height=height,
826819
width=width,
827-
force_binary_mask=force_binary_mask,
828820
out_dtype=out_dtype,
829821
resampling_method=resampling_method,
830822
unscale=unscale,

0 commit comments

Comments
 (0)