![]() |
tuple indices must be integers or slices, not str - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: tuple indices must be integers or slices, not str (/thread-39470.html) Pages:
1
2
|
RE: tuple indices must be integers or slices, not str - cybertooth - Mar-06-2023 Thanks a lot for explaining things . I was not sure of saving the images in a list would work hence created a temporary folder to save the extracted images but i see that creating a list is better as it is more efficient . The folder method was i guess the main reason i was getting a lag in loading the images and secondly it was not working every time though the temporary folder was being created.As for the use of grid yes I might not be understanding it clearly , can you suggest a good tutorial to understand grid and pack methods. I would also like to know what factors determine how the images are extracted from the pdf file. I have seen that in some cases all the images are individually extracted while in some cases they are extracted as a group. RE: tuple indices must be integers or slices, not str - deanhystad - Mar-06-2023 You created your temporary directory in the wrong place. You should have created the directory in the select_pdf_files() function. def select_pdf_file(): global images, temp_folder # open file dialog to choose a PDF file if file_path := filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")]): if file_path: temp_dir = tempfile.TemporaryDirectory() # <-- Create temporary directory here with fitz.open(file_path) as pdf_file: for page_index in range(len(pdf_file)):For the first PDF, this will work exactly like your program. The second time you open a PDF, re-assigning temp_dir deletes the previous temporary directiory and you have a new, empty, temporary directory to work with. After opening three PDF files the temporary directory would contain images from the last PDF file. The way your program was written, opening a PDF added files to the temporary directory. After opening three PDF files, the temporary directory would contain images from three different PDF files. RE: tuple indices must be integers or slices, not str - cybertooth - Mar-10-2023 Thanks. I have added a DICOM option to the code but I am getting a lot of noise in the images. When I tried the code with matpoltlip and plt.show the images were quite sharp but when I add the function to show in tkinter canvas the images are quite grainy. what can be the cause. Secondaly there is a slight lag in loading the DICOM images. import fitz import io import tkinter as tk from tkinter import filedialog from PIL import Image, ImageTk import pydicom import os import matplotlib.pyplot as plt from pathlib import Path def select_pdf_file(): """Load images from a PDF file""" global images if file := filedialog.askopenfilename(filetypes=[("PDF", "*.pdf")]): images = [] with fitz.open(file) as doc: for page in doc: for xref, *_ in page.get_images(): image = doc.extract_image(xref) images.append(Image.open(io.BytesIO(image["image"]))) show_image(0) def select_folder(): """Load images from a folder""" global images if folder := filedialog.askdirectory(): images = [] for file in Path(folder).iterdir(): if file.suffix.lower() in (".png", ".bmp", ".jpg", ".bmp"): images.append(Image.open(file)) show_image(0) def select_dicom_folder(): """Load images from a DICOM folder""" global images if folder := filedialog.askdirectory(): images = [] for file in Path(folder).iterdir(): if file.suffix.lower() == ".dcm": d_img = pydicom.dcmread(str(file)) pixel_array = d_img.pixel_array rows=d_img.Rows cols=d_img.Columns rescale_slope = d_img.RescaleSlope rescale_intercept = d_img.RescaleIntercept pixel_array = pixel_array * rescale_slope + rescale_intercept images.append(Image.fromarray(pixel_array)) show_image(0) def show_image(index): """Display selected image""" global img_index canvas.delete("all") size = (canvas.winfo_width(), canvas.winfo_height()) if images: img_index = index % len(images) image = images[img_index].copy() # Keep original image image.thumbnail(size) image = ImageTk.PhotoImage(image) canvas.create_image( size[0] / 2, size[1] / 2, anchor=tk.CENTER, image=image ) canvas.image = image else: canvas.create_text( size[0] / 2, size[1] / 2, anchor=tk.CENTER, text="No Images", fill="white", font=('Helvetica 15 bold') ) def rotate_image(): """Rotate selected image 90 degrees""" if images: images[img_index] = images[img_index].rotate(-90, expand=True) show_image(img_index) images = [] img_index = 0 root = tk.Tk() canvas = tk.Canvas(root, bg="black") canvas.bind("<Configure>", lambda event: show_image(img_index)) canvas.pack(padx=10, pady=10, side=tk.TOP, expand=True, fill=tk.BOTH) bbar = tk.Frame(root) bbar.pack(side=tk.TOP, fill=tk.X, padx=10, pady=(0, 10)) button = tk.Button(bbar, text="<<", command=lambda: show_image(img_index-1)) button.pack(side=tk.LEFT) button = tk.Button(bbar, text="Select PDF", command=select_pdf_file) button.pack(side=tk.LEFT, expand=True, fill=tk.X) button = tk.Button(bbar, text="Select Folder", command=select_folder) button.pack(side=tk.LEFT, expand=True, fill=tk.X) button = tk.Button(bbar, text="Select DICOM", command=select_dicom_folder) button.pack(side=tk.LEFT, expand=True, fill=tk.X) button = tk.Button(bbar, text="Rotate Image", command=rotate_image) button.pack(side=tk.LEFT, expand=True, fill=tk.X) button = tk.Button(bbar, text=">>", command=lambda: show_image(img_index+1)) button.pack(side=tk.LEFT) root.mainloop() RE: tuple indices must be integers or slices, not str - deanhystad - Mar-10-2023 Do you have a question? I don't see any reason for "select_dicom_folder()". You don't have a "select_jpeg_folder()" or a "select_png_folder()". Just add "dcm" to the list of image file extensions. def select_folder(): """Load images from a folder""" global images if folder := filedialog.askdirectory(): images = [] for file in Path(folder).iterdir(): if file.suffix.lower() in (".png", ".bmp", ".jpg", ".bmp"): images.append(Image.open(file)) elif file.suffix.lower() == ".dcm": images.append(extra_processing_required(file)) show_image(0)The pydicom documentation talks about what you need to do to comvert a dicom image to a PIL image. https://github.com/pydicom/contrib-pydicom/blob/master/viewers/pydicom_PIL.py RE: tuple indices must be integers or slices, not str - cybertooth - Mar-11-2023 yes that's a simpler . thanks. any ideas about how to reduce the noise in displayed DICOM images RE: tuple indices must be integers or slices, not str - deanhystad - Mar-12-2023 Look at the link in my post. Are you converting the dicom data to a PIL image correctly? If that doesn't fix things, try a different interpolation mode for the thumbnail() call. RE: tuple indices must be integers or slices, not str - brewer32 - Nov-02-2023 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 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 |