Python tkinter image размер

Tkinter PhotoImage – Displaying Images with Tkinter

Tkinter is a very well rounded GUI library with support for various GUI elements. One of the many features that Tkinter supports is the displaying of images within it’s GUI. This is accomplished using the Tkinter PhotoImage Class, which takes an image’s filepath and creates an object from it that can be used with the other GUI elements.

Using Tkinter PhotoImage

The process is quite simple. All you have to do is pass the file path of the Image you want to import, into the PhotoImage class. This creates an image object which Tkinter understands, allowing it to be used with other GUI elements like the Canvas or Labels.

Here is our PhotoImage example code:

from tkinter import * root = Tk() image = PhotoImage(file="Python.png")

We now have our image object stored in the variable image . In the section we’ll explain how to actually display this image now.

If the image you want to display is not in the same folder/directory, then you’ll have to specify the entire filepath, not just the name of the file.

Displaying the Image

As mentioned earlier, we need a suitable GUI element to actually display the image. Surprisingly, most GUI elements do support it in one way or the other. Let’s quickly go through them.

Tkinter Label (Image)

from tkinter import * root = Tk() image = PhotoImage(file="python.png") label = Label(root, image = image) label.pack() root.mainloop()

Several GUI elements, such as labels and buttons have the image parameter to which you can assign a suitable image object, just as we have down in the above code.

The output is as shown below.

Label Image Display - Tkinter PhotoImage

Keep in mind that Tkinter itself does not have the ability to control (resize) the images it displays. The image displayed above was displayed in it’s original dimensions (size).

Displaying Images on a Canvas

The Tkinter canvas is designed to support features like drawing shapes, images and even displaying images. Hence it has a little more control over the images than the Tkinter Label does.

from tkinter import * root = Tk() image = PhotoImage(file="python.png") canvas = Canvas(width = 300, height = 300, bg='black') canvas.create_image(200, 200, image = image) canvas.pack() root.mainloop()

The first two parameters in the create_image() function represent the width and height of the image respectively. However, as you can see in the image below, that image appears to be cropped. This is because the dimensions we gave the image were too small.

Читайте также:  Питон работа со строками пример

Canvas Image display

Displaying Images on Buttons

Even though is a tutorial on the Tkinter PhotoImage class, I want to show you some other options as well. In the below code we use a combination of PhotoImage + Pillow (python image library) to display the image in the required format.

We open the Image using Pillow’s open() function, followed by the resize function, which takes a tuple containing the new width and height. Then we converted this image to a Tkinter compatible format using ImageTk.PhotoImage() .

from tkinter import * from PIL import Image, ImageTk root = Tk() root.geometry('200x200') image = Image.open("python.png") image = image.resize((20, 20)) image = ImageTk.PhotoImage(image) button = Button(width = 100, height = 20, image = image) button.pack(padx = 20, pady = 50) root.mainloop()

We add the final image to the button the same way we did with the labels, and we get the following output. Personally, I think it looks pretty good. Resizing it was nessacery, otherwise the image would have inflated the size of the button.

Tkinter PhotoImage

This marks the end of the Tkinter PhotoImage tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Источник

How to resize an image using Tkinter?

To process images with Tkinter and other Python packages, we generally use the Pillow Package (or PIL) in Python. It provides a way to load, process, manipulate, convert, and helps to resize the images. The package can be installed by using the command pip install Pillow. Once the package is installed, we can import it using the ‘from PIL import Image, ImageTk’ command.

To resize an image using the PIL package, we have to follow these steps −

  • Install Pillow Package or PIL in the local machine.
  • Open the Image using Open(image_location) method.
  • Resize the given image using resize((w,h), Image. ANTIALIAS) method where ANTIALIAS removes the structural Padding from the Image around it.
  • Display the Image using Canvas create_image(x,y, image) method.
Читайте также:  Php get json format

Example

#Import the required Libraries from tkinter import * from PIL import Image,ImageTk #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x270") #Create a canvas canvas= Canvas(win, width= 600, height= 400) canvas.pack() #Load an image in the script img= (Image.open("download.png")) #Resize the Image using resize method resized_image= img.resize((300,205), Image.ANTIALIAS) new_image= ImageTk.PhotoImage(resized_image) #Add image to the Canvas Items canvas.create_image(10,10, anchor=NW, image=new_image) win.mainloop()

Output

Running the above code will display a window that will display a resized image in the Canvas.

Источник

Python tkinter image размер

Tkinter Advance

  • Getting screen’s height and width using Tkinter | Python
  • Python | How to dynamically change text of Checkbutton
  • Python | focus_set() and focus_get() method
  • Search String in Text using Python-Tkinter
  • Autocomplete ComboBox in Python-Tkinter
  • Autohiding Scrollbars using Python-tkinter
  • Python Tkinter – Validating Entry Widget
  • Tracing Tkinter variables in Python
  • Python | setting and retrieving values of Tkinter variable
  • Tkinter | Adding style to the input text using ttk.Entry widget
  • Python | after method in Tkinter
  • destroy() method in Tkinter | Python
  • Text detection using Python
  • Python | winfo_ismapped() and winfo_exists() in Tkinter
  • Collapsible Pane in Tkinter | Python
  • Creating a multiple Selection using Tkinter
  • Creating Tabbed Widget With Python-Tkinter
  • Open a new Window with a button in Python-Tkinter
  • Cryptography GUI using python

