Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, ctypes
- from datetime import datetime
- from PIL import Image, ImageTk, ImageFont, ImageDraw
- import tkinter as tk
- from tkinter import ttk
- #================ADD-IMAGE-ICON=================
- import sys
- def resource_path(relative_path):
- """ Get the absolute path to the resource, works for PyInstaller. """
- if getattr(sys, '_MEIPASS', False):
- return os.path.join(sys._MEIPASS, relative_path)
- return os.path.join(os.path.abspath("."), relative_path)
- # Use this function to load files:
- #splash_image = resource_path("splash-1.png")
- icon_path = resource_path("W.ico")
- #================ADD-IMAGE-ICON=================
- # === Paths ===
- # Detect if running from PyInstaller EXE
- if hasattr(sys, '_MEIPASS'):
- base_path = sys._MEIPASS
- else:
- base_path = os.path.dirname(os.path.abspath(__file__))
- wall_dir = os.path.join(base_path, "WALLPAPERS")
- logo_dir = os.path.join(base_path, "LOGO")
- font_dir = os.path.join(base_path, "FONT")
- save_path = os.path.join(os.path.expanduser("~"), "Pictures", "WALL-PICTURE", "temp_wallpaper.jpg")
- # Ensure output directory exists
- os.makedirs(os.path.dirname(save_path), exist_ok=True)
- # === Image Functions ===
- def add_margin(img, top, left, bottom, right, color=(0, 0, 0)):
- new_img = Image.new(img.mode, (img.width + left + right, img.height + top + bottom), color)
- new_img.paste(img, (left, top))
- return new_img
- def adjust_resolution(path):
- img = Image.open(path)
- if img.width / 1920 > img.height / 1080:
- pad = int((img.width * 1080 / 1920 - img.height) / 2)
- img = add_margin(img, pad, 0, pad, 0)
- else:
- pad = int((img.height * 1920 / 1080 - img.width) / 2)
- img = add_margin(img, 0, pad, 0, pad)
- return img.resize((1920, 1080))
- def update_preview(event=None):
- try:
- filename = wall_listbox.get(tk.ACTIVE)
- path = os.path.join(wall_dir, filename)
- base_img = adjust_resolution(path)
- draw = ImageDraw.Draw(base_img)
- # Font
- font_path = os.path.join(font_dir, font_var.get())
- try:
- font = ImageFont.truetype(font_path, 28)
- except:
- font = ImageFont.load_default()
- # Logo
- logo_file = logo_var.get()
- if logo_file != "None":
- logo_path = os.path.join(logo_dir, logo_file)
- if os.path.exists(logo_path):
- logo = Image.open(logo_path).convert("RGBA")
- logo.thumbnail((int(logo_w.get()), int(logo_h.get())), Image.Resampling.LANCZOS)
- lx = (base_img.width - logo.width) // 2
- ly = (base_img.height - logo.height) // 2
- base_img.paste(logo, (lx, ly), logo)
- # Text overlay
- if show_time_var.get():
- time_text = datetime.now().strftime("%H:%M:%S - %d %b %Y")
- name_text = name_entry.get()
- time_bbox = draw.textbbox((0, 0), time_text, font=font)
- time_x = 1660 - (time_bbox[2] - time_bbox[0])
- time_y = 950
- draw.text((time_x, time_y), time_text, font=font, fill=(255, 255, 255))
- name_bbox = draw.textbbox((0, 0), name_text, font=font)
- name_x = 1660 - (name_bbox[2] - name_bbox[0])
- name_y = time_y + (time_bbox[3] - time_bbox[1]) + 8
- draw.text((name_x, name_y), name_text, font=font, fill=(255, 255, 255))
- draw.line((name_x, name_y + 30, name_x + (name_bbox[2] - name_bbox[0]), name_y + 30), fill="white", width=2)
- base_img.save(save_path)
- preview_img = base_img.resize((700, 400))
- preview_photo = ImageTk.PhotoImage(preview_img)
- preview_label.config(image=preview_photo)
- preview_label.image = preview_photo
- except Exception as e:
- print(f"❌ Preview update failed: {e}")
- def set_wallpaper():
- ctypes.windll.user32.SystemParametersInfoW(20, 0, save_path, 3)
- # === GUI Setup ===
- root = tk.Tk()
- root.title("Wallpaper Customizer - Najeeb Shah Khan")
- root.geometry("1080x700")
- #root.configure(bg="#181818")
- root.configure(bg="#2c3e50")
- root.iconbitmap(icon_path)
- # === Left: Wallpaper list with scrollbar ===
- left_frame = tk.Frame(root, bg="#222")
- left_frame.pack(side="left", fill="y", padx=10, pady=10)
- tk.Label(left_frame, text="Wallpapers", bg="#222", fg="white", font=("Segoe UI", 12)).pack(pady=5)
- scrollbar = tk.Scrollbar(left_frame, orient="vertical")
- wall_listbox = tk.Listbox(left_frame, height=28, yscrollcommand=scrollbar.set,
- bg="black", fg="white", selectbackground="#444")
- scrollbar.config(command=wall_listbox.yview)
- wall_listbox.pack(side="left", fill="y")
- scrollbar.pack(side="right", fill="y")
- wallpapers = [f for f in os.listdir(wall_dir) if f.lower().endswith(('.jpg', '.png'))]
- for w in wallpapers:
- wall_listbox.insert(tk.END, w)
- wall_listbox.bind("<<ListboxSelect>>", update_preview)
- # === Right: Controls & Preview ===
- right_frame = tk.Frame(root, bg="#181818")
- right_frame.pack(side="right", fill="both", expand=True, padx=10, pady=10)
- # === Top Row Controls ===
- top_row = tk.Frame(right_frame, bg="#181818")
- top_row.pack(pady=5)
- tk.Label(top_row, text="Font:", bg="#181818", fg="white").grid(row=0, column=0, sticky="e", padx=3)
- font_files = [f for f in os.listdir(font_dir) if f.endswith(".ttf")]
- font_var = tk.StringVar(value=font_files[0])
- ttk.Combobox(top_row, textvariable=font_var, values=font_files, width=20).grid(row=0, column=1, padx=5)
- tk.Label(top_row, text="Logo:", bg="#181818", fg="white").grid(row=0, column=2, sticky="e", padx=3)
- logo_files = ["None"] + [f for f in os.listdir(logo_dir) if f.lower().endswith((".png", ".jpg"))]
- logo_var = tk.StringVar(value=logo_files[0])
- ttk.Combobox(top_row, textvariable=logo_var, values=logo_files, width=20).grid(row=0, column=3, padx=5)
- tk.Label(top_row, text="Logo Size (W x H):", bg="#181818", fg="white").grid(row=0, column=4, padx=(10, 3))
- logo_w = tk.Entry(top_row, width=5)
- logo_w.insert(0, "400")
- logo_h = tk.Entry(top_row, width=5)
- logo_h.insert(0, "400")
- logo_w.grid(row=0, column=5)
- logo_h.grid(row=0, column=6)
- # === Second Row Controls ===
- second_row = tk.Frame(right_frame, bg="#181818")
- second_row.pack(pady=5)
- tk.Label(second_row, text="Custom Name:", bg="#181818", fg="white").grid(row=0, column=0, sticky="e", padx=3)
- name_entry = tk.Entry(second_row, width=40)
- name_entry.insert(0, "By - Najeeb Shah Khan")
- name_entry.grid(row=0, column=1, columnspan=2, padx=5)
- show_time_var = tk.BooleanVar(value=True)
- tk.Checkbutton(second_row, text="Show Time & Date", variable=show_time_var,
- bg="#181818", fg="white", selectcolor="#333").grid(row=0, column=3, padx=10)
- # === Third Row: Buttons ===
- btn_row = tk.Frame(right_frame, bg="#181818")
- btn_row.pack(pady=10)
- tk.Button(btn_row, text="Update Preview", command=update_preview, bg="#333", fg="white", width=20).grid(row=0, column=0, padx=10)
- tk.Button(btn_row, text="Set As Wallpaper", command=set_wallpaper, bg="#007acc", fg="white", width=20).grid(row=0, column=1, padx=10)
- # === Preview Label ===
- preview_label = tk.Label(right_frame, bg="black", width=800, height=500)
- preview_label.pack(pady=10)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement