I have myself a large raster (ecw) that comprises of 3 bands. I'm trying to find the most convenient way of clipping away unwanted black areas around the actual imagery itself. See the raster in question below.

The problem is that surrounding black cells are not classified as nodata, but have differing values such as 0/0/0, 1/0/1, 2/0/1...Thus it takes take time and effort to choose all of them in e.g. raster calculator. Also these values might exist in the area of interest.

For example, if I would set value 1/0/1 to be transparent in layer properties --> transparency dialog, I get the result shown below.

...most of the cells are included...But not all and some wanted cells are now missing

Does anyone have any tips about how to proceed with this? Am I forced to manually create a masking polygon?

2 Answers 2


with gdal, there is the nearblack function that could potentially solve your problem.

This utility will scan an image and try to set all pixels that are nearly or exactly black, white or one or more custom colors around the collar to black or white. This is often used to "fix up" lossy compressed air photos so that color pixels can be treated as transparent when mosaicing

unfortunately, this will be an issue for the "near black" values that do exist in your raster. In this case, I would suggest to extract all "near black" values and clean it with morphological mathematics in order to have a mask of the outside values or (if it is only for visualisation) use gdal fill nodata to remove the gaps after applying near black and setting its value as nodata.

As mentioned by @Luke in the comments, this is also available in QGIS processing toolbox (GDAL > Raster Analysis > Near Black), like all other GDAL tools.

  • Near black is also available via the QGIS processing toolbox docs.qgis.org/testing/en/docs/user_manual/processing_algs/gdal/…
    – user2856
    Jul 18, 2018 at 12:43
  • Both you and csk have really sophisticated answers, thanks a lot. I'll accept one of the answers once I've gotten the process to go through. For now I have problems for running any processes due to various reasons which would require producing separate questions. For example transforming my raster to geotiff produces too big of a file, JPEG-compression produces an error, saving as ecw doesnt work and finally GDAL complains about missing proj4 information (CRS info in TAB). Jul 19, 2018 at 11:45
  • try these creation options for your tif: -co TILED=YES -co BIGTIFF=YES -co COMPRESS=LZW ( -ot Byte if no values are larger than 255)
    – radouxju
    Jul 19, 2018 at 13:02

Use the Raster Calculator to create a new raster of the near black values. It should be fine to use only one band of your multiband raster. Raster calculator function:

(your_raster@1 <= 2) * your_raster@1

Use the Polygonize (raster to vector) tool to convert the new raster into polygons. This should give you one large polygon that covers the black border around your original raster, and a bunch of small polygons scattered throughout the interior of the raster.

Select the large exterior polygon, and use it as a clipping mask with the Clip raster by mask layer tool. Be sure to check the option "Selected features only" under Mask Layer.

Note: This method will remove any near-black values along the edges. If you want to keep these values, you should manually clean up the edges of the mask polygon before running the clip tool. The Vertex Editor tool is probably the most efficient way of doing this.

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.