It looks like you have a Python code snippet for extracting images from a PDF file using the fitz library (PyMuPDF) and displaying them using tkinter and PIL. However, it appears that the code is incomplete. To help you fix it, I'll provide a more complete version of the code, along with comments for clarity:
python
Copy code
python
Copy code
import fitz import tempfile import os import tkinter as tk from PIL import Image, ImageTk # Create a temporary directory to store extracted images temp_dir = tempfile.TemporaryDirectory() # Open PDF file pdf_file_path = 'G:\\Patient OPG\\Arshita Nagpal\\CBCT 48,38\\Axials.pdf' doc = fitz.open(pdf_file_path) # Initialize a tkinter window root = tk.Tk() root.title("PDF Image Extractor") # Create a canvas for displaying images canvas = tk.Canvas(root, width=800, height=600) canvas.pack() # Function to display images def display_image(image_path): img = Image.open(image_path) img = ImageTk.PhotoImage(img) canvas.create_image(0, 0, anchor=tk.NW, image=img) canvas.image = img # Keep a reference to prevent the image from being garbage collected # Iterate over pages for page_index in range(len(doc)): page = doc[page_index] # Iterate over images on the page for image_index, img in enumerate(page.get_images()): xref = img[0] base_image = doc.extract_image(xref) image_data = base_image["image"] # Determine the image file extension image_ext = base_image["ext"] # Save the image to a temporary file image_path = os.path.join(temp_dir.name, f"page{page_index}_image{image_index}.{image_ext}") with open(image_path, "wb") as image_file: image_file.write(image_data) # Display the extracted image display_image(image_path) # Start the tkinter main loop to display the images root.mainloop() # Close the PDF document and clean up the temporary directory doc.close() temp_dir.cleanup()This code will open the PDF file, extract images from each page, save them to a temporary directory, and display them in a tkinter window. Make sure you have the required libraries (PyMuPDF, tkinter, and Pillow) installed in your Python environment to run this code
buran write Dec-05-2023, 09:24 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
buran write Dec-05-2023, 09:23 AM:
Spam link removed
Spam link removed