0

I am running this script to convert PNG to GTiff and tag it with coordinates and a crs of my choice. It works just fine but after the conversion the geotiff file is 177 MB (the png is 1 mb or smt) and it blocks me from working with the file. I am new to programming and i dont know where this is coming from. Below shown is the code which worked for me:

import rasterio
dataset = rasterio.open("input_file_path", 'r')
bands = [1,2,3]
data = dataset.read(bands)
transform = rasterio.transform.from_origin(9.171524, 53.057311, data.shape[1], data.shape[2])
crs = {'init': 'epsg:4326'}
with rasterio.open("output_file_path", 'w', driver='GTiff',
 width=data.shape[2], height=data.shape[1],
 count=3, dtype=data.dtype, nodata=0,
 transform=transform, crs=crs) as dst:
dst.write(data, indexes=bands)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 31, 2023 at 10:13

1 Answer 1

1

You will need to use a compress option in your dst rasterio.open() statement. The docs at https://rasterio.readthedocs.io/en/stable/topics/writing.html barely mention this, but compress='lzw' is a starting point.

rasterio uses GDAL under the hood, so ultimately you can use all of GDAL's options to tweak the compression, which includes deflate and packbits as well as LZW compression, and different predictor settings. See https://kokoalberti.com/articles/geotiff-compression-optimization-guide/ for a guide to the options (written from a GDAL command line point of view) which also points to an article regarding using jpeg compression if you're using satellite imagery (orthophotos), which might compress them better than the original png.

The best combination of settings will depend on our image characteristics (data type as well as what's actually in the image). I haven't tried it in rasterio specifically, but in my regular use of GDAL with QGIS, a combination of compress='zstd',predictor=2 works well with integer data, predictor=3 with floating point, and lossy algorithms like jpeg make a big difference for orthophotos or similar, however I'm not sure which versions of rasterio support ZSTD compression.

Editing to add: I note from Why does rasterio compression reduces image size with single band but not with multiple bands that others have had trouble with compressing rasterio multiband images, like you are doing. Another option is to write your huge file as you are doing now, and then run it through GDAL itself as a 2nd step to compress it. You can do that via command line using gdal_translate with compression options specified there, or as a separate step using Python. For the latter, follow the breadcrumbs starting at Calling options/arguments of gdal_translate in Python or How to call gdal_translate from Python code?

answered Mar 31, 2023 at 10:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.