Python实现拼接多张图片的方法
本文实例讲述了Python实现拼接多张图片的方法。分享给大家供大家参考。具体分析如下:
这里所述计划实现如下操作:
① 用Latex写原始博文,生成PDF文档;
② 将PDF转成高清的PNG格式的图片;
③ 将多个PNG格式的图片合并成一大张图片;
④ 将最终的大图片直接上传到博文编辑器中
好了,如果将PDF文档转换成其他的图片格式呢?我建议windowns下可用Adobe Acrobat X Pro软件完成这个工作,操作步骤如下面两图所示。注意在图二中一定要自己指定一个分辨率,不用用自动的,否则生成的图片大小会有差异的。就我的多次尝试来看,分辨率设置得太大了,虽然图片放大后仍然很清晰,但是贴到博文中仍然需要不断地调整大小,选择“59.06像素/厘米”就非常合适了。需要注意的是,博客的主题要选那种供博文显示的页面比较宽的,否则贴图片上去也不怎么好看的。
将PDF文档用Adobe Acrobat X Pro另存为图片后,就会在PDF文档所在的目录下生成一系列的名为“PDFfilename_页面_XX.png"的一系列图片。我们接下来的任务就是要将这些图片合并成一张图片。我选用了强大便捷的Python来完成这项任务。刚开始用matplotlib库来操作,可是最终发现matplotlib中的保存图片的函数(无论是Image.imsave()还是pyplot.imsave())都有一定的限制,那就是图片的长或宽都不能超过32768。这个限制让我很不满意,继续尝试其他的图像操作的库,最终发现PIL库不存在这个限制,问题也得到了解决。下面这段Python代码默认所有图片对应的顺序是文件名末尾序号的升序,序号可以不连续,能处理的图片名字必须是形如xx_1.png ... xx_100.png或者xx_001.png ... xx_100.png。最后短小精悍的Python代码如下:
代码如下:
#!/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')
希望本文所述对大家的Python程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
