0% found this document useful (0 votes)
9 views4 pages

Importing The Required Libraries111

Uploaded by

2254810300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Importing The Required Libraries111

Uploaded by

2254810300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Importing the required libraries

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageTk, ImageSequence

# Creating root window


root = tk.Tk()

# Setting title
root.title("ProjectGurukul - GIF Creator")

# Setting window size


root.geometry("600x650")
root.resizable(width=False, height=False)
root.configure(background="#D7BDE2")

# Function to select input files (images)


def input_file():
global input_file_names
input_file_names = filedialog.askopenfilenames(
initialdir="/",
title="Select image files",
filetypes=(("Image files", "*.png;*.jpg;*.jpeg"), ("all files", "*.*"))
)
input_label.configure(text="Selected Files: " + str(len(input_file_names)) + " images")

# Function to select output file


def output_file():
global output_file_name
output_file_name = filedialog.asksaveasfilename(
initialdir="/",
title="Select file",
defaultextension=".gif",
filetypes=(("GIF files", "*.gif"), ("all files", "*.*"))
)
output_label.configure(text="Save As: " + output_file_name.split("/")[-1])

# Function to animate and display GIF


def animate_gif(gif_image):
# Extract each frame and resize to fit the display area
frames = [img.copy() for img in ImageSequence.Iterator(gif_image)]

# Get the GIF duration per frame (if available)


try:
frame_duration = gif_image.info['duration']
except KeyError:
frame_duration = 100 # Default to 100ms if duration is not provided

def update_frame(frame_idx=0):
frame = frames[frame_idx].resize((300, 300), Image.ANTIALIAS)
frame_photo = ImageTk.PhotoImage(frame)

gif_label.config(image=frame_photo)
gif_label.image = frame_photo # Keep a reference to avoid garbage collection

frame_idx = (frame_idx + 1) % len(frames)


root.after(frame_duration, update_frame, frame_idx) # Change frame based on GIF duration

update_frame()

# Function to create GIF from images


def create_gif():
# Checking for errors
if not input_file_names or not output_file_name:
messagebox.showerror("Error", "Input files and output file not selected")
return

# Load images using Pillow


images = []
for img_file in input_file_names:
img = Image.open(img_file)
images.append(img)

# Check if images are loaded


if not images:
messagebox.showerror("Error", "No valid images selected")
return

# Save as GIF
images[0].save(output_file_name, save_all=True, append_images=images[1:], loop=0, duration=500)

# Show success message


messagebox.showinfo("Success", "GIF created successfully")
# Display the created GIF
gif_image = Image.open(output_file_name)
animate_gif(gif_image)

# Creating GUI elements


title_label = tk.Label(root)
title_label.configure(
background="#095aaa", foreground="#ABEBC6", font="Arial 18 bold", justify="center",
text="ProjectGurukul - GIF Creator"
)
title_label.place(x=0, y=0, width=600, height=45)

input_label = tk.Label(root)
input_label.configure(background="#D7BDE2", foreground="#333333", font="Arial 14", justify="center",
text="Input Files: ")
input_label.place(x=20, y=70, width=328, height=40)

input_button = tk.Button(root)
input_button.configure(font="Arial 14", justify="center", text="Choose Images", command=input_file)
input_button.place(x=380, y=70, width=160, height=40)

output_label = tk.Label(root)
output_label.configure(background="#D7BDE2", foreground="#333333", font="Arial 14",
justify="center", text="Save As: ")
output_label.place(x=20, y=150, width=328, height=40)

output_button = tk.Button(root)
output_button.configure(font="Arial 14", justify="center", text="Choose Path", command=output_file)
output_button.place(x=380, y=150, width=160, height=40)

create_button = tk.Button(root)
create_button.configure(font="Arial 20 bold", justify="center", text="Create GIF", command=create_gif)
create_button.place(x=200, y=230, width=205, height=52)

# Label to display the animated GIF, sized for 300x300 images


gif_label = tk.Label(root, width=300, height=300)
gif_label.place(x=150, y=320, width=300, height=300)

# Running the root window


root.mainloop()

You might also like