How to remove black edges on georeferenced images with GDAL

Use GDAL's nearblack utility. It converts black, white or any specified color to black/white.


A nodata value is just that, no data. So the idea of having it white, black, or any other colour within the dataset doesn't make sense. Now, obviously when you load an image into a GIS, you have to represent nodata as something, and this is, in Quantum GIS at least, either whatever is underneath, or if there is nothing then the project's background colour. So there is a decoupling of what nodata means, how it is stored, and how it is represented.

If you're getting black regardless of the background colour of the GIS you're using, then the chances are GDAL hasn't set a nodata value at all, and it just defaults to black pixels. So you need to explicitly tell GDAL how you want nodata to be stored. With gdalwarp you use the -dstnodata command line switch to achieve this. You may find that nodata values in the source image aren't carried over in a warp operation, in which case you need to set it explicitly with -srcnodata. If you want to override an image's nodata vaue, you can do that with gdal_translate -a_nodata ....


Between gdal_translate and gdal2tiles.py, use gdalwarp with -dstnodata parameter. For example, if you want to turn these black edges to white (255 255 255):

gdal_translate <ORIGINAL_FILE.jpg> cache/transform/georef.tif -of GTiff -gcp 3798 138 26.355081965420457 59.34191837150522 -gcp 3448 4920 26.35345118233987 59.323597258855266 -gcp 8108 3875 26.387508872834236 59.327911749143276

gdalwarp -dstnodata "255 255 255" cache/transform/georef.tif cache/transform/warp.tif

gdal2tiles.py --profile mercator --s_srs 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]' --zoom 12-16 --title 'New tiles' --tile-format jpeg --webviewer all cache/transform/warp.tif <TILES_OUTPUT_DIRECTORY>

Tags:

Gdal