Creating an Image Popup in Kivy - python

I'm trying to write a very basic Kivy program that will have an image popup if the right user input is given. I had one image I'm using to test with, but I keep getting the error
"pygame.error: couldn't load cattemp.jpg"
The image is in the same directory as the file, so I know that's not the issue.
Here's what the code looks like:
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.core.image import Image
def test():
popup = Popup(title='Test popup',
content=Image('cattemp.jpg').texture,
size_hint=(None, None), size=(400, 400))
Any help would be hugely appreciated.
Thank you!

You can only load PNG.
Use PIL to convert the file or use another.
Create your image with something like:
import PIL
im = PIL.Image.open('cattemp.jpg')
im.save('cattemp.png')
Then you use the image in your program
content=Image('cattemp.png').texture,
...
Can't load doesn't necessarily mean it's not found.

Related

Kivy not loading video

I'm using the following code using kivy, trying to load a video. I created a new file that didn't have all the extra things I was doing, as to eliminate all the possible complications. I'm new to stack overflow, so if I've done something wrong don't judge...The video is in the same file, and I've tried everything I found online about these kind of errors, I've also installed pillow and ffpyplayer. The error I receive is [ERROR ] [Image ] Error loading <ironman.mp4>
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.video import Video
from kivy.uix.videoplayer import VideoPlayer
class MyApp(App):
def build(self):
video = Video(source='ironman.mp4')
video.state = "play"
video.options = {'eos': 'loop'}
video.allow_stretch = True
video.loaded = True
return video
if __name__ == "__main__":
MyApp().run()
I had the same problem but with Image. Install pillow 8.4.0.

Kivy - I can't see the picture

I am developing a mobile application. There should be a picture in the introduction of my application. Even though I put the picture and show the way, the picture does not appear when I run the application. It is replaced by a white screen. How can I solve it?
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.animation import Animation, AnimationTransition
class loginImage(Image):
pass
class loginScreen(App):
def build(self):
img = Image()
return img
loginScreen().run()
My .kv code in here:
<-loginScreen>:
source: 'images.jpeg'
kv rules are for widgets, not the App class, so your source isn't applied to anything.
Set the source of the Image widget that you return.

Kivy Android Save Image

I have a simple program with the goal of saving an image to the Android Gallery.
Here is my code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.image import Image as KVImage
from kivy.core.image import Image
from os import path
class SITApp(App):
def build(self):
return Button(text=path.expanduser('~')+'/DCIM/test.jpg', on_press=self.save)
def save(self, button):
try:
Image('test.png').save(path.expanduser('~')+'/DCIM/test.jpg')
except Exception as e:
button.text = str(e)
SITApp().run()
Right now, the code does not save the image to the specified folder.
Note: The file test.png is a picture in the same directory that looks like the following:
If anyone can help me save this picture in the Gallery of an Android device, it would be extremely appreciated!

Images in Kivy app not displayed (show black box) after filling up memory, not released

I've been using the Kivy sample Python code to try to make a slideshow on the official Raspberry Pi touchscreen. When I loop through a jpg photo directory, the images eventually refuse to show up, displaying a black box only. I get no error messages. I think it's a memory leak, though I'm not sure how to test this. The following code (which downloads a photo, copies it 9 times and displays each file with a delay of 3 seconds per photo) stops displaying images at #7. If I use larger images, they stop appearing earlier. Shouldn't clearing the widgets free up the memory?
main.py
import kivy
kivy.require('1.0.6')
from kivy.clock import Clock
import urllib
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from functools import partial
from kivy.core.window import Window
import shutil
def displayphoto(i, root, *largs):
name=str(i)+".jpg"
root.clear_widgets()
img = Image(source=name, nocache=False)
root.add_widget(img)
label = Label(text=name)
root.add_widget(label)
class NoscatterApp(App):
def build(self):
root = FloatLayout()
#os.remove("*.jpg")
urllib.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Shed_with_green_roof_at_Lyngen_fjord%2C_2012_June.jpg/1200px-Shed_with_green_roof_at_Lyngen_fjord%2C_2012_June.jpg", "main.jpg")
for i in range(0, 10):
name=str(i)+".jpg"
shutil.copy("main.jpg", name)
Clock.schedule_once(partial(displayphoto, i, root), i*3)
return root
def on_pause(self):
return True
if __name__ == '__main__':
NoscatterApp().run()
A bit late, but try using AsyncImage instead of Image
from kivy.uix.image import AsyncImage
img = Image(source=name, nocache=False)
If this doesn't fix the issue, try setting nocache=True.
This significantly reduced the amount of times I encountered a black image, but still not fully fixed.
Finally I ended up resizing and compressing the original image using PIL, which completely fixed the issue for me.
from PIL import Image as PILImage # Name conflict with kivy.uix.image
img = Image(nocache=False)
pil_image = PILImage.open(name)
pil_image.thumbnail((512, 512)) # Pick any size you want.
pil_image.save("temp/current_image.jpg", format='JPEG', optimize=True, quality=90)
# Lower quality = more compression
img.source = "temp/current_image.jpg"
The original image on disk remains the same and only the one displayed is compressed. Any other technique that reduces the image size should also work.

Kivy image not showing

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖
Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

Categories