前幾日在博客上看到一篇“使用python拼接多張圖片”的Blog【具體是能將的圖片名字必須是形如xx_1.png ... xx_100.png或者xx_001.png ... xx_100.png,拼接成一張png圖片,來達到一些目的(默認所有圖片對應的順序是文件名末尾序號的升序,序號可以不連續)】,自己也正想學習Python,覺得有趣就想試試。先是在windows上嘗試了下,就遇到各種問題;正好有台mac(對mac也不熟悉),就想借機會也了解下mac。就copy了該短小精悍的代碼...之后蛋疼的歷程就驚現了(當然也是合理的,好如 基本功沒學懂的人就強行修煉上乘的九陰真經一般,實在是很費力,弄不好就入魔了); 該python代碼如下(命名為:margePng.py):
#!/usr/bin/python3 #encoding=utf-8 import numpy as np from PIL import Image import glob,os if __name__=='__main__': prefix=input('Input the prefix of images:') files=glob.glob(prefix+'_*') num=len(files) filename_lens=[len(x) for x in files] #length of the files min_len=min(filename_lens) #minimal length of filenames max_len=max(filename_lens) #maximal length of filenames if min_len==max_len:#the last number of each filename has the same length files=sorted(files) #sort the files in ascending order else:#maybe the filenames are:x_0.png ... x_10.png ... x_100.png index=[0 for x in range(num)] for i in range(num): filename=files[i] start=filename.rfind('_')+1 end=filename.rfind('.') file_no=int(filename[start:end]) index[i]=file_no index=sorted(index) files=[prefix+'_'+str(x)+'.png' for x in index] print(files[0]) baseimg=Image.open(files[0]) sz=baseimg.size basemat=np.atleast_2d(baseimg) for i in range(1,num): file=files[i] im=Image.open(file) im=im.resize(sz,Image.ANTIALIAS) mat=np.atleast_2d(im) print(file) basemat=np.append(basemat,mat,axis=0) final_img=Image.fromarray(basemat) final_img.save('merged.png')
使用mac自帶的python 運行了之后就有問題了【沒有 PIL庫】:
ImportError: No module named PIL
然后就開始搜索怎么解決,一般都是這樣的答案:
1、下載PIL的Source Kit(因為這個包支持全部平台) Imaging--1.1.6.tar.gz URL: http://www.pythonware.com/products/pil/index.htm 2、解壓縮包 tar -zxvf Imaging-1.1.6.tar.gz 3、進入到解壓后的目錄 cd Imaging-1.1.6 4、Build pakage: python setup.py build_ext -i 5、測試; python selftest.py 6、安裝 python setup.py install
進行到第4步操作的時候就又出現了問題:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/tk.h:78:11: fatal error: 'X11/Xlib.h' file not found # include <X11/Xlib.h> ^ 1 error generated. error: command 'cc' failed with exit status 1
繼續搜索怎么解決,得到答案是需要安裝 pip; 通過pip安裝pil ; 好吧,do it 運行這個:
sudo easy_install pip
【溫馨說明: Pip 是安裝python包的工具,提供了安裝包,列出已經安裝的包,升級包以及卸載包的功能。
Pip 是對easy_install的取代,提供了和easy_install相同的查找包的功能,因此可以使用easy_install安裝的包也同樣可以使用pip進行安裝。
Pip的安裝可以通過源代碼包,easy_install或者腳本。$ easy_install pip
可是運行了之后就又出現了問題:
pip install Pil Downloading/unpacking Pil Could not find any downloads that satisfy the requirement Pil Some externally hosted files were ignored (use --allow-external Pil to allow). Cleaning up... No distributions at all found for Pil Storing debug log for failure in /Users/macbook/Library/Logs/pip.log
經過了一次又一次的google或者度娘: 找到一篇詳細的好文章:Mac OSX 10.9安裝python Pillow
於是開始安裝Pillow:通過git下載源碼地址https://github.com/python-imaging/Pillow ( git clone https://github.com/python-imaging/Pillow.git )
因為有前車之鑒的提醒:此次沒走太多的彎路【因為知道了這個編譯成功需要libjpeg的支持】;
所以就開始安裝libjpeg : brew install libjpeg 【幸好先前發現mac 沒有apt-get,就按照網上的分享,安裝了安裝brew】
curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1
然后開始編譯安裝 : python setup.py build_ext -i
安裝成功之后重新編譯pillow :
-------------------------------------------------------------------- version Pillow 2.4.0 platform darwin 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] -------------------------------------------------------------------- --- TKINTER support available --- JPEG support available *** OPENJPEG (JPEG2000) support not available --- ZLIB (PNG/ZIP) support available *** LIBTIFF support not available --- FREETYPE2 support available *** LITTLECMS2 support not available *** WEBP support not available *** WEBPMUX support not available --------------------------------------------------------------------
測試一下 : python selftest.py
-------------------------------------------------------------------- Pillow 2.4.0 TEST SUMMARY -------------------------------------------------------------------- Python modules loaded from /Users/macbook/yyang/app-devel-source/python/Pillow/PIL Binary modules loaded from /Users/macbook/yyang/app-devel-source/python/Pillow/PIL -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- JPEG support ok *** JPEG 2000 support not installed --- ZLIB (PNG/ZIP) support ok *** LIBTIFF support not installed --- FREETYPE2 support ok *** LITTLECMS2 support not installed *** WEBP support not installed -------------------------------------------------------------------- Running selftest: --- 57 tests passed.
執行了下安裝;
sudo python setup.py install
OK,竟然奇跡般的OK了。好,終於安裝成功了【內心波濤洶涌:基本功很重要哇】
然后就再次運行該copy的代碼:我擦又有問題出現了[生成的png有問題打不開]報錯如下?
Traceback (most recent call last): File "margePng.py", line 44, in <module> final_img.save('merged_test.png') File "build/bdist.macosx-10.10-intel/egg/PIL/Image.py", line 1682, in save File "build/bdist.macosx-10.10-intel/egg/PIL/PngImagePlugin.py", line 735, in _save File "build/bdist.macosx-10.10-intel/egg/PIL/ImageFile.py", line 473, in _save File "build/bdist.macosx-10.10-intel/egg/PIL/Image.py", line 434, in _getencoder IOError: encoder zip not available
因為之前安裝了libjpeg,就猜測性的將最后一句: final_img.save('merged.png') 改為了 final_img.save('merged.jpeg');好吧,這樣竟然的確生成了拼接好的jpeg格式的圖!!!
將代碼改回原來的,再來按照網上的fix方法:
wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz tar xvfz Imaging-1.1.7.tar.gz cd Imaging-1.1.7 python setup.py build_ext -i python setup.py install
之后運行 python margePng.py;問題依舊,(⊙o⊙)…!
繼續搜索網上遇到這種問題的解決辦法:http://www.tuicool.com/articles/QjEvm2 說多需要安裝ZLIB。OK install it
下載地址:http://www.zlib.net/ ;download taar 之后運行下面代碼
./configure
make
make install
卸載了之前安裝的pillow后重新install了,跑了下那段代碼。問題還是沒有解決。(⊙o⊙)…
罷了罷了: 基本功了解的不扎實,已入魔道了。還是從基礎學起吧,待的來日弄清了問題所在再將其 補錄於此!
參考Blog文章: Here and Mac OSX 10.9安裝python Pillow
