gpt4 book ai didi

Python3 tkinter 设置图像大小

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:38 26 4
gpt4 key购买 nike

我到处寻找设置图像大小的方法。图像设置为 url。我在网站上发现了其他问题,但都没有用。

import urllib.request, base64

u = urllib.request.urlopen(currentWeatherIconURL)
raw_data = u.read()
u.close()

b64_data = base64.encodestring(raw_data)
image = PhotoImage(data=b64_data)

label = Label(image=image, bg="White")
label.pack()

这是创建图像的代码,我将如何设置图像的大小

最佳答案

正如其他几个人所提到的,在将图像附加到 tkinter 标签之前,您应该使用 PIL 调整图像的大小:

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

root = Tk()

img = ImageTk.PhotoImage(Image.open('img-path.png').resize(pixels_x, pixels_y)) # the one-liner I used in my app
label = Label(root, image=img, ...)
label.image = img # this feels redundant but the image didn't show up without it in my app
label.pack()

root.mainloop()

关于Python3 tkinter 设置图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44977163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com