Advertisement
Najeebsk

WALLPAPER-CHANGER.pyw

May 24th, 2025 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.15 KB | None | 0 0
  1. import os, ctypes
  2. from datetime import datetime
  3. from PIL import Image, ImageTk, ImageFont, ImageDraw
  4. import tkinter as tk
  5. from tkinter import ttk
  6. #================ADD-IMAGE-ICON=================
  7. import sys
  8.  
  9. def resource_path(relative_path):
  10.     """ Get the absolute path to the resource, works for PyInstaller. """
  11.     if getattr(sys, '_MEIPASS', False):
  12.         return os.path.join(sys._MEIPASS, relative_path)
  13.     return os.path.join(os.path.abspath("."), relative_path)
  14.  
  15. # Use this function to load files:
  16. #splash_image = resource_path("splash-1.png")
  17. icon_path = resource_path("W.ico")
  18. #================ADD-IMAGE-ICON=================   
  19.    
  20. # === Paths ===
  21. # Detect if running from PyInstaller EXE
  22. if hasattr(sys, '_MEIPASS'):
  23.     base_path = sys._MEIPASS
  24. else:
  25.     base_path = os.path.dirname(os.path.abspath(__file__))
  26.  
  27. wall_dir = os.path.join(base_path, "WALLPAPERS")
  28. logo_dir = os.path.join(base_path, "LOGO")
  29. font_dir = os.path.join(base_path, "FONT")
  30. save_path = os.path.join(os.path.expanduser("~"), "Pictures", "WALL-PICTURE", "temp_wallpaper.jpg")
  31.  
  32. # Ensure output directory exists
  33. os.makedirs(os.path.dirname(save_path), exist_ok=True)
  34.  
  35. # === Image Functions ===
  36. def add_margin(img, top, left, bottom, right, color=(0, 0, 0)):
  37.     new_img = Image.new(img.mode, (img.width + left + right, img.height + top + bottom), color)
  38.     new_img.paste(img, (left, top))
  39.     return new_img
  40.  
  41. def adjust_resolution(path):
  42.     img = Image.open(path)
  43.     if img.width / 1920 > img.height / 1080:
  44.         pad = int((img.width * 1080 / 1920 - img.height) / 2)
  45.         img = add_margin(img, pad, 0, pad, 0)
  46.     else:
  47.         pad = int((img.height * 1920 / 1080 - img.width) / 2)
  48.         img = add_margin(img, 0, pad, 0, pad)
  49.     return img.resize((1920, 1080))
  50.  
  51. def update_preview(event=None):
  52.     try:
  53.         filename = wall_listbox.get(tk.ACTIVE)
  54.         path = os.path.join(wall_dir, filename)
  55.         base_img = adjust_resolution(path)
  56.         draw = ImageDraw.Draw(base_img)
  57.  
  58.         # Font
  59.         font_path = os.path.join(font_dir, font_var.get())
  60.         try:
  61.             font = ImageFont.truetype(font_path, 28)
  62.         except:
  63.             font = ImageFont.load_default()
  64.  
  65.         # Logo
  66.         logo_file = logo_var.get()
  67.         if logo_file != "None":
  68.             logo_path = os.path.join(logo_dir, logo_file)
  69.             if os.path.exists(logo_path):
  70.                 logo = Image.open(logo_path).convert("RGBA")
  71.                 logo.thumbnail((int(logo_w.get()), int(logo_h.get())), Image.Resampling.LANCZOS)
  72.                 lx = (base_img.width - logo.width) // 2
  73.                 ly = (base_img.height - logo.height) // 2
  74.                 base_img.paste(logo, (lx, ly), logo)
  75.  
  76.         # Text overlay
  77.         if show_time_var.get():
  78.             time_text = datetime.now().strftime("%H:%M:%S - %d %b %Y")
  79.             name_text = name_entry.get()
  80.  
  81.             time_bbox = draw.textbbox((0, 0), time_text, font=font)
  82.             time_x = 1660 - (time_bbox[2] - time_bbox[0])
  83.             time_y = 950
  84.             draw.text((time_x, time_y), time_text, font=font, fill=(255, 255, 255))
  85.  
  86.             name_bbox = draw.textbbox((0, 0), name_text, font=font)
  87.             name_x = 1660 - (name_bbox[2] - name_bbox[0])
  88.             name_y = time_y + (time_bbox[3] - time_bbox[1]) + 8
  89.             draw.text((name_x, name_y), name_text, font=font, fill=(255, 255, 255))
  90.             draw.line((name_x, name_y + 30, name_x + (name_bbox[2] - name_bbox[0]), name_y + 30), fill="white", width=2)
  91.  
  92.         base_img.save(save_path)
  93.         preview_img = base_img.resize((700, 400))
  94.         preview_photo = ImageTk.PhotoImage(preview_img)
  95.         preview_label.config(image=preview_photo)
  96.         preview_label.image = preview_photo
  97.     except Exception as e:
  98.         print(f"❌ Preview update failed: {e}")
  99.  
  100. def set_wallpaper():
  101.     ctypes.windll.user32.SystemParametersInfoW(20, 0, save_path, 3)
  102.  
  103. # === GUI Setup ===
  104. root = tk.Tk()
  105. root.title("Wallpaper Customizer - Najeeb Shah Khan")
  106. root.geometry("1080x700")
  107. #root.configure(bg="#181818")
  108. root.configure(bg="#2c3e50")
  109. root.iconbitmap(icon_path)
  110.  
  111. # === Left: Wallpaper list with scrollbar ===
  112. left_frame = tk.Frame(root, bg="#222")
  113. left_frame.pack(side="left", fill="y", padx=10, pady=10)
  114.  
  115. tk.Label(left_frame, text="Wallpapers", bg="#222", fg="white", font=("Segoe UI", 12)).pack(pady=5)
  116.  
  117. scrollbar = tk.Scrollbar(left_frame, orient="vertical")
  118. wall_listbox = tk.Listbox(left_frame, height=28, yscrollcommand=scrollbar.set,
  119.                           bg="black", fg="white", selectbackground="#444")
  120. scrollbar.config(command=wall_listbox.yview)
  121. wall_listbox.pack(side="left", fill="y")
  122. scrollbar.pack(side="right", fill="y")
  123.  
  124. wallpapers = [f for f in os.listdir(wall_dir) if f.lower().endswith(('.jpg', '.png'))]
  125. for w in wallpapers:
  126.     wall_listbox.insert(tk.END, w)
  127. wall_listbox.bind("<<ListboxSelect>>", update_preview)
  128.  
  129. # === Right: Controls & Preview ===
  130. right_frame = tk.Frame(root, bg="#181818")
  131. right_frame.pack(side="right", fill="both", expand=True, padx=10, pady=10)
  132.  
  133. # === Top Row Controls ===
  134. top_row = tk.Frame(right_frame, bg="#181818")
  135. top_row.pack(pady=5)
  136.  
  137. tk.Label(top_row, text="Font:", bg="#181818", fg="white").grid(row=0, column=0, sticky="e", padx=3)
  138. font_files = [f for f in os.listdir(font_dir) if f.endswith(".ttf")]
  139. font_var = tk.StringVar(value=font_files[0])
  140. ttk.Combobox(top_row, textvariable=font_var, values=font_files, width=20).grid(row=0, column=1, padx=5)
  141.  
  142. tk.Label(top_row, text="Logo:", bg="#181818", fg="white").grid(row=0, column=2, sticky="e", padx=3)
  143. logo_files = ["None"] + [f for f in os.listdir(logo_dir) if f.lower().endswith((".png", ".jpg"))]
  144. logo_var = tk.StringVar(value=logo_files[0])
  145. ttk.Combobox(top_row, textvariable=logo_var, values=logo_files, width=20).grid(row=0, column=3, padx=5)
  146.  
  147. tk.Label(top_row, text="Logo Size (W x H):", bg="#181818", fg="white").grid(row=0, column=4, padx=(10, 3))
  148. logo_w = tk.Entry(top_row, width=5)
  149. logo_w.insert(0, "400")
  150. logo_h = tk.Entry(top_row, width=5)
  151. logo_h.insert(0, "400")
  152. logo_w.grid(row=0, column=5)
  153. logo_h.grid(row=0, column=6)
  154.  
  155. # === Second Row Controls ===
  156. second_row = tk.Frame(right_frame, bg="#181818")
  157. second_row.pack(pady=5)
  158.  
  159. tk.Label(second_row, text="Custom Name:", bg="#181818", fg="white").grid(row=0, column=0, sticky="e", padx=3)
  160. name_entry = tk.Entry(second_row, width=40)
  161. name_entry.insert(0, "By - Najeeb Shah Khan")
  162. name_entry.grid(row=0, column=1, columnspan=2, padx=5)
  163.  
  164. show_time_var = tk.BooleanVar(value=True)
  165. tk.Checkbutton(second_row, text="Show Time & Date", variable=show_time_var,
  166.                bg="#181818", fg="white", selectcolor="#333").grid(row=0, column=3, padx=10)
  167.  
  168. # === Third Row: Buttons ===
  169. btn_row = tk.Frame(right_frame, bg="#181818")
  170. btn_row.pack(pady=10)
  171.  
  172. tk.Button(btn_row, text="Update Preview", command=update_preview, bg="#333", fg="white", width=20).grid(row=0, column=0, padx=10)
  173. tk.Button(btn_row, text="Set As Wallpaper", command=set_wallpaper, bg="#007acc", fg="white", width=20).grid(row=0, column=1, padx=10)
  174.  
  175. # === Preview Label ===
  176. preview_label = tk.Label(right_frame, bg="black", width=800, height=500)
  177. preview_label.pack(pady=10)
  178.  
  179. root.mainloop()
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement