After georeferencing a black and white TIFF image, the QGIS Raster Georeferencer fills the corners with black pixels, creating black elongated triangles along the edges of the new image. I could not find anything in the Georeferencer settings to force these filler pixels to be while. I want to set all white pixels transparent when importing the image into QGIS, so eliminating the black corners is preferred. I tried editing the georeferenced TIFF image but it loses the reference data on saving. I tried the GeoTIFF tools (listgeo.exe and geotifcp.exe) but writing the data back to the TIFF image using geotifcp.exe gives a warning "LZW compression is not available to due to Unisys patent enforcement." It would be ideal if QGIS can be set to force the padding colour to white otherwise I am happy to try any other tools, methods or instructions how to achieve what I want.
4 Answers
If image gets rotated in the georeferencing process it is inevident that triangle shaped nodata areas appear at the corners of the georeferenced image. There are a few different ways to deal with the nodata area. I clipped a part of your image to be used as a test case.
- Do nothing with the nodata. Georeferencer just fills the slivers with black pixels. That is the default in QGIS georeferencer.
- Mark a certain pixel value to meand nodata so the viewer can handle those pixels as transparent. QGIS gives an option to mark black pixels as nodata but unfortunately it makes all black pixels transparent. If you had a good quality pure black and white scan you would see nothing in the map but now only the black border around Warner Estate disappears.
- Add alpha band that defines the nodata area. This method saves the real black pixels in the map but hides the slivers if the image viewer can deal with alpha band.
QGIS georeferencer does not have a direct option for writing alpha band but you can generate the GDAL commands from the user interface "File - Generate GDAL Script". You can edit the script by hand so that it includes the option for alpha band. Then run the commands from the command line. In my tests I used commands
gdal_translate -of GTiff -gcp 7.64286 140.143 163.767 -5.74727 -gcp 380.538 323.722 270.053 -430.893 -gcp 67.4699 362.241 -3.98671 -370.066 "/test/georef.png" "/Temp/georef.tif"
gdalwarp -r near -order 1 -co COMPRESS=NONE -dstalpha "/Temp/georef.tif" "/test/georef_modified.tif"
The program that you use seems to be quite old. The last LZW patent has expired in 2004 https://en.wikipedia.org/wiki/Lempel–Ziv–Welch#Patents. If you want to use LZW compression with the QGIS georeferencer edit the gdal_translate command a bit more:
gdalwarp -r near -order 1 -co COMPRESS=LZW -dstalpha "/Temp/georef.png" "/test/georef_modified.tif"
-
If you are able to use gdal_translate, a fourth option is to use it to choose an arbitrary NoData value or make space for a nodata value. For example:
gdal_translate -a_nodata 177 "/test/georef.png" "/Temp/pregeoref.png"
orgdal_translate -a_nodata 0 -scale 0 255 1 255 "/test/georef.png" "/Temp/pregeoref.png"
orgdal_translate -a_nodata 255 -scale 0 255 0 254 "/test/georef.png" "/Temp/pregeoref.png"
and then georeference the pre-processed file. Georeferencing a file with a nodata value will use that to mark the newly missing data in the georeferenced file.– Dave XCommented Oct 30, 2020 at 15:55 -
I do not believe it does. For gdal_translate the order is input-output and the command is using
"/Temp/georef.png" "/test/georef_modified.tif"
. Commented Oct 30, 2020 at 16:13 -
Right. I will edit, thank you. The commands still worked because the .png named tiff was used in the next command. Commented Oct 30, 2020 at 16:22
-
Yes. I was a little confused when I tried to add GCPs to a png file--I could scale and set a nodata value and still get a .png formatted result, but if I tried the GCPs, the result was a TIFF– Dave XCommented Oct 30, 2020 at 16:43
-
Out of the numerous answers to this question over the years I think this one explains it the best. Note to anyone else reading, for some reason even in QGIS 3.32 the GDAL Scripting option doesn't work with Linear and Helmert transformations. So far I've found if you just change it to Polynomial 1 and generate the script it still works exactly the same. Commented Nov 27, 2023 at 0:55
It's a super hack, but it works.
- Do the georeferencing as normal.
- Open the created _modified.tif file in Paint.NET (https://www.getpaint.net/download.html) FREE!
- Bucket fill the black corner triangles with white. Save file as new TIFF (just in case).
- Drag-drop new TIFF into QGIS. It'll maintain the georeferencing and now have white corners.
If you can convert the image to PNG with an alpha channel, the georeferencer will be able to make the nodata areas transparent while keeping the black parts black.
I had a stack of B+W map scans, what finally worked is using ImageMagick to add an alpha channel to the image before georeferencing.
v7: magick input.png png32:output.png
older: convert input.png png32:output.png
No other fiddly options are required in the georeferencer or the QGIS layer.
This works because it forces creation of an empty alpha channel. Many applications (e.g. Photoshop) will remove empty alpha channels when saving to PNG even if you try to include one.