➤01 背景
TEASOFT教学软件 应用中需要将DOP文件转换成可以分发的PDF文件,主要过程是将DOP文件转换成高分辨率的BMP\JPEG文件,然后再通过 Acrobat软件将它们合成为PDF文件。
为了能够将该过程进一步自动化,可以借助于 img2pdf 0.4.0 Python软件来完成相应的转换。
该软件可以转换的图片格式:
Format | Colorspace | Result |
---|---|---|
JPEG | any | direct |
JPEG2000 | any | direct |
PNG (non-interlaced) | any | direct |
TIFF (CCITT Group 4) | monochrome | direct |
any | any except CMYK and monochrome | PNG Paeth |
any | monochrome | CCITT Group 4 |
any | CMYK | flate |
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
#============================================================