Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to rezize image in python tkinter

from tkinter import *
from PIL import ImageTk, Image
root=Tk()

image = Image.open('path_to_your_image.png')
# The (450, 350) is (height, width)
image = image.resize((450, 350), Image.ANTIALIAS)
my_img = ImageTk.PhotoImage(image)
my_img = Label(image = my_img)
my_img.pack()

root.mainloop()
Comment

python resize image in tkinter

# How to resize an image in Tkinter to exactly half of the original size
from tkinter import *
from PIL import Image, ImageTk

# Function to resize an image
def resize_image():
    global img_info
    global img
    global c
    global new_width
    global new_height

    # They are int
    print("Width type: ", type(new_width), "Height type: ", type(new_height))

    # Divide the original width by 2
    resized_width = new_width / int(2)
    resized_height = new_height / int(2)

    # They are float
    print("Width type: ", type(resized_width), "Height type: ", type(resized_height))

    # Convert float to int
    resized_width = int(resized_width)
    resized_height = int(resized_height)

    # They are int
    print("Width type: ", type(resized_width), "Height type: ", type(resized_height))

    # Declare new size to the image
    img_info = img_info.resize((resized_width, resized_height), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img_info)
    c.create_image(0, 0, image=img, anchor=NW)

    # New size of image
    print("Size of image: " + str(resized_width) + "x" + str(resized_height))

# Setting up the window
root = Tk()

# Resize image button
Button(root, text='resize image', command=lambda:resize_image()).pack()

# Display image
img_info = Image.open("placeholder.jpg")
new_width, new_height = img_info.size

# Size of image
print("Size of image: " + str(new_width) + "x" + str(new_height))

c = Canvas(root, width=new_width, height=new_height, bg="black")
c.pack()

img = ImageTk.PhotoImage(Image.open(r"placeholder.jpg"))
c.create_image(2, 2, image=img, anchor=NW)

# Main loop
root.mainloop()
Comment

PREVIOUS NEXT
Code Example
Python :: install spotipy 
Python :: how to convert data type of a column in pandas 
Python :: remove python ubuntu 
Python :: selenium python find all links 
Python :: sudo python3 -m pip install pyautogui 
Python :: how to round the values in a list 
Python :: get stats from array 
Python :: how to add legend to python plot 
Python :: python download image 
Python :: python capture exception 
Python :: discord.py unban command 
Python :: current datetime pandas 
Python :: python delete directory if exists 
Python :: how to import pygame onto python 
Python :: window size cv2 
Python :: renaming headers pandasd 
Python :: yyyy-mm-dd hh:mm:ss.0 python 
Python :: ipykernel pip 
Python :: export image python 
Python :: pickle a dictionary 
Python :: rotate screen trick in python 
Python :: mac install python 3.8 
Python :: pandas convert header to first row 
Python :: python how to generate random number in a range 
Python :: plus or minus symbol 
Python :: copy image from one folder to another in python 
Python :: python savefig full screen 
Python :: python string argument without an encoding 
Python :: pandas percent change 
Python :: django user form 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =