Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fill value not respected when saving palette images while keeping palette #180

Open
gerritholl opened this issue Aug 19, 2024 · 2 comments · May be fixed by #181
Open

fill value not respected when saving palette images while keeping palette #180

gerritholl opened this issue Aug 19, 2024 · 2 comments · May be fixed by #181
Labels

Comments

@gerritholl
Copy link
Contributor

gerritholl commented Aug 19, 2024

Describe the bug

When saving a palette image (mode P) with keep_palette=True, XRImage._replace_fill_value does not get called and fill values are not applied.

To Reproduce

import numpy as np
import xarray as xr
import trollimage.xrimage
import rasterio

arr = np.ones((1, 5, 5), dtype="uint8")
arr[0, 2, 2] = 255
data = xr.DataArray(
        arr, dims=["bands", 'y', 'x'],
        attrs={"_FillValue": 255},
        coords={"bands": ["P"]})
img = trollimage.xrimage.XRImage(data)
img.save("test.tif", keep_palette=True, fill_value=42)
f = rasterio.open("test.tif")
cont = f.read()
print(cont)

Expected behavior

I expect that the fill value is replaced, like it is when keep_palette=False (the default).

[[[  1   1   1   1   1]
  [  1   1   1   1   1]
  [  1   1  42   1   1]
  [  1   1   1   1   1]
  [  1   1   1   1   1]]]

Actual results

In reality, the fill value is ignored:

[[[  1   1   1   1   1]
  [  1   1   1   1   1]
  [  1   1 255   1   1]
  [  1   1   1   1   1]
  [  1   1   1   1   1]]]

Environment Info:

  • trollimage Version: main

Additional context

It's explicitly excluded:

if not keep_palette:
final_data = self._scale_alpha_or_fill_data(final_data, fill_value, dtype)

The exclusion was there ever since keep_palette was introduced in #39.

@gerritholl gerritholl added the bug label Aug 19, 2024
@djhoese
Copy link
Member

djhoese commented Aug 19, 2024

I think we need to be really careful with the solution for this. As you know, palettize is different from the other enhancement methods/functions as the resulting values are indexes into a color table/map. So I think there is an assumption that every value in the result is a valid index in the colormap. In both the current result and expected result I could see this being hard to ensure. Also, should the fill value be replaced as an index (as you've described it) or should it be done before the palettizing?

@gerritholl
Copy link
Contributor Author

Every value is a valid index in the colormap, but the colormap contains no information about the interpretation of the colors (legend / palette meanings). We can define the color with index 0 to be used for missing data, or the color with index 255, or another value, but this information is not contained in the color table itself (at most implicitly via the alpha value, but pixels mapping to transparency are not necessarily invalid data).

In the current result, we force the user to use the color with index 255 for missing data. My user (NinJo) has asked to use the color with index 0 for missing data. This suits well for nwcsaf-geo cloud_top_height, where the colour for pixels with value 0 has alpha 0 (fully transparent), whereas the colour for pixel value 255 is black.

Hence this issue and the sister issue #178. The NWCSAF-GEO CTH data use a pixel value of zero for missing data, but there is currently no way to encode this information in the output file for a mode P GeoTIFF image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants