Python concatenates multiple images,
This example describes how to splice multiple images in Python. Share it with you for your reference. The specific analysis is as follows:
The plan is as follows:
① Use Latex to write original blog posts and generate PDF documents;
② Convert a PDF file into a PNG Image in high definition;
③ Merge multiple images in PNG format into a large image;
④ Upload the final big image directly to the blog Editor
Okay. What if I convert a PDF file to another image format? I recommend that you use Adobe Acrobat X Pro in windowns to complete this operation, as shown in the following two figures. Note that you must specify a resolution by yourself in the Figure 2 without using automatic resolution. Otherwise, the size of the generated image may vary. I have tried many times to see that the resolution is too large. Although the image is still clear after being enlarged, it still needs to be adjusted continuously in the post, select "59.06 pixels/Centimeter. It should be noted that the blog topic should select a page that is relatively wide for the blog to display, otherwise it will not look nice to post images.
After using the PDF document with Adobe Acrobat X pro。 as an image, a series of images named "pdpdffilename_ _xx.png" will be generated under the contents of the document. Our next task is to combine these images into one image. I chose Python, Which is powerful and convenient, to complete this task. At the beginning, matplotlib library was used for operations. However, it was found that the function for saving images in matplotlib (whether it is Image. imsave () or pyplot. imsave () has a certain limit, that is, the length or width of the image cannot exceed 32768. I was not satisfied with this restriction. I continued to try other image operation libraries and finally found that the PIL library did not have this restriction and the problem was also solved. Examples... xx_100.png1_xx_001.png... xx_100.png. Finally, the short and concise Python code is as follows:
Copy codeThe 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 will help you with Python programming.