您的位置:首页 > 编程语言 > Python开发

为ios编译python的图像库pillow

2017-03-12 16:17 323 查看
    Pillow是python的图像处理库,可以从github下载pillow中部分代码使用C编写,如果需要在ios环境中使用,则需要编译为ios的静态库。通过python命令编译出来的是共享库,需要使用ar命令将目标文件打包成静态库

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

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

   jpeg使用版本jpegsrc.v9b;png使用版本libpng-1.6.26

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

export PATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/:/usr/local/bin:/usr/bin:/bin:$PATH"

export CFLAGS="-DIOS -arch x86_64 -miphoneos-version-min=6.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk"

export CXXFLAGS="$CFLAGS"

export CC="clang $CFLAGS"

export CXX="clang++ $CXXFLAGS"

export LDFLAGS=""

export AR="ar"

export RANLIB="ranlib"

export STRIP="strip"

export MAKE="make -j4"

    b.编译jpeg

    解压jpegsrc.v9b.tar.gz

./configure LDFLAGS="-lm" CFLAGS="-fomit-frame-pointer -Wno-format" --disable-shared --host=x86_64-iphone-darwin --build=x86_64-apple-darwin  --prefix="$(pwd)/../install/jpeg9b.x86_64"
make
make install

    c.编译png

  解压libpng-1.6.26.tar.gz

./configure --disable-shared --host=x86_64-iphone-darwin --build=x86_64-apple-darwin  --prefix="$(pwd)/../install/libpng1626.x86_64"
make
make install

    d.编译freetype

  解压freetype-2.6.5.tar.gz

./configure LDFLAGS="-lm" LIBPNG_LIBS="-L$(pwd)../install/libpng1626.x86_64/lib" CFLAGS="-fomit-frame-pointer -Wno-format" LIBPNG_CFLAGS="-I$(pwd)/../install/libpng1626.x86_64/include" --disable-shared --host=x86_64-iphone-darwin --build=x86_64-apple-darwin  --prefix="$(pwd)/../install/freetype265.x86_64"
make
make install

2. 编译pillow

        从github下载pillow,解压Pillow-master.zip
在ios环境中,pillow支持交叉编译,但编译之后生成的为动态库,需要手动连接静态库
编译需要python头文件,下载starcore_for_ios,解压到pillow同级目录
       


        设置编译环境
export PATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/:/usr/local/bin:/usr/bin:/bin:$PATH"

export ARCH="x86_64"

export CFLAGS="-fomit-frame-pointer -miphoneos-version-min=6.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk -I../starcore_for_ios/python.files/python-2.7/include -I../install/jpeg9b.x86_64/include -I../install/freetype265.x86_64/include"

export CXXFLAGS="$CFLAGS"

export CC="clang"

export LDFLAGS="-lm -L../starcore_for_ios/python.files/python-2.7 -L../install/jpeg9b.x86_64/lib -L../install/freetype265.x86_64/lib -L../install/libpng1626.x86_64/lib"

export CXX="clang++"

export AR="ar"

export RANLIB="ranlib"

export STRIP="strip"

export MAKE="make -j4"
        使用python2.7编译
python setup.py build
编译结果在Pillow-master/build/lib.linux-x86_64-2.7
 
生成静态库

