Kivy Android Save Image - python

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!

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.

How to open file chooser in kivy android webview?

I created a webview app using kivy for android. Using following.
from kivy.app import App
from jnius import autoclass
from kivy.clock import Clock
from android.runnable import run_on_ui_thread
from kivy.uix.widget import Widget
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
#run_on_ui_thread
def create_webview(*args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('https://google.com')
class Wv(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.__functionstable__ = {}
Clock.schedule_once(create_webview, 0)
class ServiceApp(App):
def build(self):
return Wv()
Using buildozer I have made apk. But the problem is that when I visit some website and try to upload image using [choose file] button then file chooser window not opeining. I view logcat before click and after click but still not found any log about this.
How can I resovle this issues? How can select image from sdcard and upload it to website opened in webview kivy (android)?
Thanks

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 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

Creating an Image Popup in Kivy

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.

Categories