|
| 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