I built a Python script that downloads tiles from some map service. This service cannot be imported to QGIS as a tile layer so I decided to download tiles directly using requests module. The script also performs a georeference task that is done by SetGeoTransform method in gdal.

The service provided multiple output formats so I tested *.png, *.jpeg and *.bmp. As test shows, *.png is fastest to download and close to the best quality. *.bmp has the best quality but download time is too long.

In the end I have a set of georeferenced PNG images that need to be merged into one image file. The problem is based on keeping oroginal color of all images.

When I load images to QGIS, their colors were presented in normal condition:

enter image description here

Merged them them using gdal_merge using this code:

attrs =['', '-o', 'out_file_merged.png', 'out_g_1', 'out_g_2', 'out_g_3', 'out_g_4', 'out_g_5']

But result was like this:

enter image description here

Right the only one way for me to keep colors is to download *.bmp tiles. But is it possible for gdal to operate with *.png images and keep their colors?

  • 1
    Try: gdalbuildvrt output.vrt "folderName\*.png" after that, load it in QGIS and try to export it as single file. Oct 31 '19 at 7:55
  • @JanDoležal strange thing. It took the first image like a constant color values, but other are in different conditions. Here is the result. Oct 31 '19 at 8:32
  • 1
    Looks like it is taking the color profile from the first dataset and applying it to the other. It was just an idea. Other way would be, try to convert all of the files into .tif file and then merge it and after that export it to PNG if you really need PNG file. But i am not really sure if that will work. Try to convert one to tif and see if it keeps the color profile. Also what about merging the files via PIL (stackoverflow.com/questions/30227466/…) and georeference the result? Oct 31 '19 at 8:39
  • 1
    @JanDoležal I need PNG format just becuse of tile download time. BMP loads much longer. Tried to convert images using gdal_translate result is the same as if I still tried to merge PNG. Will see if I can do something with PIL. Oct 31 '19 at 8:57

I had the same problem. I had several tiles downloaded from the OpenStreetMaps servers, I georeference each one of them and then I wanted to unite them in a single mosaic, but just like you, it didn't keep with the original colors.

The problem was that the tiles downloaded from OpenStreetMaps come with an indexed color space, which when you want to merge them generates the one you have.

The solution I found was to treat the downloaded images with the ImageMagick tool to change the color space to an RGB color.

Finally I just ran the gdal_merge command again and this time I got the result I expected, that is, the images merged correctly with their original color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.