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
#1
I am trying to make a Tkinter GUI with PyMuPDF. however I am geting the error
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()
Reply


Messages In This Thread
tuple indices must be integers or slices, not str - by cybertooth - Feb-22-2023, 06:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers deneme2 2 625 Feb-14-2025, 12:23 AM
Last Post: deneme2
  TypeError: string indices must be integers, not 'str' LEMA 2 2,369 Jun-12-2024, 09:32 PM
Last Post: LEMA
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 2,581 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 3,098 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 9,037 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 5,822 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 2,844 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 5,810 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 4,429 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 9,986 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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