4

I have these two GeoTIFF's:

https://terrafrost.com/gdal/1.tif

https://terrafrost.com/gdal/2.tif

I'm trying to merge them by doing gdal_merge -o result.tif 1-dark.tif 2-dark.tif but when I do the result is just a solid gray image:

https://terrafrost.com/gdal/result.tif

Is there something I'm doing wrong? Do I need to do some pre-processing on the input GeoTIFF's? Maybe another tool would be better?

1
  • Originaly you have two single band GeoTiffs with 256 color palette, but only two colors are used (white and green). The result of gdal_merge is a single band gray image. Your result is OK, but the background color is black and the foreground color is white.
    – Zoltan
    Jan 27, 2023 at 12:59

2 Answers 2

4

Gdalbuildvrt it better than gdal_merge.py for most use cases. However, your issue is easy to fix. Gdalinfo shows that source images are paletted, and both are having the same color table

 0: 253,253,253,0
 1: 17,204,142,255
 2: 0,0,0,255
 3: 0,0,0,255
 4: 0,0,0,255
...

From the documentation https://gdal.org/programs/gdal_merge.html:

-pct

Grab a pseudo-color table from the first input image, and use it for the output. Merging pseudo-colored images this way assumes that all input files use the same color table.

So the gdal_merge command to use is

gdal_merge -pct -o result.tif 1.tif 2.tif

How to do the same with gdalbuildvrt:

gdalbuildvrt test.vrt 1.tif 2.tif
gdal_translate test.vrt result.tif

Your original result.tif image is not solid gday. Running gdalinfo result.tif -stats shows that is has values 0 and 1 like the original paletted images have. But because the color table was not copied the image turned into grayscale with totally black and almost black pixels that eye cannot separate.

STATISTICS_MAXIMUM=1
STATISTICS_MEAN=0.012810842015339
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=0.11245765577406
STATISTICS_VALID_PERCENT=100
3

You can use the following gdal_merge command line to merge your images. This will produce a 26KB output TIF instead of 9MB paletted:

gdal_merge -o result.tif -co NBITS=1 -co COMPRESS=CCITTFAX4 -co PHOTOMETRIC=MINISWHITE -co NBITS=1  1.tif 2.tif
1
  • 1
    Great alternative if the color of the lines does not matter.
    – user30184
    Jan 27, 2023 at 13:05

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.