In this Python Kivy article we want to learn about How to Load Image in Python Kivy, so Kivy is one of the best Python GUI frameworks and it is used for creating cross platform user interfaces for desktop and mobile applications. when you are building GUI applications, some times you need to load an image. in this article we want to learn about load images in Python Kivy.
First of all we need to install Python Kivy.
1 |
pip install kivy |
After installation of the kivy, now we can load our image.
1 2 3 4 5 6 7 8 9 10 11 |
from kivy.app import App from kivy.uix.image import Image class MyApp(App): def build(self): # create an Image widget img = Image(source='kivy.png') return img if __name__ == '__main__': MyApp().run() |
In the above example, we have created a new Kivy application at name of MyApp. in the build method, we have created an Image widget and set its source property to the path of the image we want to load. and lastly we return the Image widget from the build method.
After running the code, this will be the result, make sure that you have already added an image.

You can also load images from URLs or from binary data. this is an example of loading an image from a URL:
1 2 3 4 5 6 7 8 9 10 11 |
from kivy.app import App from kivy.uix.image import Image class MyApp(App): def build(self): # create an Image widget img = Image(source='https://example.com/image.png') return img if __name__ == '__main__': MyApp().run() |
In this example, we have set the source property of the Image widget to a URL instead of a local file path.
And this is an example of loading an image from binary data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from kivy.app import App from kivy.uix.image import Image from io import BytesIO from PIL import Image as PILImage class MyApp(App): def build(self): # load the image as binary data with open('path/to/image.png', 'rb') as f: img_data = f.read() # create PIL Image object from the binary data pil_img = PILImage.open(BytesIO(img_data)) # create Kivy Image widget from the PIL Image object kivy_img = Image(texture=pil_img.texture) return kivy_img if __name__ == '__main__': MyApp().run() |
In the above example, we have loaded the image as binary data using open function and rb mode. after that, we create a PIL Image object from the binary data using BytesIO class. and lastly we creates a Kivy Image widget from the PIL Image object.