EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter image
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Tkinter image

Tkinter image

Introduction to Tkinter image

A class in Tkinter called PhotoImage class is used to display grayscale images or real color images in Tkinter. And the format of these images can be of various types including png, jpeg, gif, tif, ppm, bmp, etc. and image.open(file to be opened) method is used to open the file which searches for the specified file in the program directory and PhotoImage class, image.open(file to be opened) method to work, Python Imaging Library or PIL must be installed which allows loading of images and the corresponding module is present in PyPi which can be installed using the pip package manager.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,198 ratings)

The syntax for the tkinker image function in Python is as follows:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

import tkinter as tk
from PIL import ImageTk, Image
ImageTk.PhotoImage(Image.open(path_of_the_image))

where path_of_the_image is the path where the image to be displayed is present.

Working of the image in Tkinter

  • A class in Tkinter called PhotoImage class is used to display grayscale images or real color images in Tkinter.
  • The format of these images can be of various types including png, jpeg, gif, tif, ppm, bmp, etc.
  • The image.open(file to be opened) method is used to open the file which searches for the specified file in the program directory.
  • For the PhotoImage class, image.open(file to be opened) method to work, Python Imaging Library, or PIL must be installed which allows loading of images.
  • The corresponding PIL module is present in PyPi which can be installed using the pip package manager.

Constructor

__init__(self, name=None, cnf={}, master=None, **kw)

The above constructor creates an image with a name and the resources that values are data, file, format, gamma, width, height, and palette.

Methods

  • __init__(self, name=None, cnf={}, master=None, **kw)

This method is used to create an image with a name.

  • blank(self)

A transparent image can be displayed using this method.

  • cget(self, option)

The value of the option is returned using this method.

  • copy(self)

A new photo image is returned which is the same image as the widget using this method.

  • zoom(self, x, y=’ ’)

A new photo image is returned which is the same image as the widget but the image is zoomed with x and y, using this method.

  • subsample(self, x, y=’ ’)

A new photo image is returned which is based on the same image as the widget but the image uses only xth or yth pixel, using this method.

  • get(self, x, y)

The colors red, green, and blue at the pixel x and y is returned using this method.

  • put(self, data, to=None)

The row formatted colors are put to the image which begins from the position specified from to, using this method.

  • write(self, filename, format=None, from_coords=None)

The image to file specified as the filename in the specified format is written beginning from the position specified as from_coords.

Examples of Tkinter image

Here are the following examples mention below

Example #1

Tkinter program to display an image whose path is specified by making use of PhotoImage class and image.open() function.

Code:

#importing tkinter package
from tkinter import *
#importing ImageTK, Image from PIL package
from PIL import ImageTk, Image
#specifying the path of the image to be displayed
pathoftheimage = 'C:\\Users\\admin\\Desktop\\quote1.png'
root = Tk()
#specifying the title of the tkinter window within which the specified image is displayed
root.title("QUOTES")
#using image.open function and PhotoImage function to open the spcified image and display on the screen
img = ImageTk.PhotoImage(Image.open(pathoftheimage))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

Output:

Tkinter image output 1

In the above program, the Tkinter package is imported and then the ImageTK, Image packages are imported from the installed PIL package. Then the path of the image to be displayed is specified. Then the title of the Tkinter window within which the image is displayed is specified. Then image.open function and photoimage function is used to open the specified image and display it on the screen.

Example #2

Tkinter program to display an image whose path is specified by making use of PhotoImage class and image.open() function.

Code:

#importing tkinter package
from tkinter import *
#importing ImageTK, Image from PIL package
from PIL import ImageTk, Image
#specifying the path of the image to be displayed
pathoftheimage = 'C:\\Users\\admin\\Desktop\\Mario.jpg'
root = Tk()
#specifying the title of the tkinter window within which the specified image is displayed
root.title("MARIO")
#using image.open function and PhotoImage function to open the spcified image and display on the screen
img = ImageTk.PhotoImage(Image.open(pathoftheimage))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

Output:

Tkinter image output 2

In the above program, the Tkinter package is imported and then the ImageTK, Image packages are imported from the installed PIL package. Then the path of the image to be displayed is specified. Then the title of the Tkinter window within which the image is displayed is specified. Then image.open function and photoimage function is used to open the specified image and display it on the screen.

Example #3

Tkinter program to display an image whose path is specified by making use of PhotoImage class and image.open() function.

Code:

#importing tkinter package
from tkinter import *
#importing ImageTK, Image from PIL package
from PIL import ImageTk, Image
#specifying the path of the image to be displayed
pathoftheimage = 'C:\\Users\\admin\\Desktop\\duck.png'
root = Tk()
#specifying the title of the tkinter window within which the specified image is displayed
root.title("DONALD_DUCK")
#using image.open function and PhotoImage function to open the spcified image and display on the screen
img = ImageTk.PhotoImage(Image.open(pathoftheimage))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

Output:

output 3

In the above program, the Tkinter package is imported and then the ImageTK, Image packages are imported from the installed PIL package. Then the path of the image to be displayed is specified. Then the title of the Tkinter window within which the image is displayed is specified. Then image.open function and photo image function is used to open the specified image and display it on the screen.

Recommended Articles

This is a guide to the Tkinter image. Here we discuss the concept of Tkinter image through definition, syntax, working of Tkinter image through programming examples, their outputs, then the constructor and methods of Tkinter image. You may also have a look at the following articles to learn more –

  1. Tkinter TreeView
  2. Tkinter Notebook
  3. Tkinter Grid
  4. Tkinter Menu
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more