2

So I have done some RGB-channel extraction from a .tif file, meaning I took UAV_image.tif and I have extracted R,G,B channels on three different .tif files. Next, I did some calculations such as result = ((red^2)+(blue^2))/(blue). For all the previous operations I used mainly cv2.imwrite and tiff.imread commands. The problem is that the resulted .tif does not contains coordinates.. How can I copy the coordinates from the initial UAV_image.tif to result.tif? I tried to followed this tutorial:

Use gdalsrsinfo to get the srs of the tiff that still has the projection:

gdalsrsinfo -o wkt tiffwithsrs.tiff Then copy the output and use gdal_translate to apply it to a new tiff:

gdal_translate -a_srs '...' tiffwithoutsrs.tif newfixedtif.tif just paste your projection after the -a_srs option

(source: Using GDAL command line to copy projections)

But the result was to copy both coordinates and colors...!!! Did I do something wrong?

Also, this guy here: "Copy" Image coordinates to another image that is nd.array has a an approach that does not fit my problem, because I do not do any kind of Machine Learning training (as I see to his example - correct me If I am wrong) and I do not use nd.array.

So, the question is how do I copy coordinates from a .tif image that has coordinates to a .tif image that does not have coordinates?

1 Answer 1

2

You could do something like this in python using rasterio.

UAV_image = rasterio.open('UAV_image.tif')
new_tif = rasterio.open('new.tif','w',
                        driver='Gtiff',
                        height = UAV_image.height,
                        width = UAV_image.width, 
                        count = 1,
                        crs = UAV_image.crs,
                        transform = UAV_image.transform, 
                        dtype = UAV_image.dtypes)

new_tif.write(result, 1) #result from calculations
UAV_image.close()
new_tif.close()

This should take the geo-information from the original tif and write it to the new one.

8
  • Why do I get this error? File "copy_coordinates.py", line 11, in <module> dtype = UAV_image.dtype) AttributeError: 'DatasetReader' object has no attribute 'dtype' Nov 2, 2021 at 13:23
  • 1
    @just_learning Try with .dtypes , sorry spelling error. If that doesn't work you can write the datatype your new image has as a string 'uint8', 'float32' etc.
    – DNy
    Nov 2, 2021 at 13:28
  • 1
    I assumed you had some array named result as calculated in the original question. That result will then be written to this new_tif file.
    – DNy
    Nov 2, 2021 at 14:06
  • 1
    Maybe you are looking for img.countrasterio.readthedocs.io/en/latest/…
    – DNy
    Nov 2, 2021 at 14:44
  • 1
    I'm not really sure sorry. Maybe try adding an extra empty band to the 3 band image? The solutions you linked look useful also.
    – DNy
    Nov 9, 2021 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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