Python implementation of multi-image stitching method

Source: Internet
Author: User
Tags glob
This article describes the Python implementation of multiple images stitching method. Share to everyone for your reference. The specific analysis is as follows:

The plan described here is to do the following:

① The original blog post with latex to generate PDF documents;
② convert PDF to high-definition PNG image;
③ combine images from multiple PNG formats into a single large picture;
④ upload the final big picture directly to the post editor

OK, what if I convert the PDF document to another picture format? I recommend that you complete this work with Adobe Acrobat X Pro software under Windowns, as shown in the following two figure. Note In Figure II must specify a resolution, do not use automatic, otherwise the resulting image size will be different. In my many attempts to see, the resolution is set too large, although the image is still very clear after magnification, but paste to blog post still need to constantly adjust the size, select "59.06 pixels/cm" is very suitable. It should be noted that the theme of the blog to choose the kind of pages for the blog to display more wide, or paste the picture is not very good.

When you save a PDF document as a picture with Adobe Acrobat X Pro, a series of images named "Pdffilename_ page _xx.png" are generated in the same directory as the PDF document. Our next task is to combine these images into one image. I chose the powerful and convenient python to do this task. Just started using the Matplotlib library to operate, but finally found that the matplotlib in the preservation of the image function (whether image.imsave () or Pyplot.imsave ()) has a certain limit, that is, the length or width of the picture can not exceed 32768. This limit makes me very dissatisfied, continue to try other image operation Library, finally found that the PIL library does not exist this limit, the problem has been resolved. The following Python code by default all pictures corresponding to the order of the file name at the end of the ascending sequence, the sequence number can be discontinuous, can handle the image name must be shaped like xx_1.png ... xx_100.png or xx_001.png ... xx_100.png. The last short and concise Python code is as follows:

The code is as follows:

#!/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 have 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 ')

Hopefully this article will help you with Python programming.

  • 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.