I want to create an application that takes photos from camera and show thumbnails of them on android. The related part of my code is:
from plyer import camera
from PIL import Image
.
.
.
def take_photo_from_camera(self, x):
filename = str(random.randint(0, 100000000000)) # create random filenames
self.camera.take_picture("/storage/sdcard0/MyApp/%s.jpg"%(filename), self.on_success_shot)
def on_success_shot(self, path):
#Create a thumbnail of taken photo here using PIL
I can use android camera without any problem.
I've added the PIL/pillow to the requirements of the kivy buildozer.spec file
requirements = kivy, openssl, futures, requests, plyer, pyjnius, pillow
When i want to create an apk with this configuration, the apk package succesfully builds but if i install apk and run on my android phone, i'm getting this error in logcat:
I/python (20188): Traceback (most recent call last):
I/python (20188): File "/home/mnrl/teknik/.buildozer/android/app/main.py", line 32, in <module>
I/python (20188): File "/home/mnrl/teknik/.buildozer/android/app/_applibs/PIL/Image.py", line 67, in <module>
I/python (20188): ImportError: dlopen failed: "/data/data/org.tokerteknik.tokerteknik/files/_applibs/PIL/_imaging.so" is 64-bit instead of 32-bit
I/python (20188): Python for android ended.
I think the problem related to architecture. I'm using ubuntu 16.04 64 bit and kivy buildozer installs 64 bit libraries with pip while installing requirements.
A similar problem here too: https://github.com/kivy/kivy/issues/4095 but there's not any solution.
Briefly how can i import PIL on android with kivy buildozer or how to install 32 bit libraries of PIL on 64 bit system?
Use pygame instead of PIL for basic image manipulation processes.
Add pygame to the buildozer requirements list, it works without any problem.
import pygame
picture = pygame.image.load(filepath)
picture = pygame.transform.scale(picture, (100, 100))
pygame.image.save(picture, "scaled_image.png")
Related
I'm currently trying to compile a simple Kivy app for use on Android. It runs in both my windows and linux environments without errors or warnings. When I go to package it using buildozer I get an error almost immediately. I did search for this error and although the title is very similar to other questions, the error is not.
Output from buildozer:
(mykivyinstall) oli#Bud: buildozer android debug
# Check configuration tokens
# Ensure build layout
# Check configuration tokens
# Preparing build
# Check requirements for android
# Install platform
Traceback (most recent call last):
File "/home/oli/mykivyinstall/bin/buildozer", line 11, in <module>
sys.exit(main())
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/scripts/client.py", line 13, in main
Buildozer().run_command(sys.argv[1:])
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/__init__.py", line 1059, in run_command
self.target.run_commands(args)
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/target.py", line 92, in run_commands
func(args)
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/target.py", line 102, in cmd_debug
self.buildozer.prepare_for_build()
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/__init__.py", line 176, in prepare_for_build
self.target.install_platform()
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/targets/android.py", line 467, in install_platform
self._install_p4a()
File "/home/oli/mykivyinstall/lib/python3.6/site-packages/buildozer/targets/android.py", line 524, in _install_p4a
deps = re.findall("^install_reqs = (\[[^\]]*\])", setup, re.DOTALL | re.MULTILINE)[0]
IndexError: list index out of range
My environment:
buildozer version=0.35
cython version=0.25.2
python version=3.6.6
OS is Ubuntu=18.04
Am I mixing and matching the wrong versions of things?
This seems a like a bug in the latest version of buildozer. The solution is to edit the file "../python3.6/site-packages/buildozer/targets/android.py" and replace the line
deps = re.findall("^install_reqs = (\[[^\]]*\])", setup, re.DOTALL | re.MULTILINE)[0]
with
deps = re.findall("install_reqs = (\[[^\]]*\])", setup, re.DOTALL | re.MULTILINE)[0]
Then run buildozer -v android debug
I've found a nice plugin for lldb that allows watching memory regions as images: https://github.com/carlodalmutto/ImageWatchLLDB
As I can see, this plugin was developed for Xcode and not for Android.
However, it is written in Python and therefore should be cross-platform.
I've found a directory c:\Users\username\.lldb\ on my hard drive. Its size is about 1 Gb.
I've created following files: c:\Users\username\.lldbinit, c:\Users\username\lldbinit, c:\Users\username\.lldb\lldbinit, c:\Users\username\.lldb\.lldbinit, c:\Users\username\.lldb\init
All of them have the similar content:
script print "script print Hi from lldb"
echo "echo Hi"
Unfortunately I don't see any output.
I can open LLDB console and issue these commands:
(lldb) script print "script print Hi from lldb"
script print Hi from lldb
(lldb) echo "Hi"
error: 'echo' is not a valid command.
error: Unrecognized command 'echo'.
This means that LLDB understands script print command.
Installation directory for Android Studio contains the subdirectory bin\lldb containing python scripts with pretty printers (file jstring_reader.py, gdb\printing.py, etc)
So, it seems possible to extend LLDB from recent NDK.
The question is: how can I add my custom plugins?
UPDATE. After some hacking I've found that LLDB commands, starting with the "script" string, are actually python commands:
(lldb) script print sys.executable
C:\Android\sdk\lldb\3.1\bin\LLDBFrontend.exe
(lldb) script print sys.version
2.7.10 (default, Feb 24 2016, 14:25:13) [MSC v.1900 64 bit (AMD64)]
(lldb) script print sys.path
['C:/Android/sdk/lldb/3.1/bin', 'C:/Android/Android Studio/bin/lldb/shared/jobject_printers', 'C:/Android/sdk/lldb/3.1/lib/site-packages', 'C:\\Android\\sdk\\lldb\\3.1\\bin\\python27.zip', 'C:\\Android\\sdk\\lldb\\3.1\\DLLs', 'C:\\Android\\sdk\\lldb\\3.1\\lib', 'C:\\Android\\sdk\\lldb\\3.1\\lib\\plat-win', 'C:\\Android\\sdk\\lldb\\3.1\\lib\\lib-tk', 'C:\\Android\\sdk\\lldb\\3.1\\bin', 'C:\\Android\\sdk\\lldb\\3.1', 'C:\\Android\\sdk\\lldb\\3.1\\lib\\site-packages', '.', 'C:\\Android\\Android Studio\\bin\\lldb\\shared\\stl_printers', 'C:\\Android\\sdk\\ndk-bundle\\prebuilt\\windows-x86_64\\share\\pretty-printers\\libstdcxx\\gcc-4.9']
(lldb) script import pip
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named pip
The first idea of installing missing modules with pip has failed.
I update my question: Are there any official guide on writing python plugins for lldb from NDK?
UPDATE 2. Have tried creating Anaconda virtual environment with the same python version and adding it to sys.path. Also failed.
(lldb) script os.environ['PATH']+= r'C:\Anaconda\envs\ndkpy;'
(lldb) script os.environ['PATH']+= r'C:\Anaconda\envs\ndkpy\Library\bin;'
(lldb) script import numpy as np
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: A dynamic link library (DLL) initialization routine failed.
UPDATE 3. Have found C:\Android\Sdk\lldb\3.1\lib\lib-tk. Tried importing Tkinter, but still no luck:
(lldb) script import Tkinter
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Android\sdk\lldb\3.1\lib\lib-tk\Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
Scheme1:
Just run command script import C:\\lldb_plugins\\chisel\\fbchisellldb.py or command script import C:\\lldb_plugins\\LLDB\\lldb_commands\\dslldb.py on your lldb console in Android Studio.
Scheme2:
Create file C:\Users\username\.lldbinit and put the following content to this file:
command script import C:\\lldb_plugins\\chisel\\fbchisellldb.py
command script import C:\\lldb_plugins\\LLDB\\lldb_commands\\dslldb.py
And then, run reload_lldbinit on your lldb console in Android Studio.
Writing my first Android app in Python and using Buildozer to package it. Because I will need to use numpy later on in the project, I tried packaging the following test code:
import numpy
import kivy
kivy.require('1.0.6')
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
However, I got the following error:
Traceback (most recent call last):
File "setup.py", line 251, in <module>
setup_package()
File "setup.py", line 243, in setup_package
setup(**metadata)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/core.py", line 169, in setup
return old_setup(**new_attr)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/core.py", line 152, in setup
dist.run_commands()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_ext.py", line 59, in run
self.run_command('build_src')
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 153, in run
self.build_sources()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 164, in build_sources
self.build_library_sources(*libname_info)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 299, in build_library_sources
sources = self.generate_sources(sources, (lib_name, build_info))
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 386, in generate_sources
source = func(extension, build_dir)
File "numpy/core/setup.py", line 686, in get_mathlib_info
raise RuntimeError("Broken toolchain: cannot link a simple C program")
RuntimeError: Broken toolchain: cannot link a simple C program
STDERR:
# Command failed: /usr/bin/python -m pythonforandroid.toolchain create --dist_name=myapp --bootstrap=sdl2 --requirements=kivy,numpy --arch armeabi-v7a --copy-libs --color=always --storage-dir=/home/kivy/Desktop/cam/.buildozer/android/platform/build
#
# Buildozer failed to execute the last command
# The error might be hidden in the log above this error
# Please read the full log, and search for it before
# raising an issue with buildozer itself.
# In case of a bug report, please add a full log with log_level = 2
Also, here is my buildozer.spec file:
title = My Application
package.name = myapp
package.domain = org.test
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = 1.0
requirements = kivy,numpy
orientation = portrait
osx.kivy_version = 1.9.1
fullscreen = 0
android.api = 19
android.sdk = 20
android.ndk = 9c
android.arch = armeabi-v7a
log_level = 2
warn_on_root = 1
Note that when I removed "import numpy" from the python code and removed "numpy" from the requirements list in the buildozer.spec file, my code was packaged perfectly. I am running this on VM Virtual Box which had Buildozer pre-installed.
Also it is not just Numpy giving me this issue- OpenCV is giving me the exact same errors. Will make separate post for that if needed.
This issue is reported in Python for Android (p4a) project here, didn't knew it's actual for stable p4a. Nevertheless, by link you can find PR that fixes issue. I didn't test it, but different people say it works.
You can try to build numpy with this fix, here's what you'll need:
Make sure you clean all left from current building process with command:
buildozer distclean
Clone p4a branch with fix using command:
git clone -b p4a_numpy_fix https://github.com/mahomahomaho/python-for-android fix-numpy
Change your buildozer.spec to use this cloned version of p4a (use your actual path):
p4a.source_dir = /home/ubuntu/p4a_numpy_fix
And run building apk again. If everything will work fine, you'll be able to build apk. If not you'll face another errors, no guarantees here :(
So I am looking into using Kivy for Android development. Defeating the jedi etc.
But I have hit a roadblock! I installed the Kivy VM image in VirtualBox, but when I try to run the test script:
# /usr/bin/kivy
__version__ = 1.0
from kivy.app import App
from kivy.uix.button import Button
class Hello(App):
def build(self):
btn = Button(text='Hello World')
return btn
Hello().run()
Using:
python main.py
I get:
Traceback (most recent call last):
File "main.py", line 3, in <module>
from kivy.app import App
ImportError: No module named kivy.app
do you have a file named kivy.py in the same folder as main.py?
try
#!/usr/bin/kivy
import kivy
print kivy.__file__
I tried just plain installing kivy as they say to on their website, and it worked.
sudo add-apt-repository ppa:kivy-team/kivy
apt-get install python-kivy
Does anyone know if kivy has image processing library?? I am planning to do android application project for one of my class but I do not know java. I have experience with c++ and python and I know there are some development tools for android app. I prefer to use python but c++ is also good. So anyone have information?? Thanks.
One thing you can easily do is to import numpy to your as usual, write your code accordingly. You can almost do any image processing with numpy, however it won't be accelerated...
Anyways, while building the Kivy application's apk file (I would advise you to use buildozer from github), just include it to your build. Here is an example of adding numpy to your apk build by adjusting specific line in buildozer.spec:
requirements = kivy,numpy
As far as I know, these are the modules you can add to your kivy code while building apk:
android apsw audiostream c_igraph cymunk django docutils ffmpeg gevent
greenlet hostpython igraph jpeg kivent kivy libevent libswift libxml2
libxslt lxml m2crypto msgpack mysql_connector netifaces numpy openssl
paramiko pil plyer png polygon psutil pyasn1 pycrypto pygame pyjnius
pylibpd pyopenssl pyparsing pyqrcode python sdl setuptools sqlalchemy
sqlite3 swift twisted txws wokkel zope
Let me know, if this satisfy your needs.