CodeMode CodeMode - 2 months ago 12
Python Question

Kivy Image Child Class Produces Two Images

I think this is probably a simple miss understanding about something deeper in kivy, but I have an RoundedImage class that is producing two images, where one is the original without rounded corners and one with the rounded corners off screen a bit. Whats going on?

Screenshot:
http://imgur.com/gallery/IRYfr

I think it may be something to do with subclassing Image?

class RoundedImage(Image,StyleUnit):

_styleist = RadialGradientStyleist
_radius = [20]
_source = ''
_style = None
def __init__(self,**kwargs):

super(RoundedImage,self).__init__(**kwargs)
#self._source = source
#self.initalizeStyle()

with self.canvas:
StencilPush()
self.m_rect = RoundedRectangle( size = self.norm_image_size , \
pos=self.center, \
radius=self._radius)
StencilUse()
self.rect = Rectangle( size = self.norm_image_size , \
pos = self.center, \
texture = self.texture)
StencilUnUse()
StencilPop()
#Color(1,1,1)
#self.line = Line( rounded_rectangle=self.pos+self.size+self._radius,
# width=10)


self.bind(pos = self.update_rect,
size = self.update_rect)

def update_rect(self,*args):
self.m_rect.pos = self.center
self.m_rect.size = self.norm_image_size
self.rect.pos = self.center
self.rect.size = self.norm_image_size


The application code is simple:

class ProfilesApp(App):

def build(self):
profile = RoundedImage( source = source,#self.imageLocation,\
allow_stretch=True)
return profile

profileApp = ProfilesApp()
profileApp.run()

Answer

Ok so this ended up being an issue of not removing a mask in the stencil instructions.

I ended up adding this to the .kv definition for the RoundedRectangle

'''
<-RoundedImage>:
canvas:
Color:
rgb: self.color
StencilPush
RoundedRectangle:
size: self.norm_image_size
pos: self.center[0] - self.norm_image_size[0]/2.0,self.center[1] - self.norm_image_size[1]/2.0
radius: self._radius
StencilUse
Rectangle:
texture: self.texture
size: self.norm_image_size
pos: self.center[0] - self.norm_image_size[0]/2.0,self.center[1] - self.norm_image_size[1]/2.0
StencilUnUse
RoundedRectangle:
size: self.norm_image_size
pos: self.center[0] - self.norm_image_size[0]/2.0,self.center[1] - self.norm_image_size[1]/2.0
radius: self._radius
StencilPop

'''