是否可以仅使用tkinter调整图像大小?
答案 0 :(得分:4)
from PIL import Image
img = Image.open("flower.png")
img = img.resize((34, 26), Image.ANTIALIAS)
有关详细信息,请转到http://effbot.org/imagingbook/image.htm
答案 1 :(得分:3)
以防万一有人遇到这个以供将来参考,因为我之前正在寻找这个。你可以使用tkinter的PhotoImage =>子样本方法
我不会说它确实在某种意义上调整了大小但是如果你查看文档它会返回相同的图像,但会跳过方法中指定的X像素数量。
即:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, ....)
canvas_image = tk.PhotoImage(file = path to some image)
#Resizing
canvas_image = canvas_image.subsample(2, 2) #See below for more:
#Shrinks the image by a factor of 2 effectively
canvas.create_image(0, 0, image = canvas_image, anchor = "nw")
self.canvas_image = canvas_image #or however you want to store a refernece so it's not collected as garbage in memory
所以说我们的原始图像是400x400,现在实际上是200x200。当我需要编写游戏或我制作的东西并且不想处理PIL及其编译问题时,这就是我一直在使用的。
然而,除了上述原因,我只使用PIL。
答案 2 :(得分:3)
您可以使用zoom
和subsample
方法调整PhotoImage的大小。两种方法都返回新的PhotoImage对象。
from tkinter import *
root = Tk() #you must create an instance of Tk() first
image = PhotoImage(file='path/to/image.gif')
larger_image = image.zoom(2, 2) #create a new image twice as large as the original
smaller_image = image.subsample(2, 2) #create a new image half as large as the original
但是,这两种方法都只能将整数值作为参数,因此功能有限。
可以按十进制值进行缩放,但速度很慢并且质量下降。下面显示的是可以按比例缩放1.5倍
new_image = image.zoom(3, 3) #this new image is 3x the original
new_image = image.subsample(2, 2) #halve the size, it is now 1.5x the original
答案 3 :(得分:1)
据我所知(自从我接触过Tkinter已经有一段时间了),它是一个GUI工具包。最接近“图像”的是PhotoImage
类,它允许您加载它们并在GUI中使用它们。如果您想编辑/更改图片,我认为您最好使用Python imaging library (PIL)。