Skip to content

Instantly share code, notes, and snippets.

@Pablito2020
Created March 3, 2023 18:26
Show Gist options
  • Save Pablito2020/74f1770618f1e63df12fa4b6c3848fcb to your computer and use it in GitHub Desktop.
Save Pablito2020/74f1770618f1e63df12fa4b6c3848fcb to your computer and use it in GitHub Desktop.
import os
import random
import shutil
import tkinter as tk
from PIL import Image, ImageTk
# Define the paths to the folders containing the images
source_folder = "new_images"
target_folder = "old_images"
# Get a list of all the images in the source folder
image_files = os.listdir(source_folder)
# Define a function to select a random image from the list
def get_random_image():
random_image = random.choice(image_files)
return random_image
# Define a function to move the selected image to the target folder
def move_image(image_file):
shutil.move(os.path.join(source_folder, image_file), os.path.join(target_folder, image_file))
# Define a function to display the selected image on the screen
def display_image(image_file):
# Load the image using the PIL library
image = Image.open(os.path.join(source_folder, image_file))
# Resize the image to fit the screen
max_width = 800
max_height = 600
if image.width > max_width or image.height > max_height:
image.thumbnail((max_width, max_height), Image.ANTIALIAS)
# Create a Tkinter window and display the image
window = tk.Tk()
window.title(image_file)
photo = ImageTk.PhotoImage(image)
label = tk.Label(window, image=photo)
label.pack()
# Define a function to move the image when the user clicks on it
def on_click(event):
move_image(image_file)
window.destroy()
main()
# Bind the click event to the image label
label.bind("<Button-1>", on_click)
# Start the Tkinter event loop
window.mainloop()
# Define a main function to run the script
def main():
# Get a random image file from the source folder
image_file = get_random_image()
# Display the image on the screen and wait for the user to click it
display_image(image_file)
# Call the main function to start the script
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment