将多个图片转换成PDF文件-img2pdf

 

01 背景


TEASOFT教学软件 应用中需要将DOP文件转换成可以分发的PDF文件,主要过程是将DOP文件转换成高分辨率的BMP\JPEG文件,然后再通过 Acrobat软件将它们合成为PDF文件。

为了能够将该过程进一步自动化,可以借助于 img2pdf 0.4.0 Python软件来完成相应的转换。

该软件可以转换的图片格式:

FormatColorspaceResult
JPEGanydirect
JPEG2000anydirect
PNG (non-interlaced)anydirect
TIFF (CCITT Group 4)monochromedirect
anyany except CMYK and monochromePNG Paeth
anymonochromeCCITT Group 4
anyCMYKflate

1.软件官方下载地址

https://pypi.org/project/img2pdf/

2.软件安装

pip install img2pdf

3.软件使用范例

如下是在 img2pdf 给出的应用范例。它显示了在python应用img2pdf的多种格式。

import img2pdf

# opening from filename
with open("name.pdf","wb") as f:
	f.write(img2pdf.convert('test.jpg'))

# opening from file handle
with open("name.pdf","wb") as f1, open("test.jpg") as f2:
	f1.write(img2pdf.convert(f2))

# using in-memory image data
with open("name.pdf","wb") as f:
	f.write(img2pdf.convert("\x89PNG...")

# multiple inputs (variant 1)
with open("name.pdf","wb") as f:
	f.write(img2pdf.convert("test1.jpg", "test2.png"))

# multiple inputs (variant 2)
with open("name.pdf","wb") as f:
	f.write(img2pdf.convert(["test1.jpg", "test2.png"]))

# convert all files ending in .jpg inside a directory
dirname = "/path/to/images"
with open("name.pdf","wb") as f:
	imgs = []
	for fname in os.listdir(dirname):
		if not fname.endswith(".jpg"):
			continue
		path = os.path.join(dirname, fname)
		if os.path.isdir(path):
			continue
		imgs.append(path)
	f.write(img2pdf.convert(imgs))

# convert all files ending in .jpg in a directory and its subdirectories
dirname = "/path/to/images"
with open("name.pdf","wb") as f:
	imgs = []
	for r, _, f in os.walk(dirname):
		for fname in f:
			if not fname.endswith(".jpg"):
				continue
			imgs.append(os.path.join(r, fname))
	f.write(img2pdf.convert(imgs))

# convert all files matching a glob
import glob
with open("name.pdf","wb") as f:
	f.write(img2pdf.convert(glob.glob("/path/to/*.jpg")))

# writing to file descriptor
with open("name.pdf","wb") as f1, open("test.jpg") as f2:
	img2pdf.convert(f2, outputstream=f1)

# specify paper size (A4)
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
layout_fun = img2pdf.get_layout_fun(a4inpt)
with open("name.pdf","wb") as f:
	f.write(img2pdf.convert('test.jpg', layout_fun=layout_fun))

裁判系统中的信标系统的使用

 

02 软件测试


1.实验文件

利用TEASOFT软件生成了多个图像文件,具体文件如下图所示:

▲ 需要转换的文件

▲ 需要转换的文件

2.Python程序

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST1.PY                     -- by Dr. ZhuoQing 2020-11-27
#
# Note:
#============================================================

from headm import *
import img2pdf

file1 = r'd:\temp\1.bmp'
file2 = r'd:\temp\2.bmp'
file3 = r'd:\temp\3.jpg'

with open(r'd:\temp\1.pdf', 'wb') as f:
    f.write(img2pdf.convert(file1, file2, file3))

printf('\a')

#------------------------------------------------------------
#        END OF FILE : TEST1.PY
#============================================================

3.结果

通过上述程序最终生成文件 1.pdf。这是一个多页的PDF文件包含了三个图片文件。

如果图片是BMP文件, 在PDF中显示比较清晰。如果原始文件是JPG,在PDF中看到的文件会有被压缩后的模糊感。

 

※ 结论


通过实验验证了Python库函数 img2pdf的有效性,可以将多个图片文件合并成PDF文件。

利用该库函数,可以配合TEASOFT软件完成课件到PDF的自动转换过程。

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PIC2PDF.PY                   -- by Dr. ZhuoQing 2020-11-27
#
# Note: Combine all the file in one directory into PDF
#
# Usage:  pic2pdf <out file> <inputdir>
#
#============================================================

from head import *
import img2pdf

outdir = r'd:\temp'
outfile = 'pic.pdf'

inputdir = r'd:\picture'

#------------------------------------------------------------
if len(sys.argv) > 1:
    outfile = sys.argv[1]

    if outfile.find('.') < 0:
        outfile += ".PDF"

if len(sys.argv) > 2:
    inputdir = sys.argv[2]

#------------------------------------------------------------
filedim = os.listdir(inputdir)
if len(filedim) == 0:
    printf("No file in directory:%s.\a"%inputdir)
    exit()

files = []
for f in filedim:
    files.append(os.path.join(inputdir, f))
    printf(f)

#------------------------------------------------------------
outputfile = os.path.join(outdir, outfile)

with open(outputfile, 'wb') as f:
    f.write(img2pdf.convert(files))

printf("PDF :%s.\a"%outfile)

tspfocuswindow("TEASOFT")

#------------------------------------------------------------
#        END OF FILE : PIC2PDF.PY
#============================================================
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卓晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值