0

I have a tkinter application that reads images from a Basler camera and show the original image and the processed one on a frame of the GUI.

The function that does this is :

def begin_funct():

    global image
    global processed
    global crease
    global Horizontal
    #open the camera and set the parameters
    tlf = pylon.TlFactory.GetInstance()
    cam = pylon.InstantCamera(tlf.CreateFirstDevice())
    cam.Open()
    cam.UserSetSelector = 'Default'
    cam.UserSetLoad.Execute()
    cam.PixelFormat.SetValue('Mono8')
    cam.Gain.SetValue(17.9)
    cam.ExposureTime.SetValue(80.0)
    cam.TriggerMode.SetValue('On')
    cam.TriggerSource.SetValue('Line1')
    cam.TriggerActivation.SetValue('RisingEdge')
    cam.LineSelector = "Line1"
    cam.LineMode = "Input"
    #start grabbing the image, process and then show it on the GUI
    while True:
        res = cam.GrabOne(10000)
        img = res.GetArray()
        #process_image_from_camera is a module that I wrote to process the image
        original,processed,crease,Horizontal=process_image_from_camera(img)
         #get the result
        image=cv2.resize(original,(int(width_value/2),int(height_value/2)))
        edged = cv2.resize(processed, (int(width_value/2),int(height_value/2)))
         # OpenCV represents images in BGR order; however PIL represents
          # images in RGB order, so we need to swap the channels
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
         # convert the images to PIL format...
        image = Image.fromarray(image)
        edged = Image.fromarray(edged)
         # ...and then to ImageTk format
        image = ImageTk.PhotoImage(image)
        edged = ImageTk.PhotoImage(edged)
        #labels to put the image on
        label = Label(frame2, image=image, bg='green')
        label.image = image
        label.grid(row=0,column=0)
        label2 = Label(frame2, image=edged, bg='green')
        label2.image = edged
        label2.grid(row=0,column=1)

My problem is that the images are only displayed on the GUI if there's a timeout or an error in the processing, which means only the last image grabbed is displayed, only when there's an error of execution and the processing stops that we see the result.

I want every image grabbed to be displayed and then the next one and then the next one..

Can anyone see the problem here?

acw1668
  • 40,144
  • 5
  • 22
  • 34
fatma cheour
  • 35
  • 1
  • 6
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – jasonharper Apr 13 '21 at 16:54
  • I'm already anchoring my image with `label.image = image` but it still doesn't work – fatma cheour Apr 13 '21 at 17:30
  • 1
    @fatmacheour Don't use a `while True` loop when using tkinter. Look at how the `.after` method works. – TheLizzard Apr 13 '21 at 17:32
  • `label.image` doesn't preserve a reference to the image if `label` itself does not remain referenced. You need to append the images (or the Labels that reference the images) to an array that will stay around for as long as the window does. – jasonharper Apr 13 '21 at 17:34
  • @jasonharper I think OP wants to have 2 images shown on the screen and they should be updates (so even if the old ones go out of scope it's fine). – TheLizzard Apr 13 '21 at 17:36
  • is `cam.GrabeOne(10000)` a blocking call? Is that 10000 a timeout in ms? – Bryan Oakley Apr 13 '21 at 17:47
  • @BryanOakley yes it is – fatma cheour Apr 14 '21 at 08:05
  • @BryanOakley cam.GrabOne(10000) waits for a trigger in the camera to show an image, and I put in a while true loop so that everytime the camera is triggered the GUI shows the new images after some processing done – fatma cheour Apr 14 '21 at 08:24

0 Answers0