0

here i am using baumer machine vision camera and tkinter framework for making gui application. i captured image from camera and convert using PIL Library but lable throws error for numpy array. this application work for webcam so i cant understand diffrence of camera image data.

import neoapi
from neoapi.neoapi import NeoException
import numpy as np
import cv2 
from tkinter import *
import sys
from PIL import Image, ImageTk


try:
    
    camera = neoapi.Cam()
    camera.Connect()
    dim = (640,480)
    camera.f.ExposureTime = 4000
    camera.f.Width = 640
    camera.f.Height = 480



except(neoapi.NeoException, Exception)as exc:
    print(exc)

root = Tk()


def main():
    while True:
         img =camera.GetImage().GetNPArray()
         img2 = Tk.PhotoImage(Image.fromarray(img))
         L1['Image'] = img2
         
        
         



L1 = Label(root,borderwidth=1,text= ' var')
L1.pack()    




B1 = Button(root,text='connnect',command= main).pack()
root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • 1
    First you should use `ImageTk.PhotoImage` instead of `tk.PhotoImage`. Second, don't use while loop in tkinter main thread as it will block the tkinter mainloop from updating and handling pending events. – acw1668 Jun 24 '21 at 04:31
  • I tried both ImageTk and tk. what can i use instead of while loop? – Urvish solanki Jun 24 '21 at 04:35
  • You can use either `threading` or tkinter `.after()`. – acw1668 Jun 24 '21 at 04:38

1 Answers1

0

First you need to use ImageTk.PhotoImage instead of tkinter PhotoImage.

Second better use threading or tkinter .after() instead of while loop in the tkinter application as while loop will block tkinter mainloop() from updating and handling pending events.

Below is an example of using .after() instead of while loop:

def main():
    img = camera.GetImage()
    if not img.IsEmpty():
        img = img.GetNPArray()
        # use ImageTk.PhotoImage instead
        img2 = ImageTk.PhotoImage(Image.fromarray(img))
        # use L1['image'] instead of L1['Image']
        L1['image'] = img2
        # save a reference of the image
        L1.image = img2
    # call main() after 25ms later (use other value to suit your case)
    L1.after(25, main)

Note that you should avoid calling main() multiple times (i.e. clicking connect multiple times) as it will create multiple periodic tasks.

acw1668
  • 40,144
  • 5
  • 22
  • 34