Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an image using kivy

Tags:

python

kivy

How do I display an image in my pwd?

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image


class MyApp(App):
  def build(self):
    return Image('b1.png')

MyApp().run()
like image 636
Rajeev Avatar asked May 14 '14 10:05

Rajeev


People also ask

How do I open images on Kivy?

Basically, set your image file name under source attribute under "Image" in kivy. kv (file). Remember to set a clock schedule once as above and you can make your modification of your photo. You can even add some photo looping (dynamically) by changing the clock scheduler to Clock.


2 Answers

You can check the Image documentation to see that the image source is controlled by the source property. Therefore you should be able to change just one line to make it work:

    return Image(source='b1.png')
like image 189
inclement Avatar answered Sep 19 '22 19:09

inclement


The acceptable result for me is too comprehensive and simple.

I have a better way of doing it using .kv file:

  • Set your photo at a particular box_layout using "ids"
  • You can change your photo dynamically

Kivy.kv (file)

<main_display>:
    BoxLayout:
        orientation: "vertical"
        
        Image:
              id: imageView
              source: '<random_name>.jpg'
              allow_stretch: True
 ....

Kivy.py (file)

class main_display(BoxLayout):
    def __init__(self, **kwargs):
       super(main_display,self).__init__()
       # Photo can be reference by running the photo function once:
       Clock.schedule_once(self.photo)
    
     def photo(self,dt):
       # Replace the given image source value:
       self.ids.imageView.source = 'kivy_test.jpg'
  1. Basically, set your image file name under source attribute under "Image" in kivy.kv (file).
  2. Remember to set a clock schedule once as above and you can make your modification of your photo. You can even add some photo looping (dynamically) by changing the clock scheduler to Clock.schedule_interval(self.photo, 0.06).

I have try to directly assign the attribute 'source' from kivy.py (file) but failed with assertion error.

ENJOY and please do comment if you're not clear !

like image 41
Jason Akon Avatar answered Sep 19 '22 19:09

Jason Akon