Ale*_*all 10
您可以使用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
Run Code Online (Sandbox Code Playgroud)
但是,这两种方法都只能将整数值作为参数,因此功能有限。
可以按十进制值进行缩放,但速度很慢并且会降低质量。下面的代码演示了 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
Run Code Online (Sandbox Code Playgroud)
小智 7
from PIL import Image
img = Image.open("flower.png")
img = img.resize((34, 26), Image.ANTIALIAS)
Run Code Online (Sandbox Code Playgroud)
如需更多信息,请访问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
Run Code Online (Sandbox Code Playgroud)
该函数应该逐像素调整图像大小并返回新图像。
简而言之,您创建所需尺寸的图像,并且必须使用原始图像中的数据逐像素填充所需的颜色。您可以认为这个过程可能使用一条线(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()
Run Code Online (Sandbox Code Playgroud)
这是 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()
Run Code Online (Sandbox Code Playgroud)
这里只是一些任意数字,比如 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()
Run Code Online (Sandbox Code Playgroud)
这个答案的灵感来自以下链接中的 acw1668 和 roninpawn: How to spin an image on a canvas without using PIL?
照片归属:nnlhyeyyeusysAnderson Nascimento,CC BY 2.0 https://creativecommons.org/licenses/by/2.0,来自维基共享资源: https: //en.wikipedia.org/wiki/Puppy
据我所知(自从我接触 Tkinter 以来已经有一段时间了),它是一个 GUI 工具包。最接近“图像”的是PhotoImage允许您加载它们并在 GUI 中使用它们的类。如果你想编辑/更改图像,我认为你最好使用Python图像库(PIL)。