Trying to add some Transparency / Opacity to my GeoTiff raster files for a better rendering option when overlapping.
Without transparency
With transparency 20%
Is there any gdal command to achieve this?
Driver: GTiff/GeoTIFF
Files: Gb_127_d_D_0006_georefYCBCR_CJPEG.TIF
Gb_127_d_D_0006_georefYCBCR_CJPEG.tfw
Size is 14013, 13566
Coordinate System is:
PROJCS["CH1903 / LV03",
GEOGCS["CH1903",
DATUM["CH1903",
SPHEROID["Bessel 1841",6377397.155,299.1528128000008,
AUTHORITY["EPSG","7004"]],
TOWGS84[674.4,15.1,405.3,0,0,0,0],
AUTHORITY["EPSG","6149"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4149"]],
PROJECTION["Hotine_Oblique_Mercator_Azimuth_Center"],
PARAMETER["latitude_of_center",46.95240555555556],
PARAMETER["longitude_of_center",7.439583333333333],
PARAMETER["azimuth",90],
PARAMETER["rectified_grid_angle",90],
PARAMETER["scale_factor",1],
PARAMETER["false_easting",600000],
PARAMETER["false_northing",200000],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","21781"]]
Origin = (541546.758578460663557,152626.253885675920174)
Pixel Size = (0.031810463058218,-0.031810463058218)
Metadata:
AREA_OR_POINT=Area
Image Structure Metadata:
COMPRESSION=JPEG
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 541546.759, 152626.254) ( 6d40'39.68"E, 46d31'25.15"N)
Lower Left ( 541546.759, 152194.713) ( 6d40'39.88"E, 46d31'11.17"N)
Upper Right ( 541992.519, 152626.254) ( 6d41' 0.60"E, 46d31'25.29"N)
Lower Right ( 541992.519, 152194.713) ( 6d41' 0.79"E, 46d31'11.31"N)
Center ( 541769.639, 152410.484) ( 6d40'50.24"E, 46d31'18.23"N)
Band 1 Block=256x256 Type=Byte, ColorInterp=Red
Overviews: 7007x6783, 3504x3392, 1752x1696, 876x848
Mask Flags: PER_DATASET ALPHA
Overviews of mask band: 7007x6783, 3504x3392, 1752x1696, 876x848
Band 2 Block=256x256 Type=Byte, ColorInterp=Green
Overviews: 7007x6783, 3504x3392, 1752x1696, 876x848
Mask Flags: PER_DATASET ALPHA
Overviews of mask band: 7007x6783, 3504x3392, 1752x1696, 876x848
Band 3 Block=256x256 Type=Byte, ColorInterp=Blue
Overviews: 7007x6783, 3504x3392, 1752x1696, 876x848
Mask Flags: PER_DATASET ALPHA
Overviews of mask band: 7007x6783, 3504x3392, 1752x1696, 876x848
Band 4 Block=256x256 Type=Byte, ColorInterp=Alpha
Overviews: 7007x6783, 3504x3392, 1752x1696, 876x848
EDIT : Proposed workflow to enable transparency on raster data containing no_data values
In my case I had RGBA rasters with no-data values:
Explode bands into one rgb file and one transparency file:
os.system("gdal_translate -b 1 -b 2 -b 3 " + infile + " " + rgbfile)
os.system("gdal_translate -b 4 " + infile + " " + transfile)
Compute desired transparency
os.system("gdal_calc -A " + transfile + " --outfile="+ transfilemoded + " --calc=(A/5*4)")
# A was 255 in my case for all data values, 0 for no_data values
# Hint: since we're in Byte, 255*5/4 = 50 and 255/5*4 = 204
Merge the produced rgb and new transparency file
os.system("gdal_merge " + rgbfile + " " + transfilemoded + " -o " + rgbafile + " -separate")
Best Answer
Your
gdalinfo
dump looks like the raster already has 4 bands and is interpreted as RGBA. This answer will assume you are starting from a 3-band RGB 8-bit/channel image.First create a single-band 8-bit image which has the desired alpha value (alpha 255 is fully opaque, 0 is fully transparent). You said you want about 20% transparency, so 80% opacity => ~200.
Edit the nodata value so it isn't 255 (default from
gdal_calc.py
).Merge the RGB and alpha images, separating the bands.