Reputation: 281
How do I convert a pdf to an image (say a .png or .tiff file)?
Is there a way to do this without purchasing a third-party component?
Upvotes: 0
Views: 338
Reputation: 12420
You can use next solution with fitz
and PIL
python libraries
import io
import fitz
from PIL import Image
def process_pdf(self, io_bytes: io.BytesIO) -> None:
try:
pdf = fitz.open(stream=io_bytes, filetype="pdf")
except fitz.fitz.FileDataError:
msg = "Broken pdf for file"
raise RuntimeError(msg)
for page_num in range(0, pdf.page_count):
page = pdf.load_page(page_num)
logging.info("yield page %s/%s", page_num, pdf.page_count)
dpi = min(int(300 * 1080 / max(page.mediabox_size.x, page.mediabox_size.y)), 300)
# A4 is ~12 inches, # 300 dpi => 1mm
pix = page.get_pixmap(dpi=dpi)
with io.BytesIO() as page_io_bytes:
page_io_bytes.write(pix.pil_tobytes(format="webp", optimize=True))
with Image.open(page_io_bytes) as image:
image.save(f"page_{page_num + 1}.png", "png")
Upvotes: -1
Reputation: 7025
I assume that you want to manually convert a PDF. Yo have two ways of doing it:
Online:
PRO: very very easy.
CONTRA: if your document is somewhat private your are giving it away.
I used last week Neevia and worked perfectly: http://convert.neevia.com/pdfconvert/
In your machine
PRO: More Secure.
CONTRA: You need to download a program and have it setup.
Here you can find 3 ways of converting it online and another three from your pc. http://www.makeuseof.com/tag/6-ways-to-convert-a-pdf-file-to-a-jpg-image/
Upvotes: 0
Reputation: 12896
Take a look at Imagemagic you can call these programs from roughly any programing language. And it is available on Unix, Windows, Mac, ...
Upvotes: 2