When I merge my geotiffs using buildVRT and translate from gdal the transparity is showed as transparity blocks instead of the content of layer behind it.
used code for merging geotiff:
demList = glob.glob(os.path.dirname(os.path.abspath(__file__)) + "//..//icons//*.tif") print(demList) VRToptions = gdal.BuildVRTOptions(srcNodata="255 255 255") vrt = gdal.BuildVRT(os.path.dirname(os.path.abspath(__file__)) + "//merged.vrt", demList, options=VRToptions) translateOptions = gdal.TranslateOptions(creationOptions=["TILED=YES"]) gdal.Translate(os.path.dirname(os.path.abspath(__file__)) + "//mergedDEM.tif", vrt, options=translateOptions) vrt = None
my question is how i can use the previous layer as background instead of the transparity blocks.
Compleet code with problem: https://github.com/NickSmeding/MergeGeotiff
What about this (using rasterio and it’s extension rioxarray) and using both your tifs :
import rioxarray import rioxarray.merge icon = "./tiffs/horse2.tif" background = "./tiffs/4c5ae720f274505972ecedf4066bb32dfb28b63e5785030a7eb55b4e9978602c.tif" # open the rasters with rioxarray.open_rasterio('./tiffs/horse2.tif') as rds1: with rioxarray.open_rasterio(background) as rds2: # ensure the rasters have the same projection/transform/shape rds3 = rds1.rio.reproject_match(rds2) ar = rds3.values #look for pixels in icon having alpha = 255 ix = (ar == 255) #update background pixels with icon for k in range(4): rds2.values[k][ix]=ar[k][ix] #save updated raster rds2.rio.to_raster("test.tif")
The icon is a bit pixelised, but it’s only because the resolution of you background is less than the one in your icon/tif. Maybe you could reproject both the arrays to the maximum resolution of both layers, but IMHO that would be another subject.
(Note that you icon TIF is ok ; I tried to open it in QGIS and you can superpose the layers alright. I also tried to edit it to re-declare a nodata value, but I still got the same result as you. Maybe if your background and icon tif shared the same nodata value there could be a way to compute it without accessing the pixels as I did,…)