使用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

相关文章
相关标签/搜索