diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index ba1146c..9bbb7ca 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -31,6 +31,7 @@ Fixed
 Changed
 *******
 - ``raster.Image()``: changed ``dn2toa(platform, metadata, wavelengths)`` to ``dn2toa(platform, mtl_file, wavelengths)`` #17
+- ``raster.Image()``: explicit dtype when writing, optional compression #32
 - ``raster.Image()``: auto-update of in-memory `dataset` #35
 
 
diff --git a/ukis_pysat/raster.py b/ukis_pysat/raster.py
index fa4d337..97fd77c 100644
--- a/ukis_pysat/raster.py
+++ b/ukis_pysat/raster.py
@@ -9,6 +9,7 @@ try:
     import rasterio
     import rasterio.mask
     from rasterio import windows
+    from rasterio.dtypes import get_minimum_dtype
     from rasterio.io import MemoryFile
     from rasterio.plot import reshape_as_image
     from rasterio.warp import calculate_default_transform, reproject
@@ -360,13 +361,18 @@ class Image:
         self.da_arr = da.from_array(self.__arr, chunks=chunk_size)
         return self.da_arr
 
-    def write_to_file(self, path_to_file, dtype=rasterio.uint16, driver="GTiff"):
+    def write_to_file(self, path_to_file, dtype, driver="GTiff", compress=None):
         """
         Write a dataset to file.
         :param path_to_file: str, path to new file
-        :param dtype: datatype, optional (default: rasterio.uint16)
+        :param dtype: datatype, like np.uint16, 'float32' or 'min' to use the minimum type to represent values
+
         :param driver: str, optional (default: 'GTiff')
+        :param compress: compression, e.g. 'lzw' (default: None)
         """
+        if dtype == 'min':
+            dtype = get_minimum_dtype(self.__arr)
+
         profile = self.dataset.meta
         profile.update(
             {
@@ -379,6 +385,9 @@ class Image:
             }
         )
 
+        if compress:
+            profile.update({"compress": compress})
+
         with rasterio.open(path_to_file, "w", **profile) as dst:
             dst.write(self.__arr.astype(dtype))
 

