Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

python - How can I use ImageTk.PhotoImage to load image from ip webcam?

I share my code where I'm trying to use the image 'img' in PIL.ImageTk.PhotoImage(img) but it does not work.

        while(True):
            imgResp=urllib.request.urlopen(self.ip)
            imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
            img=cv2.imdecode(imgNp,-1)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            bodies = body_classifier.detectMultiScale(img, 1.04, 3)
            for (x,y,w,h) in bodies:
                print("X", x)
                print("Y", y)
                print("W", w)
                print("H", h)
                cv2.imshow("CAMARA 1",img)
                self.canv = Canvas(self.window, bg='white')
                self.canv.grid(row=5, column=0)
                #self.imgLoad = PIL.ImageTk.PhotoImage(PIL.Image.open ("C:\......jpg")) # works
                self.imgLoad = PIL.ImageTk.PhotoImage(img)                               # does not work
                self.canv.create_image(20,20, anchor=NW, image=self.imgLoad)
question from:https://stackoverflow.com/questions/65602917/how-can-i-use-imagetk-photoimage-to-load-image-from-ip-webcam

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use numpy asarray to read the image.

a = np.asarray(img)
img2 = Image.fromarray(a)
ImageTk.PhotoImage(img2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...