I want to generate tiles for an PNG file. In the MapTiler(GUI wrapper of gdal2tile), I can specify the bounding box and the zoom level, then it does the good job, which generates the tiles only for that bounding box.

I want to do it with gdal2tiles with own control. How can I directly use gdal2tiles.py to specify the bounding box? Which other gdal utility programs should be called before gdal2tiles?

enter image description here

1 Answer 1


I would use gdal_translate http://www.gdal.org/gdal_translate.html with Virtual raster (.VRT) output http://www.gdal.org/gdal_vrttut.html as an interim format.

gdal_translate -of VRT -projwin ulx uly lrx lry input.png output.vrt

Place the upper left and lower right coordinates of the bounding box as -projwin values. Notice the order which is different from BBOX which is lower left, upper right.

Now you can use the output.vrt file as input for gdal2tiles. Advantage in using VRT instead of GeoTIFF or any other image format as an interim format is that VRT is just a short XML file and no image data are copied.


You approach by attaching georeferencing into .VRT file with -a_ullr and converting VRT into tiles is right. With a little bit different command the result looks good. Actually the commands can be as simple as these:

gdal_translate -of VRT -a_srs EPSG:4326 -a_ullr 112 24 115 21 img.png output.vrt   
gdal2tiles.py output.vrt

The directory "output" contains Leaflet, OpenLayers, and Googlemaps maps. The leaflet.html one looks like this:

enter image description here

You may not really want to create zoom levels -z 0-4 because level zero covers the whole world and the footprint of your image is rather small. Automatically created map contains zoom levels 6-10 which suit with the size of your image better. That option -p raster does not create Leaflet and Googlemaps page may be a bug in gdal2tiles.py.

  • I have tried this command. But I only know the boundingbox in lon/lat, fill in those numbers into the command returns the "Computed -srcwin ... has negative width and/or height." error. I suppose I should project the PNG file first?
    – stevenhz
    Commented May 29, 2016 at 19:15
  • You can have another look on the documentation from -projwin_srs srs_def: (GDAL >= 2.0) Specifies the SRS in which to interpret the coordinates given with -projwin. The srs_def may be any of the usual GDAL/OGR forms, complete WKT, PROJ.4, EPSG:n or a file containing the WKT. Note that this does not cause reprojection of the dataset to the specified SRS. Adding -projwin_srs epsg:4326 should make it to work. However, GDAL must know the native projection of your png file before it can warp the epsg:4326 projwin correctly. Check with gdalinfo if it does.
    – user30184
    Commented May 29, 2016 at 19:54
  • My PNG has no projection information, so projwin_srs does not work. So I used: gdal_translate -of VRT -a_srs EPSG:4326 -a_ullr 112 24 115 21 img.png output.vrt. Then I use python gdal2tiles.py -w all -z 0-4 -p raster output.vrt ./gdal_tiles. The generated tiles are fine when checking with *.kml by google earth. But it did not generate a webviewer for leaflet. Which parts were wrong?
    – stevenhz
    Commented May 29, 2016 at 22:17
  • I used the leaflet.html generated by maptiler to view the tiles generated from previous steps. Set the tms to true. The tile just cover the whole world in zoom level 0, so not within the bounding box..... still only the *.kml shows the right tiles on Google Earth.
    – stevenhz
    Commented May 29, 2016 at 23:02
  • Could you share some test image to play with?
    – user30184
    Commented May 30, 2016 at 8:58

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.