I have a folder containing 600 PDF files, and each PDF has 20 pages. I need to convert each page into a high-quality PNG as quickly as possible. I wrote the following script for this task: import os import multiprocessing import fitz # PyMuPDF from PIL import Image def process_pdf(pdf_path, output_folder): try: pdf_name = os.path.splitext(os.path.basename(pdf_path))[0] pdf_output_folder = os.path.join(output_folder, pdf_name) os.makedirs(pdf_output_folder, exist_ok=True) doc = fitz.open(pdf_path) for i, page in enumerate(doc): pix = page.get_pixmap(dpi=850) # Render page at high DPI img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) img_path = os.path.join(pdf_output_folder, f"page_{i+1}.png") img.save(img_path, "PNG") print(f"Processed: {pdf_path}") except Exception as e: print(f"Error processing {pdf_path}: {e}") def main(): input_folder = r"E:\Desktop\New folder (5)\New folder (4)" output_folder = r"E:\Desktop\New folder (5)\New folder (5)" pdf_files = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.lower().endswith(".pdf")] with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool: pool.starmap(process_pdf, [(pdf, output_folder) for pdf in pdf_files]) print("All PDFs processed successfully!") if __name__ == "__main__": main() Issue: This script is too slow, especially when processing a large number of PDFs. I tried the following optimizations, but they did not improve speed significantly: Reduced DPI slightly – Lowered from 1200 DPI to 850 DPI. (I also tested 600-800 DPI.) Enabled alpha=False in get_pixmap() – Reduced memory usage. Used ThreadPoolExecutor instead of multiprocessing.Pool – No major improvement. Reduced PNG compression – Set optimize=False when saving images. Converted images to grayscale – Helped slightly, but I need color images for my task. Possible Solutions I Considered: Parallel Processing of Pages Instead of Files – Instead of processing one file at a time, process each page in parallel to fully utilize CPU cores. Use ProcessPoolExecutor instead of ThreadPoolExecutor – Since rendering is CPU-intensive, multiprocessing should be better. Use JPEG Instead of PNG – JPEG is much faster to save and takes less storage, but I need high-quality images. Lower DPI to 500-600 – Provides a balance between speed and quality. Batch Write Files Instead of Saving One by One – Reduces I/O overhead. What I Need Help With: How can I significantly speed up this PDF-to-PNG conversion while maintaining high image quality? Are there better libraries or techniques I should use? Is there a way to fully utilize CPU cores efficiently? Any suggestions would be greatly appreciated! Continue reading...