我到处寻找一种方法来设置图像的大小。图像被设置为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()这是创建图像的代码,我该如何设置图像的大小
发布于 2017-07-08 03:25:11
正如其他几个人所提到的,在将图像附加到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()发布于 2018-11-06 18:59:02
resize的新语法:
调整大小((pixels_x,pixels_y))
所以代码可能是这样的:
from tkinter import Tk, Label
from PIL import Image, ImageTk
root = Tk()
file = '/home/master/Work/Tensorflow/Project/Data/images/p001.png'
image = Image.open(file)
zoom = 1.8
#multiple image size by zoom
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((pixels_x, pixels_y)))
label = Label(root, image=img)
label.image = img
label.pack()
root.mainloop()基于Nelson答案
发布于 2017-07-08 02:30:51
如果可以接受简单的缩放,则可以添加以下一行:
image = PhotoImage(data=b64_data)
image = image.subsample(4, 4) # divide by 4
# image = image.zoom(2, 2) # zoom x 2
label = Label(image=image, bg="White")否则,您应该使用PIL lib,它提供了更精确的工具。
https://stackoverflow.com/questions/44977163
复制相似问题