无法使用 tkinter 调整图像大小 [英] Cannot resize Image with tkinter

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

问题描述

我正在尝试在 python 2.7 中创建一个 tkinter 项目,用户可以在其中调整窗口大小,并且窗口内的所有内容都将随之缩放.这意味着画布、画布中的形状以及最重要的 PhotoImages 将随窗口缩放.我的问题是,我一生都无法正确调整图像大小.我知道 subsamplezoom 为此目的而存在,但首先

I am trying to create a tkinter project in python 2.7, where the user can resize the window, and everything inside the window will scale with it. This means the canvas, the shapes in the canvas and most importantly the PhotoImages will scale with the window. My problem, is for the life of me I cannot get my images to resize properly. I know that subsample and zoom exist for this purpose, but first of all

plantImage = PhotoImage(file="images/Arable_Cell.gif")
plantImage.subsample(2, 2)
canvas.create_image(0, 0, anchor=NW, image=plantImage)

在 50x50 像素图像中没有明显变化,对于 zoom(2, 2) 也是如此.重要的是要注意我知道 PIL 存在,但为了这个项目的目的,我不能下载任何额外的库.那么我做错了什么?

Makes no noticeable change in a 50x50 pixel image and same for zoom(2, 2). It is important to note I know PIL exists but for the purposes of this project I cannot download any extra libraries. So what am I doing wrong?

推荐答案

根据 文档

子样本(自我,x,y='')

subsample(self, x, y='')

返回一个基于相同的新 PhotoImage图像作为此小部件,但仅使用每个第 X 或第 Y 个像素.

Return a new PhotoImage based on the same image as this widget but use only every Xth or Yth pixel.

subsample 不会修改图像,它会创建一个新图像,所以试试这个:

I.e. subsample doesn't modify the image, it creates a new one, so try this instead:

originalPlantImage = PhotoImage(file="images/Arable_Cell.gif")
displayPlantImage = originalPlantImage.subsample(2, 2)
canvas.create_image(0, 0, anchor=NW, image=displayPlantImage)

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

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