doc = fitz.open()
pdf = fitz.open("in.pdf")
for page in pdf:
pix = page.get_pixmap(matrix=fitz.Matrix(7, 7))
im = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
im = cvtColor(array(im), COLOR_RGB2GRAY)
page = "page-%i.png" % page.number
Image.fromarray(im).save(page)
img = fitz.open(page) # open pic as document
rect = img[0].rect # pic dimension
pdfbytes = img.convert_to_pdf() # make a PDF stream
img.close()
os.remove(page)
imgPDF = fitz.open("pdf", pdfbytes) # open stream as PDF
page = doc.new_page(width = rect.width, height = rect.height) # pic dimension
page.show_pdf_page(rect, imgPDF, 0) # image fills the page
doc.save("out.pdf")
I now have to save the np array as png and then convert to pdf. I would like to ask if there is a more direct way?