export OP="./build/temp.macosx-10.4-x86_64-2.7"
#_imaging.a

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar rc _imaging.a $OP/_imaging.o $OP/decode.o $OP/encode.o $OP/map.o $OP/display.o $OP/outline.o $OP/path.o $OP/libImaging/Access.o $OP/libImaging/AlphaComposite.o $OP/libImaging/Resample.o $OP/libImaging/Bands.o $OP/libImaging/BcnDecode.o $OP/libImaging/BitDecode.o $OP/libImaging/Blend.o $OP/libImaging/Chops.o $OP/libImaging/Convert.o $OP/libImaging/ConvertYCbCr.o $OP/libImaging/Copy.o $OP/libImaging/Crc32.o $OP/libImaging/Crop.o $OP/libImaging/Dib.o $OP/libImaging/Draw.o $OP/libImaging/Effects.o $OP/libImaging/EpsEncode.o $OP/libImaging/File.o $OP/libImaging/Fill.o $OP/libImaging/Filter.o $OP/libImaging/FliDecode.o $OP/libImaging/Geometry.o $OP/libImaging/GetBBox.o $OP/libImaging/GifDecode.o $OP/libImaging/GifEncode.o $OP/libImaging/HexDecode.o $OP/libImaging/Histo.o $OP/libImaging/JpegDecode.o $OP/libImaging/JpegEncode.o $OP/libImaging/LzwDecode.o $OP/libImaging/Matrix.o $OP/libImaging/ModeFilter.o $OP/libImaging/MspDecode.o $OP/libImaging/Negative.o $OP/libImaging/Offset.o $OP/libImaging/Pack.o $OP/libImaging/PackDecode.o $OP/libImaging/Palette.o $OP/libImaging/Paste.o $OP/libImaging/Quant.o $OP/libImaging/QuantOctree.o $OP/libImaging/QuantHash.o $OP/libImaging/QuantHeap.o $OP/libImaging/PcdDecode.o $OP/libImaging/PcxDecode.o $OP/libImaging/PcxEncode.o $OP/libImaging/Point.o $OP/libImaging/RankFilter.o $OP/libImaging/RawDecode.o $OP/libImaging/RawEncode.o $OP/libImaging/Storage.o $OP/libImaging/SunRleDecode.o $OP/libImaging/TgaRleDecode.o $OP/libImaging/Unpack.o $OP/libImaging/UnpackYCC.o $OP/libImaging/UnsharpMask.o $OP/libImaging/XbmDecode.o $OP/libImaging/XbmEncode.o $OP/libImaging/ZipDecode.o $OP/libImaging/ZipEncode.o $OP/libImaging/TiffDecode.o $OP/libImaging/Jpeg2KDecode.o $OP/libImaging/Jpeg2KEncode.o $OP/libImaging/BoxBlur.o $OP/libImaging/QuantPngQuant.o $OP/libImaging/codec_fd.o

#_imagingft.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar rc _imagingft.a $OP/_imagingft.o

#_imagingmath.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar rc _imagingmath.a $OP/_imagingmath.o

#_imagingmorph.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar rc _imagingmorph.a $OP/_imagingmorph.o

#_imagingtk.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar rc _imagingtk.a $OP/_imagingtk.o $OP/Tk/tkImaging.o

3.测试pillow

  创建xcode工程,添加cle,python,以及上面生成的jpeg,png,freetype,pillow的静态库。将/build/temp.macosx-10.4-x86_64-2.7/PIL目录及文件添加到工程,需要删除编译生成的.so文件
静态库需要预先导入,而且需要修改对应.py文件中的导入相关的代码,不能从包中导入,修改Image.py
#from PIL import _imaging as core
import _imaging as core
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another "
"version of Pillow or PIL")


  创建测试testpillow.py
import traceback
print(Star_folder)
try:
from PIL import Image
print(Image)

im = Image.open(Star_folder+"/Add.png")
print(im.format, im.size, im.mode)

except Exception,e:
traceback.print_exc()


  初始化加载pillow静态库

extern "C" void init_imaging(void);
extern "C" void init_imagingmorph(void);
extern "C" void init_imagingft(void);
extern "C" void init_imagingmath(void);

static VSImportPythonCModuleDef CModuleDef[]={{"_imaging",(void*)init_imaging},{"_imagingmorph",(void*)init_imagingmorph},{"_imagingft",(void*)init_imagingft},{"_imagingmath",(void*)init_imagingmath},{NULL,NULL}};

VSCoreLib_InitPython((VS_CHAR*)python_home,(VS_CHAR *)python_path,CModuleDef);


  运行test_pillow.py

char FileBuf[256];

SRPInterface->ScriptSetStr(python,"Star_folder",res_cpath);

sprintf(FileBuf,"%s/test_pillow.py",res_cpath);
SRPInterface->DoFile("python", FileBuf, NULL,NULL,VS_FALSE);


编译需要starcore_for_ios:解压后放在同一目录:

   


示例下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios python pillow