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

#!/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

转载于:https://www.cnblogs.com/jadeboy/p/4156406.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值