How to convert pdf to png using python

How to Convert PDF to PNG Using Python (With Optional Image Tweaking)

If you want to convert a PDF file into PNG images using Python, you’ve come to the right place! In this guide, I’ll explain the basic steps to get started. Then, for users who want more control over the images (like tweaking sharpness, brightness, and contrast), I’ll show how to do that too. Let’s dive in!

Basic Steps to Convert PDF to PNG

Step 1: Install Required Libraries

First, install the libraries we’ll use:

pip install pymupdf pillow

Step 2: Import the Libraries

import fitz  # PyMuPDF
from PIL import Image
  

Step 3: Open the PDF File

pdf_file = fitz.open("example.pdf")
  

Step 4: Convert Each Page to PNG

for page_num in range(len(pdf_file)):  # Loop through all pages
    page = pdf_file.load_page(page_num)  # Load a single page
    pix = page.get_pixmap()  # Render the page as an image
    pix.save(f"page_{page_num + 1}.png")  # Save the image as PNG
  

Step 5: Close the PDF File

pdf_file.close()
  

Full Code for Basic Conversion

import fitz  # PyMuPDF
from PIL import Image

# Open the PDF file
pdf_file = fitz.open("example.pdf")

# Loop through each page and save as PNG
for page_num in range(len(pdf_file)):
    page = pdf_file.load_page(page_num)  # Load a page
    pix = page.get_pixmap()  # Render the page as an image
    pix.save(f"page_{page_num + 1}.png")  # Save as PNG

# Close the PDF file
pdf_file.close()
  

Advanced: Tweaking Image Properties (Optional)

For users who want more control over the output, such as adjusting sharpness, brightness, and contrast, we can enhance the basic script. This involves using the ImageEnhance module from Pillow.

Step 1: Add a Function to Adjust Image Properties

from PIL import ImageEnhance

def adjust_image_properties(image, sharpness=1.0, brightness=1.0, contrast=1.0):
    enhancer = ImageEnhance.Sharpness(image)
    image = enhancer.enhance(sharpness)  # Adjust sharpness
    
    enhancer = ImageEnhance.Brightness(image)
    image = enhancer.enhance(brightness)  # Adjust brightness
    
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(contrast)  # Adjust contrast
    
    return image
  

Step 2: Prompt User for Adjustments

sharpness = float(input("Enter sharpness level (1.0 = normal): "))
brightness = float(input("Enter brightness level (1.0 = normal): "))
contrast = float(input("Enter contrast level (1.0 = normal): "))
  

Step 3: Apply Adjustments During Conversion

for page_num in range(len(pdf_file)):
    page = pdf_file.load_page(page_num)  # Load a page
    pix = page.get_pixmap()  # Render the page as an image

    # Convert pixmap to PIL image
    image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)

    # Adjust image properties
    image = adjust_image_properties(image, sharpness, brightness, contrast)

    # Save as PNG
    image.save(f"page_{page_num + 1}.png")
  

Full Code with Adjustments

import fitz  # PyMuPDF
from PIL import Image, ImageEnhance

def adjust_image_properties(image, sharpness=1.0, brightness=1.0, contrast=1.0):
    enhancer = ImageEnhance.Sharpness(image)
    image = enhancer.enhance(sharpness)
    enhancer = ImageEnhance.Brightness(image)
    image = enhancer.enhance(brightness)
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(contrast)
    return image

pdf_file = fitz.open("example.pdf")
sharpness = float(input("Enter sharpness level (1.0 = normal): "))
brightness = float(input("Enter brightness level (1.0 = normal): "))
contrast = float(input("Enter contrast level (1.0 = normal): "))

for page_num in range(len(pdf_file)):
    page = pdf_file.load_page(page_num)
    pix = page.get_pixmap()
    image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
    image = adjust_image_properties(image, sharpness, brightness, contrast)
    image.save(f"page_{page_num + 1}.png")

pdf_file.close()
  

Now you can easily convert PDFs to PNG and customize the output to your liking. Happy coding! 😊

No comments:

Post a Comment