Applications and Projects

  • Python | Simple GUI calculator using Tkinter
  • Create Table Using Tkinter
  • Python | GUI Calendar using Tkinter
  • File Explorer in Python using Tkinter
  • Python | ToDo GUI Application using Tkinter
  • Python: Weight Conversion GUI using Tkinter
  • Python: Age Calculator using Tkinter
  • Python | Create a GUI Marksheet using Tkinter
  • Python | Loan calculator using Tkinter
  • Python | Create a digital clock using Tkinter
  • Make Notepad using Tkinter
  • Color game using Tkinter in Python
  • Python | Simple FLAMES game using Tkinter
  • Simple registration form using Python Tkinter
  • How to create a COVID19 Data Representation GUI?

Introduction

Widgets

  • Python | Creating a button in tkinter
  • Python | Add style to tkinter button
  • Python | Add image on a Tkinter button
  • Python Tkinter – Label
  • Python Tkinter | Create LabelFrame and add widgets to it
  • RadioButton in Tkinter | Python
  • Python Tkinter – Checkbutton Widget
  • Python Tkinter – Canvas Widget
  • Python Tkinter | Create different shapes using Canvas class
  • Python Tkinter | Create different type of lines using Canvas class
  • Python Tkinter | Moving objects using Canvas.move() method
  • Combobox Widget in tkinter | Python
  • maxsize() method in Tkinter | Python
  • minsize() method in Tkinter | Python
  • resizable() method in Tkinter | Python
  • Python Tkinter – Entry Widget
  • Tkinter – Read only Entry Widget
  • Python Tkinter – Text Widget
  • Python Tkinter – Message
  • Python | Menu widget in Tkinter
  • Python Tkinter – Menubutton Widget
  • Python Tkinter – SpinBox
  • Progressbar widget in Tkinter | Python
  • Python-Tkinter Scrollbar
  • Python Tkinter – ScrolledText Widget
  • Python Tkinter – ListBox Widget
  • Scrollable ListBox in Python-tkinter
  • Python Tkinter – Frame Widget
  • Scrollable Frames in Tkinter
  • How to make a proper double scrollbar frame in Tkinter
  • Python Tkinter – Scale Widget
  • Hierarchical treeview in Python GUI application
  • Python-Tkinter Treeview scrollbar
  • Python Tkinter – Toplevel Widget
  • Python | askopenfile() function in Tkinter
  • Python | asksaveasfile() function in Tkinter
  • Python – Tkinter askquestion Dialog
  • Python Tkinter – MessageBox Widget
  • Create a Yes/No Message Box in Python using tkinter
  • Change the size of MessageBox – Tkinter
  • Different messages in Tkinter | Python
  • Change Icon for Tkinter MessageBox
  • Python – Tkinter Choose color Dialog
  • Popup Menu in Tkinter
Читайте также:  Внешняя таблица стилей

Geometry Management

Binding Functions

Tkinter Advance

  • Getting screen’s height and width using Tkinter | Python
  • Python | How to dynamically change text of Checkbutton
  • Python | focus_set() and focus_get() method
  • Search String in Text using Python-Tkinter
  • Autocomplete ComboBox in Python-Tkinter
  • Autohiding Scrollbars using Python-tkinter
  • Python Tkinter – Validating Entry Widget
  • Tracing Tkinter variables in Python
  • Python | setting and retrieving values of Tkinter variable
  • Tkinter | Adding style to the input text using ttk.Entry widget
  • Python | after method in Tkinter
  • destroy() method in Tkinter | Python
  • Text detection using Python
  • Python | winfo_ismapped() and winfo_exists() in Tkinter
  • Collapsible Pane in Tkinter | Python
  • Creating a multiple Selection using Tkinter
  • Creating Tabbed Widget With Python-Tkinter
  • Open a new Window with a button in Python-Tkinter
  • Cryptography GUI using python

Applications and Projects

  • Python | Simple GUI calculator using Tkinter
  • Create Table Using Tkinter
  • Python | GUI Calendar using Tkinter
  • File Explorer in Python using Tkinter
  • Python | ToDo GUI Application using Tkinter
  • Python: Weight Conversion GUI using Tkinter
  • Python: Age Calculator using Tkinter
  • Python | Create a GUI Marksheet using Tkinter
  • Python | Loan calculator using Tkinter
  • Python | Create a digital clock using Tkinter
  • Make Notepad using Tkinter
  • Color game using Tkinter in Python
  • Python | Simple FLAMES game using Tkinter
  • Simple registration form using Python Tkinter
  • How to create a COVID19 Data Representation GUI?

Источник

Оцените статью