Use Python to stitch multiple pictures. Two or three things

Source: Internet
Author: User
Tags glob save file webp macbook

A few days ago on the blog saw a "use python splicing multiple pictures" of the blog "specifically can be the name of the image must be shaped like xx_1.png ... xx_100.png or xx_001.png ... xx_100.png, stitched into a PNG image, To achieve some purpose (the default is the order of all the pictures is the file name at the end of the sequence number of ascending, the sequence number can be discontinuous) ", I also want to learn python, feel interesting to try. The first time you try it on Windows, you get all sorts of problems, just a Mac (not familiar to your Mac) and want to take a chance and learn about your Mac. Just copy the short and concise code ... Then the course of the egg pain was discovered (of course, it is reasonable, good as the basic skills do not understand the people on the forced cultivation of the superior nine Yin Canon, it is very laborious, not good on the obsession); The Python code is as follows (named: margepng.py):

#!/usr/bin/python3#Encoding=utf-8 ImportNumPy as NP fromPILImportImageImportGlob,osif __name__=='__main__': Prefix=input ('Input The prefix of images:') files=glob.glob (prefix+'_*') Num=len (files) filename_lens=[len (x) forXinchFiles#length of the filesMin_len=min (Filename_lens)#minimal length of filenamesMax_len=max (Filename_lens)#maximal length of filenames    ifMin_len==max_len:#The last number of each filename has the same lengthfiles=sorted (Files)#sort the files in ascending order    Else:#Maybe the filenames are:x_0.png ... x_10.png ... x_100.pngIndex=[0 forXinchrange (num)] forIinchrange (num): filename=Files[i] Start=filename.rfind ('_') +1End=filename.rfind ('.') File_no=Int (filename[start:end]) index[i]=File_no Index=sorted (index) files=[prefix+'_'+STR (x) +'. PNG'  forXinchIndex]Print(Files[0]) baseimg=Image.open (files[0]) sz=baseimg.size Basemat=np.atleast_2d (baseimg) forIinchRange (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')

After running with a Mac-brought Python, there's a problem with "no PIL Library":

And then start searching how to fix it, which is usually the answer:

1. Download PIL source Kit (because this package supports all platforms) imaging--1.1.6.tar.gz   URL:  http://www.pythonware.com/products/pil/ INDEX.HTM2, unzip the package tar-zxvf Imaging-1.1.6.tar.gz3, go to the Unpacked directory CD Imaging-1.1.64, Build pakage:python setup.py build_ext-i5, testing;  Python selftest.py6, installing python setup.py install

In the 4th step, there was another problem:

' X11/xlib.h '  not found #       include <X11/Xlib.h>               ^1'cc' failed with exit Status 1

Continue searching how to solve, get answer is need to install pip; install PiL by PIP; Okay, do it run this:

"Note: Pip is a tool for installing the Python package, providing an installation package that lists the packages that have been installed, the upgrade package, and the ability to uninstall the package."
Pip is a replacement for Easy_install, which provides the same functionality for finding packages as Easy_install, so packages that can be installed using Easy_install can also be installed using PIP.

PIP installation can be via source code package, Easy_install or script. $ easy_install pip

But after running, there was a problem:

pip install pildownloading/unpacking Pil    not  find any downloads that satisfy the Requirement Pil    --allow-external Pil to allow).  Cleaning up ...    for Pil    for  in/users/macbook/library/logs/pip.log  

After a time and again Google or Niang: To find a good article: Mac OSX 10.9 Install Python Pillow

So start installing Pillow: Download the source address via git https://github.com/python-imaging/Pillow (git clone https://github.com/python-imaging/ Pillow.git)

Because there is a cautionary tale reminder: This time did not go too many detours "because of the success of this compilation requires Libjpeg support";

So start installing Libjpeg: Brew Install Libjpeg "Fortunately previously found that the Mac did not apt-get, just follow the online share, install the installation of brew"

CURL-LSSF Http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz-c/usr/local--strip 1

Then start compiling the installation: python setup.py build_ext-i

Recompile pillow after successful installation:

--------------------------------------------------------------------version Pillow2.4. 0 Platform Darwin2.7.5 (Default, 25 2013, 00:04:04) [GCC4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]  --------------------------------------------------------------------  ---TKINTER Support Available---JPEG Support AvailableOpenjpeg (JPEG2000) support notavailable---ZLIB (png/ZIP) Support availableLIBTIFF Support notavailable---FREETYPE2 Support AvailableLITTLECMS2 Support notavailableWEBP Support notavailableWebpmux Support notavailable--------------------------------------------------------------------

Test it: Python selftest.py

--------------------------------------------------------------------Pillow2.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 OKJPEG Support notinstalled---ZLIB (png/ZIP) Support OKLIBTIFF Support notinstalled---FREETYPE2 Support OKLITTLECMS2 Support notinstalledWEBP Support notinstalled--------------------------------------------------------------------Running selftest:---tests passed.

carried out under installation;

sudo python setup.py install  

OK, even miraculously OK. Good, finally installed successfully "inner rough: Basic skills are very important"

Then run the copy code again: I have a problem with the eraser [generated PNG has a problem can not open] error is as follows?

Traceback (most recent): File"margepng.py", line 44,inch<module>Final_img.save ('Merged_test.png') File"build/bdist.macosx-10.10-intel/egg/pil/image.py", line 1682,inchSave File"build/bdist.macosx-10.10-intel/egg/pil/pngimageplugin.py", line 735,inch_save File"build/bdist.macosx-10.10-intel/egg/pil/imagefile.py", line 473,inch_save File"build/bdist.macosx-10.10-intel/egg/pil/image.py", line 434,inch_getencoderioerror:encoder Zip notAvailable

Because previously installed Libjpeg, on the speculation will be the last sentence: Final_img.save (' merged.png ') changed to Final_img.save (' merged.jpeg '); okay, This actually generated a mosaic of good JPEG format of the picture!!!

Change the code back to the original, then follow the online fix method:

wget http://effbot.org/downloads/Imaging-1.1.7. tar.gz  tar xvfz Imaging-1.1.7. tar.gz  CD Imaging-1.1.7  -i  python setup.py install  

then run Python margepng.py; the problem remains, (⊙o⊙) ...!

Continue searching online for solutions to this problem: HTTP://WWW.TUICOOL.COM/ARTICLES/QJEVM2 says more needs to be installed zlib. OK Install it

: http://www.zlib.net/; Run the following code after download Taar

./Configure Makemake Install

After uninstalling the previously installed pillow, reinstall and run the code. The problem is still unresolved. (⊙o⊙) ...

Just: basic knowledge of the not solid, has been possessed of the way. Or from the basic learning, waiting for the tomorrow to find out the problem and then to record it in this!

Reference Blog article: here and Mac OSX 10.9 install Python Pillow

Use Python to stitch multiple pictures. Two or three things

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.