简体   繁体   中英

I Cant get an image to be resized and displayed using tkinter

So im trying to make an app that will display images, and the image I have is 1000*1000 but this is way too big, I need a way to resize the image. I've tried using PIL and ImageTK but that didn't work, here's my code so far:

from tkinter import *

app = Tk()
app.title('embeded image')

fname = Canvas(bg = 'black', height=100, width=100)
fname.pack(side=TOP)

image = PhotoImage('Sun.png')
image = image.resize((25, 25), Image.ANTIALIAS)

icon = fname.create_image(image=image)

fname.pack()
app.mainloop()

I've no idea why this doesn't work, im relatively new to Tkinter so sorry if it's obvious.

You mix two differnt class PhotoImage in tkinter which doesn't have resize and PIL.Image which have resize

import tkinter as tk
from PIL import Image, ImageTk

app = tk.Tk()

fname = tk.Canvas(height=200, width=200)
fname.pack()

pil_image = Image.open('Sun.png')
pil_image = pil_image.resize((25, 25), Image.ANTIALIAS)

image = ImageTk.PhotoImage(pil_image)
icon = fname.create_image((0,0), image=image, anchor='nw')

app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM