Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
iconphoto() method in Tkinter | Python
Next article icon
There are numerous tools for designing GUI (Graphical User Interface) in Python such astkinter,wxPython,JPython, etc where Tkinter is the standard Python GUI library, it provides a simple and efficient way to create GUI applications in Python.

Reading Images With Tkinter

In order to do various operations and manipulations on images, we require PythonPillow package. If thePillow package is not present in the system then it can be installed using the below command.
  • In Command prompt:
    pip install Pillow
  • In Anaconda prompt:
    conda install -c anaconda pillow
Example 1: The below program demonstrates how to read images withtkinter usingPIL.Python
# importing required packagesimporttkinterfromPILimportImageTk,Imageimportos# creating main windowroot=tkinter.Tk()# loading the imageimg=ImageTk.PhotoImage(Image.open("gfg.jpeg"))# reading the imagepanel=tkinter.Label(root,image=img)# setting the applicationpanel.pack(side="bottom",fill="both",expand="yes")# running the applicationroot.mainloop()
Output:In the above program, an image is loaded using thePhotoImage() method and then it is read by using theLabel() method.Thepack() method arranges the main window and themainloop() function is used to run the application in an infinite loop.Example 2: Let us look at another example where we arrange the image parameters along with application parameters.Python
# importing required packagesimporttkinterfromPILimportImageTk,Image# creating main windowroot=tkinter.Tk()# arranging application parameterscanvas=tkinter.Canvas(root,width=500,height=250)canvas.pack()# loading the imageimg=ImageTk.PhotoImage(Image.open("gfg.ppm"))# arranging image parameters# in the applicationcanvas.create_image(135,20,anchor=NW,image=img)# running the applicationroot.mainloop()
Output:In the above program, the application parameters are handled by using theCanvas() method and the image parameters are handled usingcreate_image() method such that the imagegfg.ppm is displayed in the main window having defined height and width.Note: The Canvas method create_image(x0,y0, options ...) is used to draw an image on a canvas. create_image doesn't accept an image directly. It uses an object which is created by the PhotoImage() method. The PhotoImage class can only read GIF and PGM/PPM images from files.

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp