I want to create GridLayout in Kivy with many rectangle buttons with some custom images with different size. To do so, I want to scale image (the way I wan to do this is shown below) and then crop/hide the parts that overflow the widget borders. The resizing works nice, but I can not figure out how to hide/crop that parts which are outside the rectangle. For example: if widget have dimensions of 10px x 10px and my image is 100px x 200px I will rezise it to 10px x 20px and center it in widget, but the 5px below and abowe will be visible. I do not want that. :) Cloud somebody help me with this problem?
class PlaceIcon(Widget):
def __init__(self,image_path, **kwargs):
super(PlaceIcon, self).__init__(**kwargs)
self.bind(size=self.adjust_size)
self.image = Image(source=image_path)
self.image_path = image_path
def adjust_size(self,*args):
(a,b) = self.image.texture.size
(x,y) = self.size
(x1,y1) = self.pos
if x > y:
scale = x/a
else:
scale = y/b
x1 -= (scale*a-x)/2
y1 -= (scale*b-y)/2
with self.canvas:
self.canvas.clear()
self.background = Rectangle(texture=self.image.texture, pos=(x1,y1), size=(scale*a,scale*b))
I would say to look at Texture.get_region: http://kivy.org/docs/api-kivy.graphics.texture.html#kivy.graphics.texture.Texture.get_region
So you can use only the part of the texture you want to display and passing as if it was an entirely different texture.
Another way would be to use StencilView, but i think here the get_region is better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With