是否可以仅使用 tkinter 调整图像大小?如果可以的话,该怎么办?
您可以使用
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 = new_image.subsample(2, 2) #halve the size, it is now 1.5x the original
from PIL import Image
img = Image.open("flower.png")
img = img.resize((34, 26), Image.ANTIALIAS)
有关更多信息,请访问 http://effbot.org/imagingbook/image.htm(链接现已失效,此处是其存档)
这是一种仅使用 tkinter 调整图像(PhotoImages)大小的方法。这是一个可能适合您需求的简单函数。 这是一个基本函数,可以逐像素读取图像,只需从一个图像缩放到另一个图像即可。 它可能会很慢,但根据您的需要,它可能很适合您。 (在本文/讨论中,当我提到图像时,我指的是 PhotoImage 或 PhotoImage 的实例。)
基本上,它需要三个参数:
它返回 PhotoImage 的新实例。 如果需要,您可以将原始图像引用到返回的图像,从而有效地调整图像大小。 或者您可以检索新图像。 这是函数和一些示例:
from tkinter import *
def resizeImage(img, newWidth, newHeight):
oldWidth = img.width()
oldHeight = img.height()
newPhotoImage = PhotoImage(width=newWidth, height=newHeight)
for x in range(newWidth):
for y in range(newHeight):
xOld = int(x*oldWidth/newWidth)
yOld = int(y*oldHeight/newHeight)
rgb = '#%02x%02x%02x' % img.get(xOld, yOld)
newPhotoImage.put(rgb, (x, y))
return newPhotoImage
该函数应该逐像素调整图像大小并返回新图像。
基本上简而言之,您创建所需尺寸的图像,并且必须使用原始图像中的数据逐个像素地填充您想要的颜色。 您可以认为这个过程可能使用一条线(y=mx+b,其中b=0)或比率或比例因子,或者您想要考虑的任何方式。 最重要的是,您必须通过从原始图像中检索数据来填充新的像素数据。
要更改图像的大小,您可以执行类似的操作。 这是示例代码:
from tkinter import *
#insert the resize function here
root = Tk()
myCanvas = Canvas(root, width=300, height=300)
myCanvas.pack()
puppyImage = PhotoImage(file="bassethound.png") # a 200px x 200px image
puppyImage = resizeImage(puppyImage, 150, 150) # resized to 150px x 150px
myCanvas.create_image(50, 50, anchor=NW, image=puppyImage)
myCanvas.create_text(0, 0, anchor=NW, text="original 200x200\nnow150x150")
root.mainloop()
结果如下:
这是 200x200 的图像,使用以下代码缩小到 100x100 并扩展到 300x300:
from tkinter import *
#insert the resize function here
root = Tk()
myCanvas = Canvas(root, width=600, height=300)
myCanvas.pack()
puppyImage = PhotoImage(file="bassethound.png") # a 200px x 200px image
puppySmall = resizeImage(puppyImage, 100, 100)
puppyLarge = resizeImage(puppyImage, 300, 300)
myCanvas.create_image(0, 0, anchor=NW, image=puppyImage)
myCanvas.create_text(0, 0, anchor=NW, text="original 200x200")
myCanvas.create_image(200, 0, anchor=NW, image=puppySmall)
myCanvas.create_text(200, 0, anchor=NW, text="small 100x100")
myCanvas.create_image(300, 0, anchor=NW, image=puppyLarge)
myCanvas.create_text(300, 0, anchor=NW, text="larger 300x300")
root.mainloop()
这是该代码的结果:
这里只是一些任意数字,比如 273px 和 88px:
from tkinter import *
# put resize function here
root = Tk()
myCanvas = Canvas(root, width=400, height=100)
myCanvas.pack()
puppyImage = PhotoImage(file="bassethound.png") # a 200px x 200px image
puppyImage = resizeImage(puppyImage, 273, 88) # resized to 273px x 88px
myCanvas.create_image(0, 0, anchor=NW, image=puppyImage)
root.mainloop()
结果:
这个答案的灵感来自 acw1668 和 roninpawn,链接如下:
照片归属:
nlhyeyyeusysAnderson Nascimento,CC BY 2.0 https://creativecommons.org/licenses/by/2.0,来自 Wikimedia Commons 来自:https://en.wikipedia.org/wiki/Puppy
我不会说它在某种意义上确实调整了大小,但如果您查找文档,它会返回相同的图像,但会跳过方法中指定的 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。
PhotoImage
类,它允许您加载它们并在 GUI 中使用它们。如果您想编辑/更改图像,我认为您最好使用Python 图像库 (PIL)。