2

I have a image file list and I would like to generate a single output.pdf file with these images.

The following code works with one image file only (here the first element of image_list):

with open("output.pdf","wb") as f, io.BytesIO() as output:
    img = Image.open(image_list[0])
    img.save(output, format='tiff')
    f.write(img2pdf.convert(output.getvalue()))

How can I adapt this code to work with the complete list of image files ?

I tried :

with open("output.pdf","wb") as f, io.BytesIO() as output:
        img = Image.open(image_list[0])
        img.save(output, format='tiff')
        img2 = Image.open(image_list[1])
        img2.save(output, format='tiff')
        f.write(img2pdf.convert(output.getvalue()))

but it doesn't work (the created pdf contain only the last image, ie image_list[1])

Mathias
  • 177
  • 2
  • 10

2 Answers2

2

A possible hack could be to run it through ImageMagick's convert:

import os
os.system('convert '+' '.join(image_list)+' output.pdf')
Gianluca
  • 536
  • 4
  • 7
  • Thanks for your response !The final goal is a standalone executable file generated with pyinstaller --onefile. So the .exe file could be executed from anyone. Your solution does the job but I would prefer a solution without running a command line. I wish I could find one. – Mathias Dec 25 '19 at 20:43
  • Try Python Wand. Though it requires ImageMagick. See http://docs.wand-py.org/en/0.5.7/ – fmw42 Dec 25 '19 at 20:45
  • In this case, I think I would prefer to generate one pdf per tiff file and merge the pdfs as it doesn't require to install ImageMagick on each computer when sharing the program. – Mathias Dec 25 '19 at 21:13
1

I have finally used the following code :

for i in....     
            # Create one pdf file per tiff file
            with open(str(i) + '.pdf', "wb") as f, io.BytesIO() as output:
                img = PIL.Image.open(str(i) + '.tiff')
                img.save(output, format='tiff')
                f.write(img2pdf.convert(output.getvalue()))

# merge the pdf file into one output pdf file
pdf_writer = PdfFileWriter()

output_file = publication_number + ".pdf"
file_list = os.listdir()
pdf_list = []
for file in file_list:
    if file.endswith(".pdf"):
        pdf_list.append(file)
pdf_list.sort(key=lambda f: int(
    ''.join(filter(str.isdigit, f))))  # trier la liste d'image du plus petit au plus grand (et pas 1, 10, 11, 2, 3)

for pdf_file in pdf_list:
    pdf_reader = PdfFileReader(pdf_file)
    for page in range(pdf_reader.getNumPages()):
        pdf_writer.addPage(pdf_reader.getPage(page))
with open(output_file, 'wb') as fh:
    pdf_writer.write(fh)

for i in range(1, max_page + 1):  
    os.remove(str(i) + '.tiff')
    os.remove(str(i) + '.pdf')
Mathias
  • 177
  • 2
  • 10