I am trying to take a plain JPEG with no spatial references, apply ground control points (GCPs) to it, and output a georeferenced GeoTIFF or JPEG with world files.
I found some code that seemed like it might work for my task, and adapted it to my situation. The code produces an object of type osgeo.gdal.Dataset, and my understanding is that I want to save this object as a georeferenced image, but what I'm trying doesn't output a georeferenced file.
Here's what my code looks like:
from osgeo import gdal import osr inputImage = "Example_image.jpg" outputImage = "image_gdal.jpg" dataset = gdal.Open(inputImage) I = dataset.ReadAsArray(0,0,dataset.RasterXSize,dataset.RasterYSize) outdataset = gdal.GetDriverByName('GTiff') output_SRS = osr.SpatialReference() output_SRS.ImportFromEPSG(2193) outdataset = outdataset.Create(outputImage,dataset.RasterXSize,dataset.RasterYSize,I.shape) for nb_band in range(I.shape): outdataset.GetRasterBand(nb_band+1).WriteArray(I[nb_band,:,:]) # Creating ground control points (not sure if I got the order of variables right): gcp_list =  gcp_list.append(gdal.GCP(176.412984, -310.977264, 1681255.524654, 6120217.357425)) gcp_list.append(gdal.GCP(160.386905, -141.487145, 1681158.424227, 6120406.821253)) gcp_list.append(gdal.GCP(433.204947, -310.547238, 1681556.948690, 6120335.658359)) outdataset.SetProjection(output_SRS.ExportToWkt()) wkt = outdataset.GetProjection() outdataset.SetGCPs(gcp_list,wkt) # Another attempt to export the image: gdal.Warp("output_image.tif", outdataset, dstSRS='EPSG:2193', format='GTiff') outdataset = None
The above code produces a JPEG file called "image_gdal.jpg", however this JPEG does not have any spatial reference associated with it.
In the second-last line of the above code I tried another strategy for generating a georeferenced output file (this time a TIFF file), but the file does not get created at all, although I don't get any errors when running the script.
The "Example_image.jpg" is located here: https://imgur.com/a/9ThHtOz. It'd be great if you could run my code and help me troubleshoot it.
I'm very new to working with geospatial data in Python, and I might be overlooking something fundamental, or might have multiple small errors in the script that prevent it from working properly.