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
#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


Messages In This Thread
RE: tuple indices must be integers or slices, not str - by brewer32 - Nov-02-2023, 01:20 PM

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,248 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,329 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,521 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,082 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,502 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,527 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,640 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,466 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,888 Nov-04-2020, 11:26 AM
Last Post: Aggam
  TypeError: string indices must be integers hendern 2 3,059 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