Important: Please read the Qt Code of Conduct -

How to gather coordinates from a .tif image and insert them to PyQt code

  • Hello, I use this code here: As I see this part here (line 109):

    class MyToolBar(mpl_qt.NavigationToolbar2QT):
        def set_message(self, s):
                parent = self.parent()
            except TypeError:  # self.parent is not callable
                parent = self.parent
                sstr = s.split()
    #            print(sstr, len(sstr))
                while len(sstr) > 5:  # when 'zoom rect' is present
                    del sstr[0]
    #            print(sstr)
                x, y = float(sstr[0][2:]), float(sstr[1][2:])
                if parent.canTransform():
                    xC, yC = parent.beamPosRectified
                    if not parent.buttonStraightRect.isChecked():
                        xP, yP = parent.transformPoint((x, y))
                        x0, y0 = (xP-xC)/parent.zoom, (yP-yC)/parent.zoom
                        s = u'image: x={0:.1f} px, y={1:.1f} px\nplate: '\
                            'x={2:.2f} mm, y={3:.2f} mm'.format(x, y, x0, y0)
                        x0, y0 = (x-xC)/parent.zoom, (y-yC)/parent.zoom
                        s = 'plate: x={0:.2f} mm, y={1:.2f} mm'.format(x0, y0)
                    s = u'image: x={0:.1f}, y={1:.1f}'.format(x, y)
            except Exception as e:
    #            print(e)
            if self.coordinates:

    is involved with the x,y coordinates. I need to do the following: to get the coordinates from the .tif image I load and put them inside the code, so that instead x,y coordinates to show the real coordinates of the .tif. I am thinking of using the gdal package but I do not know which lines I should change on the above code and how to achieve this transformation, can you help me? Thank you...

  • I am trying to reproduce this interesting code from here: by giving random test values on x, y:

    # open the dataset and get the geo transform matrix
    ds = gdal.Open('final.tif') 
    xoffset, px_w, rot1, yoffset, px_h, rot2 = ds.GetGeoTransform()
    x = 100000
    y = 100000
    # supposing x and y are your pixel coordinate this 
    # is how to get the coordinate in space.
    posX = px_w * x + rot1 * y + xoffset
    posY = rot2 * x + px_h * y + yoffset
    # shift to the center of the pixel
    posX += px_w / 2.0
    posY += px_h / 2.0

    but I get this error:

    free(): invalid pointer
    Aborted (core dumped)

    Any idea, how to proceed?

  • Lifetime Qt Champion

    Do you have a TIF called final.tif and in a location where it actually finds it?

    else i guess
    ds = gdal.Open('final.tif')
    will result in a invalid ds and
    rot2 = ds.GetGeoTransform()
    might crash it.

    Purely guessing. sadly i dont know gdal.

  • @mrjj: instead of

    ds = gdal.Open('final.tif')

    I have the full path of the .tif file

    ds = gdal.Open('/home/john/Desktop/UAV_image.tif')

    The path is correct, in other code it works just fine

  • Lifetime Qt Champion

    Ok, so its not that.

    so it doesn't say at which line, the error comes?

  • @mrjj It only prints this, nothing else:

    free(): invalid pointer
    Aborted (core dumped)

    I won't stuck on this searching if there is a better solution to my question on the initial post...

  • Can I run gdalinfo inside the python code (not on terminal) and get the data I need of the coordinates of the .tif's corners in order to use them on x,y (I don't know how!!) ?

  • Lifetime Qt Champion

    Please ask in a forum specialized to gdal and image processing. I don't see how this is related to Qt in any way.

Log in to reply