前几日在博客上看到一篇“使用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

使用python拼接多张图片.二三事的更多相关文章

  1. 使用Python拼接多张图片

    写机器学习相关博文,经常会碰到很多公式,而Latex正式编辑公式的利器.目前国内常用的博客系统,好像只有博客园支持,所以当初选择落户博客园.我现在基本都是用Latex写博文,然后要发表到博客园上与大家 ...

  2. Python拼接多张图片

    写机器学习相关博文,经常会碰到很多公式,而Latex正式编辑公式的利器.目前国内常用的博客系统,好像只有博客园支持,所以当初选择落户博客园.我现在基本都是用Latex写博文,然后要发表到博客园上与大家 ...

  3. Python 交互式解释器的二三事

    学 Python 不知道何时起成了一种风尚.这里,我也随便聊聊跟Python 的交互式解释器的几个有意思的小问题. 如何进入 Python 交互解释器? 当你安装好 Python 后,如何进入 Pyt ...

  4. Python学习笔记(二)——列表

    Python学习笔记(二)--列表 Python中的列表可以存放任何数据类型 >>> list1 = ['Hello','this','is','GUN',123,['I','Lov ...

  5. Python的单元测试(二)

    title: Python的单元测试(二) date: 2015-03-04 19:08:20 categories: Python tags: [Python,单元测试] --- 在Python的单 ...

  6. 机器学习算法与Python实践之(二)支持向量机(SVM)初级

    机器学习算法与Python实践之(二)支持向量机(SVM)初级 机器学习算法与Python实践之(二)支持向量机(SVM)初级 zouxy09@qq.com http://blog.csdn.net/ ...

  7. Java并发编程二三事

    Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...

  8. Python爬虫学习:二、爬虫的初步尝试

    我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...

  9. linux杂记(十二?) 关于账号和密码的二三事

    关于密码的二三事 关于账号和密码的二三事 久了不更linux的相关知识,实在是懒得想内容点(纯粹是懒).那么今天就来谈谈关于linux密码和账号的重要概念. 假如你的主机遭到入侵,那么对方的第一个侵入 ...

随机推荐

  1. java调用sqlldr oracle 安装的bin目录

    package com.jyc.sqlldr; import java.io.BufferedReader;import java.io.InputStream;import java.io.Inpu ...

  2. Shell_1 简介

    1 Shell 变量 只读变量 使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变. #!/bin/bash -x varName="AAA" echo ...

  3. Xcode 8 Simulator Stop Logging too much info

    按照以下内容设置即可:

  4. uva12063数位dp

    辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...

  5. 【第三课】WEBIX 入门自学-Hello World

    在看官网教程时,入门的例子就是dataTable这个空间.So,遵循官网,一起来看一下入门的DataTable组件: WEB使用时固然是先引入相应的库文件: 代码如下 <html> < ...

  6. 导航菜单跳转后,新页面上菜单CSS选定

    <div class="menu"> <ul> <li><a href="#" title="网站首页&qu ...

  7. [Android]依赖注入框架squareup的dagger

    分享一下Android依赖注入框架--Dagger使用 Dagger源码 Dagger1-Demo 希望能给大家的开发带来帮助.

  8. git flow工作流实际项目实践

    公司项目的开发流程主要是这样 代码分为 develop分支 master分支 平时我开发的时候,主要在develop分支上改动 一般来讲,有以下几种改动方式 1.直接在develop上修改代码 这种一 ...

  9. Agile

    I think Agile development methodologies is something we get from our practice. It can be just acknow ...

  10. gkENGINE重开!

    2013年中,曾信誓旦旦的要开源gkENGINE,结果一直到了现在. 拖了一年多,问题在于 - 工作太忙... 其实在2014春节假期我还是赶了赶进度,对gles2的渲染器进行了完善,但没做完.然后留 ...