Ask Copilot to create a Python/Tkinter Image Slideshow

It's my first try asking Copilot to create a Python/Tkinter Image Slideshow, test on Windows 11.



I just enter the prompt in Copilot and submit. I even typo the word of "create".

Please creaate a Python/tkinter program to display all jpg images in current folder in slideshow.

coPython_slideshow.py, the Python code created. I just change the code of geometry() and resize() to match with my images.
from tkinter import *
from PIL import ImageTk, Image
import os

# Get a list of all .jpg files in the current folder
image_files = [filename for filename in os.listdir() if filename.lower().endswith(".jpg")]

# Initialize the Tkinter window
root = Tk()
root.title("Image Slideshow")
root.geometry("800x800")  # Set your desired window size

# Load the images and create PhotoImage objects
image_list = []
for filename in image_files:
    img = Image.open(filename).resize((400, 400))  # Resize the images as needed
    image_list.append(ImageTk.PhotoImage(img))

# Create a label to display the images
image_label = Label(root, image=image_list[0])
image_label.pack()

# Function to change the displayed image
def change_image(counter):
    image_label.config(image=image_list[counter])

# Initialize the counter
counter = 0

# Function to handle button click (change to next image)
def next_image():
    global counter
    counter = (counter + 1) % len(image_list)
    change_image(counter)

# Create a button to change images
next_button = Button(root, text="Next", command=next_image)
next_button.pack()

# Run the main event loop
root.mainloop()


coPython_slideshow_auto.py, it's another version to auto-play next after two seconds.
import os
import tkinter as tk
from PIL import Image, ImageTk

class ImageSlideshow:
    def __init__(self, root, folder):
        self.root = root
        self.root.title("Image Slideshow")
        self.folder = folder
        self.images = [img for img in os.listdir(folder) if img.endswith(".jpg")]
        self.index = 0

        self.label = tk.Label(root)
        self.label.pack()

        self.show_image()
        self.root.after(2000, self.next_image)  # Change image every 2 seconds

    def show_image(self):
        image_path = os.path.join(self.folder, self.images[self.index])
        image = Image.open(image_path)
        image = image.resize((1024, 1024), Image.LANCZOS)  # Use Image.LANCZOS instead of Image.ANTIALIAS
        photo = ImageTk.PhotoImage(image)
        self.label.config(image=photo)
        self.label.image = photo

    def next_image(self):
        self.index = (self.index + 1) % len(self.images)
        self.show_image()
        self.root.after(2000, self.next_image)

if __name__ == "__main__":
    root = tk.Tk()
    folder = "."  # Current folder
    slideshow = ImageSlideshow(root, folder)
    root.mainloop()



PIL, or Pillow, is needed, read the next post Install Pillow(PIL) for Python in Windows 11/Thonny.



It's another try asking Copilot to create Python code, to plot Descartes’s Love formula, also known as the “heart curve”.



import matplotlib.pyplot as plt
import numpy as np

# Create a figure
fig = plt.figure(edgecolor=(0, 0, 0), linewidth=10)
fig.suptitle('Descartes\'s Love Formula', fontsize=14, fontweight='bold')

# Drawing the heart curve
theta = np.linspace(0, 2 * np.pi, 100)
x = 16 * np.sin(theta) ** 3
y = 13 * np.cos(theta) - 5 * np.cos(2 * theta) - 2 * np.cos(3 * theta) - np.cos(4 * theta)

# Add the heart curve to the plot
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y, 'r')

# Show the plot
plt.axis('off')  # Hide axes
plt.show()



matplotlib and numpy modules are needed in the code.

To install in Thonny:
Click on Tools > Manager packages...
Search and install matplotlib.
It will install numpy also.

To install matplotlib (also numpy) in Terminal, enter the command:
pip install matplotlib

Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator