使用python拼接多張圖片.二三事

     前幾日在博客上看到一篇「使用python拼接多張圖片」的Blog【具體是能將的圖片名字必須是形如xx_1.png ... xx_100.png或者xx_001.png ... xx_100.png,拼接成一張png圖片,來達到一些目的(默認全部圖片對應的順序是文件名末尾序號的升序,序號能夠不連續)】,本身也正想學習Python,以爲有趣就想試試。先是在windows上嘗試了下,就遇到各類問題;正好有臺mac(對mac也不熟悉),就想借機會也瞭解下mac。就copy了該短小精悍的代碼...以後蛋疼的歷程就驚現了(固然也是合理的,好如 基本功沒學懂的人就強行修煉上乘的九陰真經通常,實在是很費力,弄很差就入魔了); 該python代碼以下(命名爲:margePng.py):python

#!/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庫】:git

ImportError: No module named PIL 

而後就開始搜索怎麼解決,通常都是這樣的答案:github

一、下載PIL的Source Kit(由於這個包支持所有平臺) Imaging--1.1.6.tar.gz
   URL:  http://www.pythonware.com/products/pil/index.htm

二、解壓縮包 tar -zxvf Imaging-1.1.6.tar.gz

三、進入到解壓後的目錄 cd Imaging-1.1.6

四、Build pakage: python setup.py build_ext -i

五、測試;  python selftest.py

六、安裝 python setup.py install

進行到第4步操做的時候就又出現了問題:macos

/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 運行這個:windows

sudo easy_install pip 

【舒適說明: Pip 是安裝python包的工具,提供了安裝包,列出已經安裝的包,升級包以及卸載包的功能。
                  Pip 是對easy_install的取代,提供了和easy_install相同的查找包的功能,所以可使用easy_install安裝的包也一樣可使用pip進行安裝。app

                  Pip的安裝能夠經過源代碼包,easy_install或者腳本。$ easy_install pipcurl

但是運行了以後就又出現了問題:工具

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

相關文章
相關標籤/搜索