Image write compression and dtype
The [Image.write_to_file()](https://github.com/dlr-eoc/ukis-pysat/blob/master/ukis_pysat/raster.py#L341) method could be expanded to support optional image compression (e.g. Packbits or LZW). This is necessary to reduce the storage space for example when saving binary masks to disk. 

To achieve this we basically would have to update the profile with "compress" and optionally also "nbits" attributes. Beyond this, the "tiled" attribute could be useful to support.

Also this method defines as default dtype uint16. It would be more reasonable to set the default dtype to the dtype of the Image array in my opinion. This would allow us to stay consistent between image loading and writing.
diff --git a/tests/test_raster.py b/tests/test_raster.py
index ca6b7fb..42ee9d3 100644
--- a/tests/test_raster.py
+++ b/tests/test_raster.py
@@ -5,8 +5,8 @@ import dask.array
 import numpy as np
 from rasterio import windows
 from rasterio.coords import BoundingBox
-from shapely.geometry import box
 from rasterio.transform import from_bounds
+from shapely.geometry import box
 
 from ukis_pysat.members import Platform
 from ukis_pysat.raster import Image
@@ -187,13 +187,21 @@ class DataTest(unittest.TestCase):
         self.assertIsInstance(img.to_dask_array(chunk_size=(1, 10, 10)), dask.array.core.Array)
 
     def test_write_to_file(self):
-        img.write_to_file(r"result.tif")
+        img.write_to_file(r"result.tif", np.uint16)
         img2 = Image("result.tif")
         self.assertTrue(np.array_equal(img2.arr, img.arr))
 
         img2.close()
         os.remove(r"result.tif")
 
+        img.write_to_file(r"result.tif", "min", compress="lzw")
+        img2 = Image("result.tif")
+        self.assertEqual(img2.arr.dtype, "uint8")
+        self.assertEqual(img2.dataset.profile["compress"], "lzw")
+
+        img2.close()
+        os.remove(r"result.tif")
+
 
 if __name__ == "__main__":
     unittest.main()

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
3.7
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
affine==2.4.0
alabaster==0.7.13
appdirs==1.4.4
attrs==24.2.0
Babel==2.14.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
chardet==3.0.4
click==7.1.2
click-plugins==1.1.1
cligj==0.7.2
dask==2.14.0
decopatch==1.4.10
docutils==0.19
exceptiongroup==1.2.2
Fiona==1.8.22
geographiclib==2.0
geojson==3.2.0
geomet==1.1.0
geopy==2.4.1
html2text==2020.1.16
idna==2.10
imagesize==1.4.1
importlib-metadata==6.7.0
iniconfig==2.0.0
Jinja2==3.1.6
landsatxplore==0.6
makefun==1.15.6
MarkupSafe==2.1.5
munch==4.0.0
numpy==1.18.5
packaging==24.0
pandas==1.0.5
Pillow==7.1.2
pluggy==1.2.0
pyfields==1.1.5
Pygments==2.17.2
pylandsat==0.4
pyparsing==3.1.4
pyproj==2.6.1.post1
pytest==7.4.4
python-dateutil==2.8.2
pytz==2025.2
rasterio==1.1.8
requests==2.23.0
rio-mucho==1.0.0
rio-toa==0.3.0
sentinelsat==0.14
Shapely==1.7.1
six==1.17.0
snowballstemmer==2.2.0
snuggs==1.4.7
Sphinx==5.3.0
sphinx-rtd-theme==2.0.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli==2.0.1
toolz==0.12.1
tqdm==4.67.1
typing_extensions==4.7.1
-e git+https://github.com/dlr-eoc/ukis-pysat.git@bd2e83a59621bb2552ece5278d5bce69919e10eb#egg=ukis_pysat
urllib3==1.25.11
utm==0.5.0
valid8==5.1.2
zipp==3.15.0

name: ukis-pysat
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - affine==2.4.0
      - alabaster==0.7.13
      - appdirs==1.4.4
      - attrs==24.2.0
      - babel==2.14.0
      - chardet==3.0.4
      - click==7.1.2
      - click-plugins==1.1.1
      - cligj==0.7.2
      - dask==2.14.0
      - decopatch==1.4.10
      - docutils==0.19
      - exceptiongroup==1.2.2
      - fiona==1.8.22
      - geographiclib==2.0
      - geojson==3.2.0
      - geomet==1.1.0
      - geopy==2.4.1
      - html2text==2020.1.16
      - idna==2.10
      - imagesize==1.4.1
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - jinja2==3.1.6
      - landsatxplore==0.6
      - makefun==1.15.6
      - markupsafe==2.1.5
      - munch==4.0.0
      - numpy==1.18.5
      - packaging==24.0
      - pandas==1.0.5
      - pillow==7.1.2
      - pluggy==1.2.0
      - pyfields==1.1.5
      - pygments==2.17.2
      - pylandsat==0.4
      - pyparsing==3.1.4
      - pyproj==2.6.1.post1
      - pytest==7.4.4
      - python-dateutil==2.8.2
      - pytz==2025.2
      - rasterio==1.1.8
      - requests==2.23.0
      - rio-mucho==1.0.0
      - rio-toa==0.3.0
      - sentinelsat==0.14
      - shapely==1.7.1
      - six==1.17.0
      - snowballstemmer==2.2.0
      - snuggs==1.4.7
      - sphinx==5.3.0
      - sphinx-rtd-theme==2.0.0
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jquery==4.1
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tomli==2.0.1
      - toolz==0.12.1
      - tqdm==4.67.1
      - typing-extensions==4.7.1
      - urllib3==1.25.11
      - utm==0.5.0
      - valid8==5.1.2
      - zipp==3.15.0
prefix: /opt/conda/envs/ukis-pysat

tests/test_raster.py::DataTest::test_write_to_file
tests/test_raster.py::DataTest::test__lookup_bands tests/test_raster.py::DataTest::test_arr tests/test_raster.py::DataTest::test_dn2toa tests/test_raster.py::DataTest::test_get_dask_array tests/test_raster.py::DataTest::test_get_subset tests/test_raster.py::DataTest::test_get_tiles tests/test_raster.py::DataTest::test_get_valid_data_bbox tests/test_raster.py::DataTest::test_init tests/test_raster.py::DataTest::test_mask_image tests/test_raster.py::DataTest::test_warp
CHANGELOG.rst ukis_pysat/raster.py
CHANGELOG.rst ukis_pysat/raster.py