简体   繁体   English

我无法使用tkinter调整图像大小并显示

[英]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. 因此,我试图制作一个将显示图像的应用程序,并且我拥有的图像为1000 * 1000,但这太大了,我需要一种调整图像大小的方法。 I've tried using PIL and ImageTK but that didn't work, here's my code so far: 我已经尝试使用PIL和ImageTK,但是没有用,到目前为止,这是我的代码:

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. 我不知道为什么这行不通,对Tkinter来说还比较陌生,所以很抱歉。

You mix two differnt class PhotoImage in tkinter which doesn't have resize and PIL.Image which have resize 你混合两种不同的充类PhotoImagetkinter不具备resizePIL.Image具有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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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