Python3 Tkinter Set Image Size

TKinter python 3.x resizing an image

I had to keep a reference of the image that was being used by the label like so:

logo = PhotoImage(file="image.png")
logo = logo.subsample(2, 2)
label = Label(root,image=logo, borderwidth=0, highlightthickness=0)
label.image = logo
label.pack()

Tkinter resizing images in labels arranged in a grid

You can use Image.thumbnail() to resize the image:

# putting 100th frame of video with 'path' into the label
def pack_thumbnail(path, label):
with imageio.get_reader(path) as video:
image = video.get_data(100) # use get_date() instead of get_next_data()
w, h = 200, 200 # thumbnail size
image = Image.fromarray(image)
image.thumbnail((w, h)) # resize the image
frame_image = ImageTk.PhotoImage(image)
label.config(image=frame_image, width=w, height=h)
label.image = frame_image

Tkinter resize background image to window size

This is example application that uses Pillow to resize image on the Label as the label changes size:

from tkinter import *

from PIL import Image, ImageTk

root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")



class Example(Frame):
def __init__(self, master, *pargs):
Frame.__init__(self, master, *pargs)



self.image = Image.open("./resource/Background.gif")
self.img_copy= self.image.copy()


self.background_image = ImageTk.PhotoImage(self.image)

self.background = Label(self, image=self.background_image)
self.background.pack(fill=BOTH, expand=YES)
self.background.bind('<Configure>', self._resize_image)

def _resize_image(self,event):

new_width = event.width
new_height = event.height

self.image = self.img_copy.resize((new_width, new_height))

self.background_image = ImageTk.PhotoImage(self.image)
self.background.configure(image = self.background_image)



e = Example(root)
e.pack(fill=BOTH, expand=YES)


root.mainloop()

This is how it works using Lenna image as example:

enter image description here

How can I resize image without stretching it in Tkinter?

Here is a solution:

from tkinter import Tk, Label
from PIL import Image, ImageTk

root = Tk()

root.title("Display Images")
root.geometry('550x550')
root.maxsize(550, 550)
root.configure(background='#333')

image = Image.open('./image.jpg')

# main part -----------------------------------------
if image.width > 540 and image.height > 540:
resized_image = image.resize((540, 540), Image.ANTIALIAS)
elif image.width > 540:
resized_image = image.resize((540, image.height), Image.ANTIALIAS)
elif image.height > 540:
resized_image = image.resize((image.width, 540), Image.ANTIALIAS)
else:
resized_image = image
# end main part ----------------------------------------

disp_image = ImageTk.PhotoImage(resized_image)

label = Label(image=disp_image, border=0)
label.pack(padx=10, pady=10)

root.mainloop()

Simple use of if statements.

How to resize a list of images (100), shown 2 by 2 in the same window and they have to change size according to the destination, enlarge the window?

You can try adding these changes to your Creator class (first just try copy-pasting and see if this works, then if you have questions, ask them):

from tkinter import Tk, Frame, Label
from PIL import Image, ImageTk


class Example(Frame):
def __init__(self, master, path, *args):
Frame.__init__(self, master, *args)

self.image = Image.open(path)
self.img_copy = self.image.copy()

self.background_image = ImageTk.PhotoImage(self.image)

self.background = Label(self, image=self.background_image)
self.background.pack(fill='both', expand=True)
self.background.bind('<Configure>', self._resize_image)

def _resize_image(self, event):
new_width = event.width
new_height = event.height

self.image = self.img_copy.resize((new_width, new_height))

self.background_image = ImageTk.PhotoImage(self.image)
self.background.configure(image=self.background_image)


def change_images(index):
if index >= len(img_lst):
index = 0
img_lst[index].lift()
root.after(1000, change_images, index + 1)


path_lst = ['pause_btn.png', 'play_btn.png', 'space.jpg'] # change/add paths here
img_lst = []

root = Tk()

for path in path_lst:
e = Example(root, path)
e.place(x=0, y=0, relwidth=1, relheight=1)
img_lst.append(e)

change_images(0)

root.mainloop()

In your code (remember to import other stuff too):

from tkinter import Tk, Frame, Label
from PIL import Image, ImageTk


class Example(Frame):
def __init__(self, master, array, *args):
Frame.__init__(self, master, *args)

self.image = Image.fromarray(array)
self.img_copy = self.image.copy()

self.background_image = ImageTk.PhotoImage(self.image)

self.background = Label(self, image=self.background_image)
self.background.pack(fill='both', expand=True)
self.background.bind('<Configure>', self._resize_image)

def _resize_image(self, event):
new_width = event.width
new_height = event.height

self.image = self.img_copy.resize((new_width, new_height))

self.background_image = ImageTk.PhotoImage(self.image)
self.background.configure(image=self.background_image)



class Applications(Tk):
def __init__(self):
Tk. __init__(self)
self.path="E:/1-RICHI/MovilesDB"
self.Images=self.files(self.path, "ever") # list one
self.Images_copy=self.files(self.path, "partial") # list two
self._toplevel()

def files(self, path, option): # here I generate the lists

images=os.listdir(path). # route
self.list_partial=[] # internal list
self.list_ever=[] # internal list
if option == "ever":
for i in images:
if ".jpg" in i:
route=path + "/" + i
open=cv2.imread (route)
if open is None:
continue
RGB=cv2.cvtColor(open, cv2.COLOR_BGR2RGB)
self.list_ever.append(RGB)
return self.list_ever

if option == "partial" :
for i in images:
if ".jpg" in i:
route=path + "/" + i
open=cv2.imread(route)
if open is None:
continue
RGB=cv2.cvtColor(open, cv2.COLOR_BGR2RGB)
objet=Image.fromarray(RGB)
self.list_partial.append(objet)
return self.list_partial

def _toplevel(self):
top1=Toplevel()
frame=Creator()
frame.pack()

class Creator(Frame):

def __init__(self,*args, **kwargs):
Frame.__init__(self,*args, **kwargs)

self.LIST=[] # img_lst
for i in self.master.Images:
Example(self, i).place(x=0, y=0, relwidth=1, relheight=1)

if __name__=="__main__":
app_1 =Applications()
app_1.mainloop()

Dynamically resize images in Tkinter

1) You need to get current screen resolution: How do I get monitor resolution in Python?


2) Then you need to adjust size of your image (How do I resize an image using PIL and maintain its aspect ratio?) and/or your window (https://yagisanatode.com/2018/02/23/how-do-i-change-the-size-and-position-of-the-main-window-in-tkinter-and-python-3/)

So the code should look like that:

from win32api import GetSystemMetrics
from Tkinter import Tk

screen_width, screen_height = GetSystemMetrics(0), GetSystemMetrics(1)

root = Tk() # this is your window
root.geometry("{}x{}".format(screen_width//2, screen_height//2)) # set size of you window here is example for 1/2 screen height and width

img = Image.open("picture_name.png", "r") # replace with picture path
width, height = screen_width//4, screen_height//4 # determine widght and height basing on screen_width, screen_height
img.resize((width, height), Image.ANTIALIAS)

# todo: create more Tkinter objects and pack them into root

root.mainloop()

This should probably solve your problem. Regarding Tkinter usage, there are a lot of tutorials, example: http://effbot.org/tkinterbook/



Related Topics



Leave a reply



Submit