当前位置:   article > 正文

为android编译python的图像库pillow_python pil android

python pil android

    Pillowpython的图像处理库,可以从github下载。pillow中部分代码使用C编写,如果需要在android环境中使用,则需要编译为android的共享库。编译过程与编译环境相关,这里使用redhat 7.0,已经安装了python 2.7androidndk

  Pillow使用了jpeg, png, freetype库,首先需要编译它们为静态库.

1.编译jpeg,png,freetype为静态库

    jpeg使用版本jpegsrc.v9bpng使用版本libpng-1.6.26

    a.首先设置交叉编译环境

export ANDROID_NDK="/usr/local/android/android-ndk-r10d"

export PATH="$ANDROID_NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/:$ANDROID_NDK:$ANDROID_NDK/tools:/usr/local/bin:/usr/bin:/bin:$PATH"

export ARCH="armeabi"

export CFLAGS="-DANDROID -mandroid -fomit-frame-pointer --sysroot=$ANDROID_NDK/platforms/android-9/arch-arm"

export CXXFLAGS="$CFLAGS"

export CC="arm-linux-androideabi-gcc $CFLAGS"

export CXX="arm-linux-androideabi-g++ $CXXFLAGS"
 
export AR="arm-linux-androideabi-ar"

export RANLIB="arm-linux-androideabi-ranlib"

export STRIP="arm-linux-androideabi-strip --strip-unneeded"

export READELF="arm-linux-androideabi-readelf"

export MAKE="make -j4 CROSS_COMPILE_TARGET=yes"

export LAPACK=

    b. 编译jpeg

    解压jpegsrc.v9b.tar.gz

./configure LDFLAGS="-llog -lm" CFLAGS="-mandroid -fomit-frame-pointer -Wno-format" --disable-shared --host=arm-linux --build=i686-pc-linux-gnu --prefix="/usr/local/android/install/jpeg9b.armeabi"
make
make install

    c. 编译png

    解压libpng-1.6.26.tar.gz

./configure LDFLAGS="-llog -lm" CFLAGS="-mandroid -fomit-frame-pointer -Wno-format" --disable-shared --host=arm-linux --build=i686-pc-linux-gnu --prefix="/usr/local/android/install/libpng1626.armeabi"
make
make install

    d. 编译freetype

    解压 freetype-2.6.5.tar.gz
./configure LDFLAGS="-llog -lm" LIBPNG_LIBS="-L/usr/local/android/install/libpng1626.armeabi/lib" CFLAGS="-mandroid -fomit-frame-pointer -Wno-format" LIBPNG_CFLAGS="-I/usr/local/android/install/libpng1626.armeabi/include" --disable-shared --host=arm-linux --build=i686-pc-linux-gnu --prefix="/usr/local/android/install/freetype265.armeabi"
make
make install

2. 编译pillow

    github 下载 pillow ,解压 Pillow-master.zip
    打补丁: patch -p0 -i pillow-master.android.patch, Pillow版本不同可能需要修改补丁,可参照修改
   
diff -ruN ../version/Pillow-master/setup.py Pillow-master/setup.py
--- ../version/Pillow-master/setup.py	2016-11-02 01:10:30.000000000 +0800
+++ Pillow-master/setup.py	2016-11-03 20:27:54.905900300 +0800
@@ -182,6 +182,7 @@
         library_dirs = []
         include_dirs = []
 
+        self.compiler.linker_so[0] = os.environ.get('CC')   
         _add_directory(include_dirs, "libImaging")
 
         #
@@ -380,7 +381,9 @@
         # between Python PYD stub libs and real libraries
 
         self.compiler.library_dirs = library_dirs + self.compiler.library_dirs
-        self.compiler.include_dirs = include_dirs + self.compiler.include_dirs
+        self.compiler.include_dirs = include_dirs # + self.compiler.include_dirs
+        self.compiler.include_dirs.remove('/usr/local/include')
+        self.compiler.include_dirs.remove('/usr/include')
 
         #
         # look for available libraries
@@ -395,7 +398,8 @@
                 elif (sys.platform == "win32" and
                       _find_library_file(self, "zlib")):
                     feature.zlib = "zlib"  # alternative name
-
+        feature.zlib = "z"  #====================
+          
         if feature.want('jpeg'):
             _dbg('Looking for jpeg')
             if _find_include_file(self, "jpeglib.h"):
@@ -559,7 +563,7 @@
         exts = [(Extension("PIL._imaging",
                            files,
                            libraries=libs,
-                           define_macros=defs))]
+                           define_macros=defs,extra_link_args = ['-Wl,-soname,_imaging.so']))]
 
         #
         # additional libraries
