Python concatenates multiple images

Source: Internet
Author: User
Tags glob

Python concatenates multiple images
Many formulas are frequently used to write machine learning-related blog posts, and Latex is a powerful tool for officially editing formulas. Currently, most popular blog Systems in China seem to only support the blog Park, so they chose to settle in the blog Park. Now I use Latex to write blog posts and share them with you on the blog site. I have to go through the following steps: first, copy the Latex source code to the blog's HTML source code editor. Then, modify some of the Latex source code that is not supported by HTML, so that the final blog is almost the same as the PDF file I generated. Here we design some column problems such as icon reference, paper reference, text color adjustment, and section labels. Once the document is too long, it will be depressing to do this. The most annoying thing is that the last modified HTML display is quite beautiful, and the size and title of the text will change with the change of the blog topic, in terms of aesthetics, it cannot match the PDF generated by Latex. For a very picky person like me, what I want to see is beautiful, even a blog! People are all inert. I am willing to share my learning experience with you, but I don't want to waste too much time on these trivial things. People are always inert. Please forgive me for having some ideas about laziness. So how can we easily share the content of the PDF document with everyone? It seems that no blog can directly browse PDF documents, but almost all blogs support images. Therefore, we can use software such as Adobe to convert a PDF file to a JPG or PNG image, but the conversion result is that each page of PDF corresponds to an image. I am too reluctant to upload images of those dozens of pages to blog posts one by one. If the image size is not suitable, I have to adjust it one by one. I really don't have the patience! I hope I can have a tool to merge all these images for me. After thinking about it, it seems that there are no ready-made tools available for use. However, it seems that it is not difficult. You can do it yourself. These factors contribute to this short blog. The following operations will be performed on all my planned blog posts: use Latex to write original blog posts, generate PDF documents, and convert PDF files to images in PNG format in high definition; merge multiple images in PNG format into a large image. Upload the final large image 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 :#! /Usr/bin/python3 # encoding = UTF-8 import numpy as npfrom PIL import Imageimport 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)'merged.png ')

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