Oct-07-2023, 07:46 AM
Hi everyone. I am trying to isolate images from a pdf file. The problem I am facing is that all the images are not isolated, out of the 3 images the code isolates them as 2 images . 1&2 as one image , and 3 as 2nd image. Can some one help me solve this issue. Please find the pdf file as attachment.
doc3.pdf (Size: 201.99 KB / Downloads: 250)
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 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')
)
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=">>", command=lambda: show_image(img_index+1))
button.pack(side=tk.LEFT)
root.mainloop()
doc3.pdf (Size: 201.99 KB / Downloads: 250)