@@ -567,7 +571,7 @@
         if feature.freetype:
             exts.append(Extension("PIL._imagingft",
                                   ["_imagingft.c"],
-                                  libraries=["freetype"]))
+                                  libraries=["freetype","png"],extra_link_args = ['-Wl,-soname,_imagingft.so']))
 
         if feature.lcms:
             extra = []
@@ -575,7 +579,7 @@
                 extra.extend(["user32", "gdi32"])
             exts.append(Extension("PIL._imagingcms",
                                   ["_imagingcms.c"],
-                                  libraries=[feature.lcms] + extra))
+                                  libraries=[feature.lcms] + extra,extra_link_args = ['-Wl,-soname,_imagingcms.so']))
 
         if feature.webp:
             libs = [feature.webp]
@@ -589,7 +593,7 @@
             exts.append(Extension("PIL._webp",
                                   ["_webp.c"],
                                   libraries=libs,
-                                  define_macros=defs))
+                                  define_macros=defs,extra_link_args = ['-Wl,-soname,_webp.so']))
 
         tk_libs = ['psapi'] if sys.platform == 'win32' else []
         exts.append(Extension("PIL._imagingtk",
@@ -597,8 +601,8 @@
                               include_dirs=['Tk'],
                               libraries=tk_libs))
 
-        exts.append(Extension("PIL._imagingmath", ["_imagingmath.c"]))
-        exts.append(Extension("PIL._imagingmorph", ["_imagingmorph.c"]))
+        exts.append(Extension("PIL._imagingmath", ["_imagingmath.c"],extra_link_args = ['-Wl,-soname,_imagingmath.so']))
+        exts.append(Extension("PIL._imagingmorph", ["_imagingmorph.c"],extra_link_args = ['-Wl,-soname,_imagingmorph.so']))
 
         self.extensions[:] = exts

    编译需要 python 头文件 , 下载 starcore_for_android, 解压到 pillow 同级目录

   

    使用python2.7编译,首先设置交叉编译环境

export ANDROID_NDK="/usr/local/android/android-ndk-r10d"

export PATH="$ANDROID_NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/:$ANDROID_NDK:$ANDROID_NDK/tools:/usr/local/bin:/usr/bin:/bin:$PATH"

export ARCH="armeabi"

export CFLAGS="-DANDROID -mandroid -fomit-frame-pointer --sysroot=$ANDROID_NDK/platforms/android-9/arch-arm -I../starcore_for_android.2.5.1/python.files/python-2.7/armeabi/include/python2.7 -I/usr/local/android/install/jpeg9b.armeabi/include  -I/usr/local/android/install/freetype265.armeabi/include"

export CXXFLAGS="$CFLAGS"

export CC="arm-linux-androideabi-gcc"

export LDFLAGS="-lm -L../starcore_for_android.2.5.1/python.files/python-2.7/armeabi  -L/usr/local/android/install/jpeg9b.armeabi/lib -L/usr/local/android/install/freetype265.armeabi/lib  -L/usr/local/android/install/libpng1626.armeabi/lib"

export CXX="arm-linux-androideabi-g++"
 
export AR="arm-linux-androideabi-ar"

export RANLIB="arm-linux-androideabi-ranlib"

export STRIP="arm-linux-androideabi-strip --strip-unneeded"

export READELF="arm-linux-androideabi-readelf"

export MAKE="make -j4 CROSS_COMPILE_TARGET=yes"

export LAPACK=

     运行python setup.py build
    编译结果在 Pillow-master/build/lib.linux-x86_64-2.7
    PIL打包成zip,既可以在android应用中使用

3.测试pillow


   测试使用eclipse开发,可以导入到android studio
   PIL.zip放到assets目录,在app中将其解压到"/data/data/"+getPackageName()+"/files"
   assets中创建test.py,在app中将test.py拷贝到"/data/data/"+getPackageName()+"/files"
   
  1. import os, sys
  2. import traceback
  3. try:
  4. from PIL import Image
  5. print(Image)
  6. print(AppPath)
  7. im=Image.open(AppPath+"/dir.jpg")
  8. print(im.size)
  9. print(im.mode)
  10. print("===end===")
  11. except Exception,e:
  12. traceback.print_exc()

    加载 test.py
  1. python._Set("AppPath", "/data/data/"+getPackageName()+"/files");
  2. Service._DoFile("python", "/data/data/"+getPackageName()+"/files/test.py", "");

    关于 android 中使用 python ,请参考之前的文章
    示例下载








声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/474898
推荐阅读
相关标签
  

闽ICP备14008679号