[GIS] Converting PNG files to GeoTIFF using Python

convertgeotiff-tiffpngpythonrasterio

I have a big set of PNG files and EPSG:3006 coordinates of the edges of each file. (actually the coordinates of the edges are given in the file name, like this: 'image_666751_7023263_667007_7023519.png', so 666751, 7023263, 667007, 7023519 are the boundaries of the this image in EPSG:3006 )

How can I convert those PNG files to GeoTIFF files using Python so the TIFF files would contain the geo metadata.
I guess it can be done with Rasterio lib, but I'm not sure how exactly.

Best Answer

eventually this worked for me:

dataset = rasterio.open(input_file_path, 'r')
bands = [1, 2, 3]
data = dataset.read(bands)
transform = rasterio.transform.from_bounds(west, south, east, north, data.shape[1], data.shape[2])
crs = {'init': 'epsg:3006'}

with rasterio.open(output_file_path, 'w', driver='GTiff',
                   width=data.shape[1], height=data.shape[2],
                   count=3, dtype=data.dtype, nodata=0,
                   transform=transform, crs=crs) as dst:
    dst.write(data, indexes=bands)
Related Question