Skip to content

Commit ad38129

Browse files
authored
Create visual_base64.py
This commit introduces the initial creation of the visual_base64.py script. The script is designed to handle drag-and-drop events for image files and convert them to Base64 strings. Key features of this script include: - Importing necessary libraries for GUI and drag-and-drop functionality (TkinterDnD2). - Defining functions to handle file selection and image conversion. - Providing a user-friendly interface for drag-and-drop operations. - Ensuring compatibility with various image file formats. Future improvements may include: - Adding more detailed error handling and user feedback. - Enhancing the user interface for better usability. - Incorporating additional image processing features as needed. This script aims to streamline the process of converting image files to Base64 strings, making it easier to integrate with other applications or systems.
1 parent ed25ac3 commit ad38129

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

visual_base64.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import os
2+
import tkinter as tk
3+
from tkinterdnd2 import TkinterDnD, DND_FILES
4+
from tkinter import filedialog, messagebox
5+
import base64
6+
7+
def on_drop(event):
8+
"""
9+
Handle the drag-and-drop event.
10+
"""
11+
file_path = event.data
12+
if file_path:
13+
file_path = file_path.replace("{", "").replace("}", "") # Clean up the file path
14+
select_and_convert_image(file_path)
15+
16+
def select_and_convert_image(file_path):
17+
"""
18+
Converts the selected image file to a Base64 string.
19+
"""
20+
try:
21+
if not file_path:
22+
return # No file path provided
23+
24+
# Convert the selected image to Base64
25+
with open(file_path, "rb") as image_file:
26+
image_data = image_file.read()
27+
base64_encoded = base64.b64encode(image_data).decode('utf-8')
28+
29+
# Display the Base64 string in the text widget
30+
output_text.delete("1.0", tk.END) # Clear existing text
31+
output_text.insert(tk.END, base64_encoded)
32+
33+
# Notify the user
34+
messagebox.showinfo("Success", "Image successfully converted to Base64!")
35+
except Exception as e:
36+
messagebox.showerror("Error", f"An error occurred: {e}")
37+
38+
def copy_to_clipboard():
39+
"""
40+
Copy the Base64 string to the clipboard.
41+
"""
42+
base64_text = output_text.get("1.0", tk.END).strip()
43+
if base64_text:
44+
app.clipboard_clear()
45+
app.clipboard_append(base64_text)
46+
app.update() # Ensure the clipboard is updated
47+
messagebox.showinfo("Copied", "Base64 string copied to clipboard!")
48+
49+
# Create the main application window
50+
app = TkinterDnD.Tk()
51+
app.title("Image to Base64 Converter")
52+
app.geometry("700x600")
53+
app.config(bg="#F7F7F7")
54+
55+
# Title label
56+
title_label = tk.Label(app, text="Image to Base64 Converter", font=("Arial", 18, "bold"), fg="#333333", bg="#F7F7F7")
57+
title_label.pack(pady=20)
58+
59+
# Add a drag-and-drop area
60+
drop_area = tk.Label(app, text="Drag and drop an image file here", font=("Arial", 14), fg="#555555", bg="#DDDDDD", width=40, height=10, relief="sunken")
61+
drop_area.pack(pady=20)
62+
drop_area.drop_target_register(DND_FILES)
63+
drop_area.dnd_bind('<<Drop>>', on_drop)
64+
65+
# Add a text widget to display the Base64 string
66+
output_text = tk.Text(app, wrap="word", font=("Arial", 12), height=10, bg="#FFFFFF", fg="#333333", padx=10, pady=10, bd=2, relief="sunken")
67+
output_text.pack(expand=True, fill="both", padx=30, pady=10)
68+
69+
# Add a scrollbar for large outputs
70+
scrollbar = tk.Scrollbar(app, command=output_text.yview)
71+
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
72+
output_text.config(yscrollcommand=scrollbar.set)
73+
74+
# Add a button to copy the Base64 string to the clipboard
75+
copy_button = tk.Button(
76+
app,
77+
text="Copy to Clipboard",
78+
command=copy_to_clipboard,
79+
font=("Arial", 14),
80+
bg="#2196F3", # Blue background
81+
fg="white",
82+
relief="flat",
83+
width=20,
84+
height=2
85+
)
86+
copy_button.pack(pady=20)
87+
88+
# Run the application
89+
try:
90+
app.mainloop()
91+
except Exception as e:
92+
messagebox.showerror("Error", f"An error occurred: {e}")
93+
app.destroy()

0 commit comments

Comments
 (0)