如何使用 tkinter 调整图像大小? [英] How to resize an image using tkinter?

查看:350
本文介绍了如何使用 tkinter 调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用 tkinter 调整图像大小?如果是这样,那怎么做?

Is it possible to resize an image using tkinter only? If so, how can that be done?

推荐答案

您可以使用 zoomsubsample 方法调整 PhotoImage 的大小.这两种方法都返回一个新的 PhotoImage 对象.

You can resize a PhotoImage using the zoom and subsample methods. Both methods return a new PhotoImage object.

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

但是,这两种方法都只能将整数值作为参数,因此功能有限.

However, both of these methods can only take integer values as arguments, so the functionality is limited.

可以按十进制值进行缩放,但速度很慢并且会降低质量.下面的代码演示了 1.5 倍的缩放:

It is possible to scale by decimal values but it is slow and loses quality. The below code demonstrates scaling by 1.5x:

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

这篇关于如何使用 tkinter 调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