To convert pdf files to images, we can use pymupdf library to do it. Here is an tutorial showing how to convert.
Best Practice to Python Convert PDF to Images for Beginners – Python Tutorial
However, this way only can convert a pdf page to a small image. For example: here is a pdf file, which size is 8.50 * 11.00 in. PyMuPDF will convert pdf page to png with 612 * 792 pixel defautly.
If you want to convert pdf page to 1224 * 1584, which is 2 times than defaulty. How to do?
Load pdf file
import sys, fitz doc = None file =r'F:\1.pdf' try: doc = fitz.open(file) except Exception as e: print(e) if doc: doc.close() exit(0)
Get the first pdf page
first_page = doc[0]
Set the pdf matrix with given scale
image_matrix = fitz.Matrix(fitz.Identity) image_matrix.preScale(2, 2)
(2, 2) means the size of page is 2 times in width and height. You also can set the value to be (2.5, 2.5) or others
Convert pdf page to image
pix = first_page.getPixmap(alpha = False, matrix=image_matrix) pix.writePNG('demo2.png')
Then you will get an image with 1224 * 1584.
Notice: if pix.writePNG() function can not be used, you should check the version of the PyMuPDF. Then, you can check the functions in Pixmap class.
For example, you also can use writeImage() function to save images in Pixmap class.
In this artical (https://www.tutorialexample.com/python-convert-pdf-to-images-with-given-scale-using-pymupdf-python-tutorial/)
pix.writePNG(‘demo2.png’) is out of function.
First, you can check the version of your pymupdf, then you can find some functions that can save images in Pixmap class. This class is in fitz.py. We also update our article, you can view to fix your problem.