dropdown menu

Back To Home

Friday, November 15, 2019

Solved python tkinter error- couldn't recognize data in image file

This issue happened while tkinter would like to show image in label using PIL module.
cv2 module command was used to solved the issue.
tkinter module was used to write the gui.

import cv2
img=cv2.imread("md_5aba147bcacf2.png")
cv2.imwrite("md_5aba147bcacf2.png",img)

Python code:

from tkinter import *
from PIL import Image, ImageTk
import cv2

root = Tk()

labelFrame =Frame(root,width=10,height=10)
labelFrame.grid(row=0, column=0)

img=cv2.imread("md_5aba147bcacf2.png")
cv2.imwrite("md_5aba147bcacf2.png",img)
photo=PhotoImage(file="md_5aba147bcacf2.png")
label=Label(labelFrame,image=photo)
label.grid(row=1, column=1)


# Determine the origin by clicking
def getorigin(eventorigin):
    global x0,y0
    x0 = eventorigin.x
    y0 = eventorigin.y
    print(x0,y0)
 
#mouseclick event
label.bind("<Button 1>",getorigin)

root.mainloop()               

                                                         


                                                     

No comments:

Post a Comment