Reputation: 30605
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?
Upvotes: 0
Views: 1459
Reputation: 30605
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.
Upvotes: 0
Reputation: 1496
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))
Upvotes: 1