Mosaic multiple images using Python

Source: Internet
Author: User
Tags glob

Writing machine learning related blog posts, often encounter many formulas, and latex formally edit the formula of the weapon. At present, the domestic commonly used blog system, as if only the blog park support, so chose to settle in the blog park. I am now basically using latex to write blog, and then to publish to the blog park to share with you, you have to go through a kung fu: first, the latex source code to copy to the blog HTML source Editor, and then, modify some of the HTML unsupported latex source code, Makes the last post almost like the PDF document I generated. This design to the icon of the reference, the paper reference, the text color adjustment, part of the paragraph marking and other issues. Once the document is a bit long, it's frustrating to do the work. Most annoying is that the final revision of the finished HTML display of the article is also very beautiful, and the text size and title what will change with the theme of the blog changes, aesthetics is not as good as the latex generated PDF. I also want to see things that are beautiful to me, even if it's a blog post! People, are inert, I would like to share with you the experience of learning, but do not want to spend too much time on these trivial things above.

  People are inert, please forgive me always have some lazy ideas. So how is it easy to share the contents of a PDF document with everyone? There seems to be no blog support for direct browsing of PDF documents, but almost all blogs support images. So, we can use software like Adobe to turn PDFs into images in JPG or PNG format, but the conversion is a single picture for each page of the PDF. I don't even bother to upload the more than 10-page picture to blog post, and then the picture size is not suitable for the words have to adjust, really no patience! I would like to have a tool that can help me merge all these pictures directly. Think about it, there seems to be no ready-made tools available. But it seems to be not difficult, I can completely handle it. These reasons have contributed to this short blog post.    I plan to post all the following operations:  use Latex to write the original blog post, create a PDF document, convert the PDF into a high-definition PNG image, merge multiple PNG images into one large picture, and upload the final big picture directly to the Post editor Ah ha, it's done! OK, what if I convert the PDF document to another picture format? I recommend that you do this 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 with Adobe Acrobat X Pro as a picture, a series of "Pdffilename_ page _xx.png" is generated in the same directory as the PDF document A series of pictures. 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 handleThe 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:   #!/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 th E 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  & nbsp     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):        F ile=files[i]        Im=image.open (file)         Im=im.resize (Sz,image.antialias )         mat=np.atleast_2d (IM)         print (file)         Basem At=np.append (basemat,mat,axis=0)     Final_img=image.fromarray (basemat)     Final_img.save (' Merged.png ')

Stitching multiple pictures with Python

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.