Hi,
This is a part of my code, which should open images one by one from an input directory, process them, and save in an output path.
def open_image(self):
if in_path:
image_extensions = ('.jpg', '.jpeg', '.png', '.tiff')
image_files = sorted([fn for fn in os.listdir(in_path) if fn.lower().endswith(image_extensions)])
total_images = len(image_files)
for i, image_file in enumerate(image_files):
self.image = Image.open(os.path.join(in_path, image_file))
self.show_image(image_file, i + 1, total_images)
# and at the end of the code:
root = tk.Tk()
app = Manual(root)
app.open_image()
root.mainloop()
However, no image is opened and shown on the resized screen (convas). Alternatively, when I use the below version, at least the first image is opened and shown.
def open_image(self):
folder_path = filedialog.askdirectory()
if folder_path:
image_files = sorted([f for f in os.listdir(folder_path) if f.lower().endswith('.jpg')])
total_images = len(image_files)
for i, image_file in enumerate(image_files):
self.image = Image.open(os.path.join(folder_path, image_file))
self.show_image(image_file, i + 1, total_images)
# and at the end of the code:
root = tk.Tk()
app = Manual(root)
app.open_image()
root.mainloop()
It seems challenging for me to provide a brief runnable version of the code. I tried it, but new errors arises. I don’t know about the rules of the website for sharing the complete version of the codes, and whether it is a common practice or not?
Depends on how big it is. The basic deal is that the bigger the code,
the harder it is for people to read and understand, and possibly to run
locally to test. This is why a small example of the problem is
preferable. But if you can’t make a very small example, sometime the
whole code (or a lot of it) is the only way. How bigis it?
(Also, often the task of reducing the problem to a small example
isolates the problem for you, and may solve your problem for you.)
Below is a shortened version of the code. In this version, the problem seems to be solved (both using options 1 and 2 could open and display the image). However, still there is a difference in the size of displayed image:
Based on this simplification, I could solve the problem in the original code. However, the similar problem about the image size is remained unsolved. Indeed, the first image in the list is displayed very small, but other images are shown correctly with their original dimensions.
As suggested by ChatGPT 3.5, adding “root.update()” at the end of the code, solved the problem:
...
root = tk.Tk()
root.eval('tk::PlaceWindow . center')
app = Manual(root)
root.update() # Update the window to ensure the size is final
app.open_folder() # Call open_folder() to open the folder only once
root.mainloop()