I'm subtracting two rasters and saving the result to my disk using Rasterio. I observed a significant increase (20x) in the size of the output file after the raster operation.

# Reading the files
A_raster = rxr.open_rasterio(A_of_A_minus_B, masked=True).squeeze()
B_raster = rxr.open_rasterio(B_of_A_minus_B, masked=True).squeeze()

# Matching rasters
B_raster = B_raster.rio.reproject_match(A_raster)
B_raster = B_raster.assign_coords({"x": A_raster.x, "y": A_raster.y})

# Subtraction and saving results
_difference = A_raster - B_raster
_difference.rio.to_raster(file_path_to_save, driver='GTiff')

Using QGIS, I'm not able to observe any difference between the input rasters and the output raster meta data. All rasters have identical data type (32 bit floating point), resolution (15078x7279), cell size (10-, -10 feet). The only difference is the size. The input files are 22 MB and the output file is 420 MB.

The things I have tried:

Studied Rasterio.to_raster() function to look for some clues on how to fix this. From other questions on StackExchange, I believe this issue may be related to compression. I'm unable to find a way to modify compression.

Desired output:

To reduce the size of the output to be similar to the input raster. Size is important as I'm working with ~2000 raster files.

  • 1
    I guess that your source images are compressed but you create an uncompressed output.
    – user30184
    May 5, 2022 at 18:00
  • Thanks! Do you know how I can compress while I'm exporting the raster using to_raster()?
    – PPR
    May 5, 2022 at 18:12
  • Thanks @user30184. I fixed the problem. _difference.rio.to_raster(file_path_to_save, driver='GTiff', compress='lzw')
    – PPR
    May 5, 2022 at 18:20
  • 1
    @PPR instead of answering in a comment, please add your solution as an answer and mark it as accepted.
    – user2856
    Jun 3, 2022 at 23:58

1 Answer 1


Following the input from @user30184, I fixed the problem by setting the compress variable.

_difference.rio.to_raster(file_path_to_save, driver='GTiff', compress='lzw')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.