将图像调整为可变/固定尺寸? [英] Resizing an image to variable/fixed dimensions?

查看:64
本文介绍了将图像调整为可变/固定尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个3行1列的GridLayout.第一行和最后一行将是标签",而中间行将包含一个图像.

Let's say you have a GridLayout with 3 rows and 1 column. The first and last rows will be Labels, and the middle row will contain an image.

调整图像大小以使其具有固定高度(与标签高度相同或不相同)并且宽度与布局宽度相同的最佳方法是什么?是否可以在kivy内执行此操作,还是有必要引入其他模块(例如PIL)并使用图像缓冲区?仅创建一个以图像为背景的非绑定按钮是否合理(因此,让kivy负责调整大小),还是这种解决方案有问题?谢谢.

What is the best way to resize the image such that it has a fixed height (may or may not be the same height as the Labels), and its width is the same as the width of the layout? Is it possible to do this within kivy, or is it necessary to bring in some other module, such as PIL, and use an image buffer? Would it be reasonable just to create a non-binded button with the image as a background (thus, letting kivy take care of the resizing), or is there a problem with this solution? Thanks.

推荐答案

调整图像大小以使其具有固定高度(与标签高度相同或不相同)并且宽度与布局宽度相同的最佳方法是什么?

What is the best way to resize the image such that it has a fixed height (may or may not be the same height as the Labels), and its width is the same as the width of the layout?

我不确定您在这里问的是什么.您是否在谈论图像"小部件?您可以使用

I'm not sure exactly what you're asking here. Are you talking about the Image widget? You could do this with

Image:
    source: 'whatever'
    size_hint_y: None  # Tells the layout to ignore the size_hint in y dir
    height: dp(40)  # The fixed height you want

您还可以通过检出

是否可以在kivy内执行此操作,还是有必要引入其他模块(例如PIL)并使用图像缓冲区?仅创建一个以图像为背景的非绑定按钮是否合理(因此,让kivy负责调整大小),还是这种解决方案有问题?

Is it possible to do this within kivy, or is it necessary to bring in some other module, such as PIL, and use an image buffer? Would it be reasonable just to create a non-binded button with the image as a background (thus, letting kivy take care of the resizing), or is there a problem with this solution?

即使我误解了您的问题,我认为您想要做的任何事情都应该很容易做到.我认为您的按钮解决方案虽然不能解决您的问题,也无法解决您的问题,但是您仍然需要针对Image小部件的大小进行说明以使其缩放,但是您没有获得任何收益将其放在按钮中.而且,无需使用Button即可轻松完成按钮background_image属性的任何缩放-如上所述,您可以使用图像"小部件的不同属性来控制宽高比缩放等.

Even if I misunderstood your question, I think whatever you want should be easy to do in kivy. I don't think your button solution would solve your problem though, or why you'd think it would - you would still need to give instructions on the size of the Image widget to make it scale, but then you didn't gain anything from putting it in a Button. And any scaling of the button's background_image properties is something you can easily accomplish without the Button - as above, you can control aspect ratio scaling etc. with the different properties of the Image widget.

这篇关于将图像调整为可变/固定尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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