Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple tiff images to pdf with python

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])

like image 733
Mathias Avatar asked Dec 22 '25 03:12

Mathias


1 Answers

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

import os
os.system('convert '+' '.join(image_list)+' output.pdf')
like image 126
Gianluca Avatar answered Dec 23 '25 15:12

Gianluca