Feb-28-2023, 05:04 PM
(This post was last modified: Feb-28-2023, 07:51 PM by deanhystad.)
https://docs.python.org/3/library/tempfile.html
In other words, as soon as you leave the context created by this:
There is no need to write the image data to files. Create the images and keep them in a list. If you want to write files to a temporary directory, you could wrap you program in the temp_folder context.
One last thing, this does not belong inside select_pdf_file()
Quote:class tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)
This class securely creates a temporary directory using the same rules as mkdtemp(). The resulting object can be used as a context manager (see Examples). On completion of the context or destruction of the temporary directory object, the newly created temporary directory and all its contents are removed from the filesystem.
In other words, as soon as you leave the context created by this:
with tempfile.TemporaryDirectory() as temp_folder:The temporary directory is deleted.
There is no need to write the image data to files. Create the images and keep them in a list. If you want to write files to a temporary directory, you could wrap you program in the temp_folder context.
with tempfile.TemporaryDirectory() as temp_folder: window = function_that_creates_your_main_window() window.mainloop()Or you could make temp_folder a global variable
def select_pdf_file(): global temp_folder, current_index file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")]) if file_path: pdf_file = fitz.open(file_path) current_index = 0 temp_folder = tempfile.TemporaryDirectory() image_index = 0 for page in pdf_file: for xref, *_ in page.get_images(): # extract the image info = pdf_file.extract_image(xref) image = PIL.Image.open(io.BytesIO(info ["image"])) #<- Why not put this in a list?? image_file = os.path.join(temp_folder.name, f"image{image_index}.{info ["ext"]}") image.save(os.path.join(temp_folder.name, image_file)) show_image(temp_folder.name, image_file) image_index += 1Making temp_folder a global variable means the temporary folder stays alive until you reassign the temp_folder variable (no reference->delete folder), exit the program, or call the cleanup() function (temp_folder.cleanup()).
One last thing, this does not belong inside select_pdf_file()
def on_key_press(event): global current_index if event.keysym == "Right": current_index = (current_index + 1) % len(image_files) pil_image = PIL.Image.open(os.path.join(temp_folder, image_files[current_index])) tk_image = ImageTk.PhotoImage(pil_image) image_label.config(image=tk_image) elif event.keysym == "Left": current_index = (current_index - 1) % len(image_files) pil_image = PIL.Image.open(os.path.join(temp_folder, image_files[current_index])) tk_image = ImageTk.PhotoImage(pil_image) image_label.config(image=tk_image) root.bind("<Key>", on_key_press)You can embed a function inside another function, but you should only do this with "helper" functions, or if you are making a closure. I don't think either applies in this case.