Using a gif as a texture for an ellipse/rectangle

888 views
Skip to first unread message

Kyle Pearson

unread,
Mar 23, 2014, 8:11:26 AM3/23/14
to kivy-...@googlegroups.com
Hi, 

I was wondering if someone could help me render a gif as a texture for an ellipse without using the kv language. My code right now is

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.image import Image
from kivy.graphics import Ellipse

class TestGif(Widget):
    def __init__(self, **kw):
        super(TestGif, self).__init__(**kw)
        with self.canvas:
            self.body = Ellipse(texture=Image('test_anim.gif').texture,size=self.size,pos=self.pos)

class TestApp(App):
    def build(self):
        return TestGif()

if __name__ == '__main__':
    TestApp().run()

I can get the ellipse to show however the gif will not animate. I would be open to a solution that involves rendering the gif as a texture for an ellipse/rectangle or just displaying the gif as an image without having to use the kv language if that is possible.
test_gif.py

Noob Saibot

unread,
Mar 24, 2014, 4:20:38 PM3/24/14
to kivy-...@googlegroups.com
Try setting the texture as an ObjectProperty, and using kvlang:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.image import Image
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty

class TestGif(Widget):
    ellipse_image
= ObjectProperty(Image(source='test_anim.gif'))
   
#ellipse_image = ObjectProperty(Image(source='~/.../test_anim_gif_frames.zip'), anim_delay=0.1)


class TestApp(App):

   
def build(self):
       
return TestGif()

Builder.load_string("""
<TestGif>:
    canvas.before:
        Ellipse:
            texture: self.ellipse_image.texture
            size: self.size
            pos: self.pos
"""
)


if __name__ == '__main__':
   
TestApp().run()

Though i will say, i have my doubts as to whether or not this is possible by just adding an animated Gif. You may need to split the frames to let kivy take over the animation part. If so, the commented ellipse_image would apply to that case.

Good luck.
Message has been deleted

Kyle Pearson

unread,
Mar 25, 2014, 11:41:55 PM3/25/14
to kivy-...@googlegroups.com
I appreciate your help on this matter however I still am not able to get this to work. The first thing I had to do to your code was remove the "source=" after Image since it didn't like it with the version of Kivy I'm using v1.8.0. 

I receive this error when I keep it attached:
ellipse_image = ObjectProperty(Image(source='test_anim.gif'))
 TypeError: __init__() takes exactly 2 arguments (1 given)

Which had me thinking that I was not using the proper environment to display the image. So originally my Image provider was set to pygame but that didn't work, I got this error:

[DEBUG  ] [ImagePIL    ] Load <test_anim.gif>
Fatal Python error: (pygame parachute) Segmentation Fault

So I then set it to gif by including these two lines at the top of my code
import os
os.environ['KIVY_IMAGE'] = 'gif'

Doing that gave me a segmentation fault when I ran the program: 
[DEBUG  ] [ImageGIF    ] Load <test_anim.gif>
Segmentation fault

After getting that error with the gif, I switched to PIL and created a zip of my images which almost worked. I am now getting an error that says: no images in zip <test_anim.zip> however I have images named test1.png, test2.png and test3.png. Do you have any experience with the proper naming convention and/or image type that should go into the zip file?

Thanks

Kyle Pearson

unread,
Mar 26, 2014, 7:53:23 PM3/26/14
to kivy-...@googlegroups.com
I was able to solve the issue! Using the syntax from your snippt @Noob I was able to get an animated image.

It was a matter of using 

from kivy.uix.image import Image

not 
from kivy.core.image import Image



I am still not able to achieve the same result not using the kv language, I copied the style from your snippet of code and tried to translate it to non-kv and all I get is a static image but I guess I can deal with using the kv language. 

    1. from kivy.app import App
    2. from kivy.uix.widget import Widget
    1. from kivy.uix.image import Image
    2. from kivy.graphics import Ellipse
    1. from kivy.properties import ObjectProperty
    2. class TestGif(Widget):
    1.    ellipse_image = ObjectProperty(Image(source='test_anim.zip',anim_delay=0.1))
    2.    #ellipse_image = ObjectProperty(Image(source='test_anim.gif'))
    1.     def __init__(self, **kw):
    2.        super(TestGif, self).__init__(**kw)
    1.         with self.canvas.before:
    2.            Ellipse(texture=self.ellipse_image.texture,size=self.size,pos=self.pos)
    Reply all
    Reply to author
    Forward
    0 new messages