Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Tools to convert images.
4"""
5import glob
8def images2pdf(images, output, fLOG=None):
9 """
10 Merges multiples images into one single pdf.
11 Relies on :epkg:`img2pdf`. If an image name contains
12 ``'*'``, the function assumes it is a pattern and
13 uses :epkg:`*py:glob`.
15 :param images: images to merge, it can be a comma separated values
16 :param output: output filename or stream
17 :param fLOG: logging function
19 .. cmdref::
20 :title: Merge images into PDF
21 :cmd: -m pyquickhelper images2pdf --help
23 Merges one or several images into a single
24 PDF document.
25 """
26 from img2pdf import convert
28 if isinstance(images, str):
29 if ',' in images:
30 images = images.split(',')
31 else:
32 images = [images] # pragma: no cover
33 elif not isinstance(images, list):
34 raise TypeError("Images must be a list.") # pragma: no cover
36 all_images = []
37 for img in images:
38 if "*" in img:
39 names = glob.glob(img)
40 all_images.extend(names)
41 else:
42 all_images.append(img)
44 if fLOG is not None: # pragma: no cover
45 for i, img in enumerate(all_images):
46 fLOG("[images2pdf] {}/{} '{}'".format(i + 1, len(all_images), img))
48 if isinstance(output, str):
49 st = open(output, 'wb')
50 close = True
51 else: # pragma: no cover
52 close = False
53 st = output
55 convert(all_images, outputstream=st, with_pdfrw=False)
57 if close:
58 st.close()
60 return all_images