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 :: convert string to integer in dictionary python 
Python :: how to change avatar of a bot using discord.py 
Python :: python how to split a number 
Python :: image rotate in python 
Python :: install python 3.6 on centos 
Python :: how to drop a column in python 
Python :: create and populate dictionary python 
Python :: create new dataframe with columns from another dataframe pandas 
Python :: get rid of unnamed column pandas 
Python :: pandas gropu by 
Python :: python check if all caps 
Python :: regex findall 
Python :: python tkinter scrollbar widget 
Python :: multinomial regression scikit learn 
Python :: how to delete role discord py rewrite 
Python :: # convert dictionary into list of tuples 
Python :: in pandas how to start an index from a specific number 
Python :: python cheat sheet 
Python :: fillna with mode pandas 
Python :: pyqt5 close event 
Python :: elif in django template 
Python :: python extend list 
Python :: create list of numbers 
Python :: delete values with condition in numpy 
Python :: python replace character in string 
Python :: display values on top of seaborn bar plot 
Python :: scikit image 0.16.2 
Python :: python comment multiple lines 
Python :: pip install qrcode python 
Python :: update queryset in django 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =