Python Forum
tuple indices must be integers or slices, not str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tuple indices must be integers or slices, not str
#11
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.
Reply
#12
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.
Reply
#13
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()
Reply
#14
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-pydic...com_PIL.py
Reply
#15
yes that's a simpler . thanks. any ideas about how to reduce the noise in displayed DICOM images
Reply
#16
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.
Reply
#17
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
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.
buran write Dec-05-2023, 09:23 AM:
Spam link removed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,178 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,274 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,431 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,047 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,472 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,412 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,591 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,398 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,846 Nov-04-2020, 11:26 AM
Last Post: Aggam
  TypeError: string indices must be integers hendern 2 3,035 Oct-02-2020, 10:16 PM
Last Post: hendern

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020