0

I want to convert the first page of pdf file as an image object. So thought of using subprocess. Subprocess only takes string as a parameter. Is there any way that I can pass a pdf page object and get image as an output.

Example:

instead of

import subprocess
params = ['convert','in.pdf','thumb.jpg']
subprocess.check_call(params)

I want something like this

import subprocess
from PyPDF2 import PdfFileWriter, PdfFileReader

q = PdfFileReader(open("in.pdf","rb"),strict=False)
page = q.getPage(0)
params = ['convert',page,'thumb.jpg']
thumbnail = subprocess.check_call(params)

I tried but failed to get the output. Is there any way to accomplish this?

2 Answers 2

1

This isn't possible, if the command you want to use doesn't allow you to give it its input via stdin. (If that is the case, you would need to write the output to file and use that as an input)

The other possibility is to pass the file via stdin, but there doesn't seem to be any possibility to turn a PyPDF2.PageObject into a bytes object. If there is, use this:

subprocess.check_call(args, stdin=to_bytes(page))
Sign up to request clarification or add additional context in comments.

1 Comment

Well writing the page to file and using that as an input worked. Thanks mate
0

I got the solution. Just like CodenameLambda said writing the page to file and then passing it as parameter works.

    import subprocess
    from PyPDF2 import PdfFileWriter, PdfFileReader

    q = PdfFileReader(open("in.pdf","rb"),strict=False)
    n = PdfFileWriter()
    n.addPage(q.getPage(0))

    with open("thumb.pdf", "wb") as outputStream:
        n.write(outputStream)

    params = ['convert','thumb.pdf','thumb.jpg']
    subprocess.check_call(params)

This will result in thumbnail of just the front page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.