Feb-22-2023, 06:18 PM
I am trying to make a Tkinter GUI with PyMuPDF. however I am geting the error
can some one help fix this.
Error:Traceback (most recent call last):
File "C:\Users\INDIAN\Desktop\python exercises\pygametooth\pdf2jpg2.py", line 21, in <module>
image_ext = img['str']
TypeError: tuple indices must be integers or slices, not str
.can some one help fix this.
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 doc = fitz.open('G:\\Patient OPG\\Arshita Nagpal\\CBCT 48,38\\Axials.pdf') # iterate over pages for page_index in range(doc.page_count): # get the page page = doc[page_index] # iterate over images for image_index, img in enumerate(page.get_images()): # get the image extension image_ext = img['str'] # get the pixmap pixmap = page.get_pixmap(image_index) # get the image data image_data = pixmap.tobytes() # save the image to the temporary directory image_path = os.path.join(temp_dir.name, f"image{page_index+1}_{image_index}.{image_ext}") pixmap.save(image_path) ## with open(image_path, 'wb') as f: ## f.write(image_data) # create a tkinter window root = tk.Tk() # iterate over extracted images and display them in the tkinter window for image_file in os.listdir(temp_dir.name): image_path = os.path.join(temp_dir.name, image_file) # open image with PIL image = Image.open(image_path) # convert image to tkinter-compatible format photo = ImageTk.PhotoImage(image) # create a label with the image and add it to the tkinter window label = tk.Label(root, image=photo) label.pack() # start the tkinter mainloop root.mainloop() # delete the temporary directory temp_dir.cleanup()