Python's method of stitching multiple pictures _python

Source: Internet
Author: User
Tags glob

The example of this article is about Python's method of stitching multiple pictures. Share to everyone for your reference. The specific analysis is as follows:

The plan described here implements the following operations:

① use Latex to write the original blog, generate PDF documents;
② the PDF into a picture in the PNG format of HD;
③ the images in PNG format into one large picture;
④ upload the final big picture directly to the blog editor

Okay, what if you convert the PDF document to another picture format? I recommend that you complete this work with Adobe Acrobat X Pro software windowns, as shown in the following two illustration. Note that in figure II you must specify a resolution, do not use automatic, otherwise the resulting picture size will be different. On my many attempts to see, the resolution is set too large, although the picture is still very clear after amplification, but paste to posting still need to constantly resize, select "59.06 pixel/centimeter" is very suitable. It should be noted that the theme of the blog to choose the kind of page for the post to display a wider, or the picture up also not very good-looking.

After you save the PDF document as a picture with Adobe Acrobat X Pro, a series of pictures named "Pdffilename_ page _xx.png" are generated in the same directory as the PDF document. Our next task is to merge these pictures into one picture. I have chosen a powerful and convenient python to accomplish this task. Just started using Matplotlib Library to operate, but finally found matplotlib in the function of saving pictures (whether Image.imsave () or Pyplot.imsave ()) have certain limitations, that is the picture of the length or width can not exceed 32768. This restriction makes me very dissatisfied, continue to try other image operation of the library, finally found that the PIL library does not exist this limitation, the problem has been solved. The following Python code defaults to the order of all pictures is the name of the end of the sequence number of ascending, serial number can be discontinuous, can handle the picture name must be shaped like xx_1.png ... xx_100.png or xx_001.png ... xx_100.png. The last dapper Python code is as follows:

Copy Code code 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 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 ')

I hope this article will help you with your Python programming.

Related Article

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.