I'm having trouble loading my image into the GUI for Python using Kivy.All it shows is a white screen. Any help would be appreciated.
from kivy.app import App
from kivy.uix.image import Image
class TheApp(App):
def build(self):
image = Image(source= "psychTREE.jpg")
return image
if __name__ == "__main__":
TheApp().run()
Related
I have been trying to make an android apk in kivy to access mobile camera through opencv, but my application crashes in my phone. It is probably not able to access my phone's camera!
import imp
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDRaisedButton
from kivy.uix.image import Image
from kivy.clock import Clock
import cv2
from kivy.graphics.texture import Texture
class MainApp(MDApp):
def build(self):
layout = MDBoxLayout(orientation='vertical')
self.image = Image()
layout.add_widget(self.image)
layout.add_widget(MDRaisedButton(
text='Click Here',
pos_hint={'center_x':.5,'center_y':.5},
size_hint=(None,None))
)
self.capture=cv2.VideoCapture(0)
Clock.schedule_interval(self.load_video, 1.0/30.0)
return layout
def load_video(self, *args):
ret, frame= self.capture.read()
self.frame=frame
buffer=cv2.flip(frame, 0).tostring()
texture=Texture.create(size=(frame.shape[1],frame.shape[0]),colorfmt='bgr')
texture.blit_buffer(buffer, colorfmt='bgr',bufferfmt='ubyte')
self.image.texture=texture
if __name__ == '__main__':
MainApp().run()
Also the changes I have made in my buildozer.spec folder are as follows:
Please forgive me I am in my infancy learning Python and started using Kivy not too long ago.
I am simply trying to add photos from my "carousel/" directory in my project to be each photo that comes up in the carousel app one by one when it loads.
The code runs fine, and I am even able to load Async photos with the link to the photo, but for whatever reason when I try to load photos from my "carousel" folder they don't show up.
I know that these photos return otherwise because I used the "Image" call and it worked, I also looked all over and there were other solutions but I could not make the connection between their solution and what it is I need.
Here is the code and a picture below, like I said the code builds and runs fine but the pictures won't show up. Thank you in advance!
[from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.core.image import Image
from kivy.factory import Factory
class CarouselApp(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(0,2):
src = "carousel/%s.jpg" % str(i)
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()][1]
https://i.stack.imgur.com/igyeq.png
You have to store all your pictures in a folder called carousel, and you can remove the Factory.
Example
main.py
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
class CarouselApp(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(0, 6):
src = "carousel/%s.png" % str(i)
image = AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()
Output
You are trying to load 'carousel/0.jpg' and 'carousel/1.jpg' instead of 'carousel_images/00.jpg' and 'carousel_images/01.jpg', Try this:
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.core.image import Image
from kivy.factory import Factory
class CarouselApp(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(0,2):
src = "carousel_images/0{}.jpg".format(str(i))
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()
This is a kivy python script with carousel I found in the web which I am trying to replicate.
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.factory import Factory
from kivy.uix.image import Image
class Example1(App):
def build(self):
carousel = Carousel(direction='right',loop='true')
for i in range(1,5):
src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
#load images asynchronously
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
print(i)
return carousel
if __name__ == '__main__':
Example1().run()
This downloads all the images at once which works well for smaller sized and lesser number of images. When I tried it in other larger number of images with consideraly larger size. It took a long time to even load those images to kivy app.
Is there a way that we can load the images one by one? Say when we run the kivy app instead of downloading all images together; the first slide of carousel should only download the first image and when we swipe left or right the corresponding slides image should download.
you can create a custom generator function to load the previous or the next slide when called which then downloads or loads the respective image. this a script in my app which automatically changes the screen after every 3 seconds that but i created the carousel in my kv file manually instead of calling them from a remote source.
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
import threading
from kivy.clock import Clock
class StartScreen(Screen):
ml = ObjectProperty(None)
scroller = ObjectProperty(None)
button = ObjectProperty(None)
carousel = ObjectProperty(None)
def caller(self):
threading.Thread(target = self.call).start()
def call(self):
Clock.schedule_interval(self.changer, 3)
def changer(self,*args):
self.ids.carousel.load_next()
Here is my code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.image import Image as CoreImage
class WeaselApp(App):
def __init__(self, image):
self.image = image
def coreimage(self, load_image):
self.load_image = load_image
load_image = CoreImage("psychTREE.jpg")
#return load_image
def built(self):
return coreimage(self, load_image)
if __name__== "__main__":
WeaselApp(App).run()
When I run it, it says " WeaselApp has no attribute 'root' ". Why is this the case? Any hints or suggestions are greatly appreciated.
You don't return any widget from your build method, for two reasons:
As Joran Beasley said, you need a build method, not built.
CoreImage is not a widget that can be displayed on screen, it's a low level tool to load image data. You should use a kivy.uix.image.Image widget.
Edit: Following the comments, replace your code with this:
from kivy.app import App
from kivy.uix.image import Image
class YourApp(App):
def build(self):
return Image(source='psychTREE.jpg')
YourApp().run() # edited this in after
I made a little Kivy application as an interface. The carousel seems to be working, but I would like to make the carousel start by itself (that means without using a mouse). In fact, the app will be deployed on a little robot that does not have any mouse or keybord, thus that is why I need the carousel passing the images by itself.
As you can see in my code, the carousel does work, but I just can't figure out how to make it start without clicking on it with a mouse.
class Logo(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(2):
src = "image.png"
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
if __name__ == '__main__':
Logo().run()
Any ideas ? Thanks.
You want an event to trigger the movement, i think the easiest way would be to use a Clock.
from kivy.clock import Clock
then in your build, you could schedule incrementing the position of the carousel at some interval, carousel has a convenient function for that load_next, but if you just do that, it'll get stuck on the last frame pretty fast, so you'll probably want to pass loop=True to the Carousel instantiation.
from kivy.app import App
from kivy.factory import Factory
from kivy.uix.carousel import Carousel
from kivy.clock import Clock
class Logo(App):
def build(self):
carousel = Carousel(direction='right', loop=True)
for i in range(2):
image = Factory.Label(text=str(i))
carousel.add_widget(image)
Clock.schedule_interval(carousel.load_next, 1)
return carousel
if __name__ == '__main__':
Logo().run()
(i changed the images by labels to see the difference more easily)