self.lab1 = tk.Label(self.root)Īt the moment we do not write anything, until something is selected in the listbox below: self.listbox = tk.Listbox(self.root)īefore we make the listbox visible, we populate it with the files in the folder with the glob function for file in glob.glob("*.png"): I think it’s a good idea to put a label below the image in wich we will show the name of the image, the size and the with and height of it. Self.label = tk.Label(self.root, image=self.img) We do create a label with the image passed to the class Resize self.img = tk.PhotoImage(file=self.image) Then we will create a class that will create a window with the widgets for an image, the list of images in the folder, an entry for the shrink ratio (how much we want to make the image smaller… or bigger) and a button to do the action.Īfter we created a window and a frame class Resize: We need to import Image form PIL, for images, tkinter for the GUI and glob to search files Nothin’ too fancy, but useful to get a little into the way tkinter works to make a little more easy to use the function (shrink), withount having to change the code of the app everytime we want to resize an image. We will see the images in a folder and then we can select an image and resize them clicking a button. I am going to use a function that i made for that purpose to create an app with a GUI (graphic user interface) that makes easier to resize images. This will take the code that we’ve seen in another post to make covers for Youtube with the PIL module, so I invite you to read that one before you watch this video below, if you do not get immediately the first lines of code. Where those lines of code at the beginning come The app will be very basic, just to show a practical application of tkinter and Python. (image = self.In this first part we will see how to build an app to resize images. Self.image = self.img_copy.resize((new_width, new_height)) Self.background = Label(self, image=self.background_image) Self.background_image = ImageTk.PhotoImage(self.image) Self.image = Image.open("./resource/Background.gif") This is example application that uses Pillow to resize image on the Label as the label changes size: from tkinter import * Not sure if there is a way of doing this in tkinter? Or if perhaps I would write my own function that resizes the image according to the window size, however the image needs to resize relatively smoothly and quickly if the user resizes the window at any point. The centering functionality is already there, I just need the resize functionality.Ĭurrently what I have is: from tkinter import *īackground_image = PhotoImage(file="Background.gif")īackground = Label(root, image=background_image, bd=0) In a 400x900 window, the image does not resize, and centers itself horizontally.In a 500x400 window, the image resizes to 500x500, and still centers itself vertically.In a 800x400 window, the image does not resize, and centers itself vertically.Is there a way of resizing the image so that if the largest of the width and height of the window is smaller than the image, the image is adjusted to that size, keeping aspect ratio. However when the window is smaller than the image in width and height, it puts the center of the image in the center of the window, so you don't see the whole image, and it looks a little odd. The image is placed over the background, and if the window is wider than it is tall, the image centers itself in the middle over the black background, and it all looks very nice. I have a square background image, which fades to black around the edges, and then the main window has a black background. Trying to set up a background for my tkinter window.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |