Python implements the method of stitching multiple images together

  • 2020-04-02 14:26:47
  • OfStack

This article illustrates an example of Python's approach to splicing multiple images. Share with you for your reference. Specific analysis is as follows:
 
The plan here implements the following operations:
 
Write original blog posts with Latex to generate PDF documents;
PDF to hd PNG format of the picture;
Will multiple PNG format of the picture into a large picture;
Upload the final big picture directly to the blog editor

Okay, what if I convert a PDF document to another image format? I recommend Adobe for Windows downs   The Acrobat X Pro software does this in the following two figures. Note that in figure 2, you must specify a resolution, do not use automatic, otherwise the resulting image size will be different. In my many attempts, the resolution was set too high, and although the image was still sharp when enlarged, it still required constant resizing to post to the post. "59.06 pixels/cm" was a good choice. It is important to note that the topic of the blog should be selected for the blog post to display a relatively wide page, otherwise posted on the picture is not very good.
 
After saving a PDF document as a picture with Adobe Acrobat X Pro, a series of images called "PDFfilename_ page _xx.png" is generated in the directory where the PDF document is located. Our next task is to combine these images into a single image. I chose the powerful and convenient Python for this task. At first, I used the matplotlib library to operate, but I finally found that the Image saving function in matplotlib (whether image.imsave () or pyplot.imsave()) has a certain limitation, that is, the length or width of the Image cannot exceed 32768. This limitation made me very unhappy, and I continued to try other libraries for image manipulation. Finally, I found that PIL library did not have this limitation, and the problem was solved. The following Python code defaults to an ascending sequence of the sequence number at the end of the file name. The sequence number may not be continuous. The image name that can be processed must be xx_1.png... Xx_100. PNG or xx_001.png... Xx_100. PNG. The final short Python 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 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 has helped you with your Python programming.


Related articles: