How to use img2pdf module as a module

I am creating a tiny Python 2.7.9 script that takes a list of JPEG images as input and outputs a PDF. I have been working for a couple of hours but cannot find a solution:

  • I tried pypdf but it says I don't have PIL, although I do.
  • I tried Reportlab but the page size is bigger than the image itself and I couldn't find a way to fix it. It's also hard for me ...
  • I tried img2pdf but couldn't figure out how to use it.

I am really tired of all these libraries and I am looking for a good solution . Good means:

  • How pythonic possible
  • size page size to image size
  • Support for a large number of images (maybe even 500)
  • easy enough

If you can help with the modules I've already tried / code examples that would be great. Also, if you have any experience with the module, I would be glad if you could share it.

Edit:

As suggested in the comments, I decided to give img2pdf one more try. I am using the following code from the official GitHub README :

import img2pdf
pdf_bytes = img2pdf('test.jpg', dpi=150)

      

But it throws an exception: `TypeError: 'module' object could not be called

Does anyone know how to use img2pdf as a module for multiple images? `

+3


source to share


1 answer


Nothing in the GitHub code tells me that this module is callable. It is possible that there is an error in the readme; they are for an example of using the function convert

. Try this instead:

import img2pdf
pdf_bytes = img2pdf.convert(('test.jpg',), dpi=150, x=None, y=None)

      

In the source code, this function requires positional arguments x

and y

:

def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=None,
            creationdate=None, moddate=None, subject=None, keywords=None,
            colorspace=None, verbose=False):

      



But as long as you provide an argument dpi

, it is set to the default case, which makes it look (at least to me), since both x

and y

assumed optional arguments:

    if not x and not y:
        pdf_x, pdf_y = 72.0*width/ndpi[0], 72.0*height/ndpi[1]

      

The package you received on PIP is not necessarily identical to the GitHub code. You can try it without supplying x

and y

as arguments convert

, and - or even better, find the source file on your own machine and see for yourself.

+1


source







All Articles