Capturing Image coordinates in wx.StaticBitmap

Dear All,

I am very beginner in python and wxpython and working on one project where i need to get the actual image coordinate when i click anywhere over the image(jpg). I have read lot of articles with respect to how to get it work but somehow nothing seems to work.

I tried following code also that i found somewhere on mailing list, but it also doesnt work -
ctrl_pos = event.GetPosition()
print("ctrl_pos: " + str(ctrl_pos.x) + ", " + str(ctrl_pos.y))
pos = self.bitmapImageFile.ScreenToClient(ctrl_pos)
print "pos relative to screen top left = ", pos
screen_pos = self.GetScreenPosition()
print "screen position - x, y: " ,screen_pos
relative_pos_x = pos[0] + screen_pos[0]
relative_pos_y = pos[1] + screen_pos[1]
print "pos relative to image top left = ", relative_pos_x, relative_pos_y

I am staticbitmap inside a scrollwindow to display the images. One problem is that the image doest stay attached to scrollwindow always, means when i maximize or stretch the frame the image gets align in the center and when i select a new image it comes align left to scrollwindow. So it changes the image coordinates.

I am attaching my code. I desperately need help. In my program, there is one frame and one menu “Open->Load Images”. On clicking “Load images” a dialogue appears with directory picker. I choose a image directory and images gets loaded in a list inside a frame. I click on image name and it gets displayed in scrollwindow.

What i want is that, whereever the image is displayed inside the scrollwindow, left aligned or center aligned, i should be able to get actual image coordinates of mouse pointer.

By the way i am using wxformbuilder to prepare all the GUIs.

Thanks in advance, please help.
Gaurav

noname.py (6.27 KB)

Gaurav Sharma wrote:

I am very beginner in python and wxpython and working on one project
where i need to get the actual image coordinate when i click anywhere
over the image(jpg). I have read lot of articles with respect to how
to get it work but somehow nothing seems to work.

You have made your task more difficult by scaling the image. Because
you bound the EVT_LEFT_DOWN event to the static bitmap control,
event.GetPosition is the relative position within the SCALED image, but
to compute the original position, you need to save the factors you used
to scale the image and use those to undo the scaling. All the stuff
about screen coordinates is just silly. So, in listBoxFileNameOnListBox:

            if self.img_w > self.scroll_win_w:
                NewW = NewH = 0
                if self.scroll_win_w > self.scroll_win_h:
                    NewW = self.scroll_win_w
                    NewH = self.scroll_win_w * self.img_h / self.img_w
                else:
                    NewH = self.scroll_win_w
                    NewW = self.scroll_win_w * self.img_w / self.img_h
                img = img.Scale(NewW,NewH)
            self.scaledsize = img.GetSize() # <<< add this

and then bitmapImageFileOnLeftDown becomes:

    def bitmapImageFileOnLeftDown( self, event ):
        ctrl_pos = event.GetPosition()
        print("ctrl_pos: %d, %d" % (ctrl_pos.x, ctrl_pos.y))
        scalex = ctrl_pos.x * self.img_w / self.scaledsize.x
        scaley = ctrl_pos.y * self.img_h / self.scaledsize.y
        print("Image pos: %d, %d" % (scalex, scaley))

Stylistically, note that instead of doing GetWidth() and GetHeight(),
you can simply do GetSize() and refer to the .x and .y members. Also
note the use of % instead of converting to strings and concatenating.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.