Advertisement
Najeebsk

IMAGE-LOGO5.0.pyw

Oct 7th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.75 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog, Canvas, Scrollbar, font
  4. from PIL import Image, ImageTk, ImageDraw, ImageFont
  5.  
  6. # Initialize global variables
  7. img = None
  8. img_with_logo = None
  9. logo_img = None
  10. original_logo_img = None  # Original logo image for high-quality resizing
  11. logo_position = (0, 0)
  12. logo_size = 100
  13. text_position = (50, 50)  # Initial text position
  14. selected_font = None
  15. move_text_start_x, move_text_start_y = 0, 0  # For moving text
  16.  
  17. def browse_image():
  18.     """Open a file dialog to choose an image file."""
  19.     filepath = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
  20.     if filepath:
  21.         original_entry.delete(0, tk.END)
  22.         original_entry.insert(0, filepath)
  23.         load_image(filepath)
  24.  
  25. def browse_logo():
  26.     """Open a file dialog to choose a logo file."""
  27.     filepath = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
  28.     if filepath:
  29.         logo_entry.delete(0, tk.END)
  30.         logo_entry.insert(0, filepath)
  31.         load_logo(filepath)
  32.  
  33. def load_image(filepath):
  34.     """Load the image into the canvas and allow zooming."""
  35.     global img, img_with_logo
  36.     img = Image.open(filepath)
  37.     img_with_logo = img.copy()
  38.     display_image()
  39.  
  40. def load_logo(filepath):
  41.     """Load the logo image and prepare it for adding to the main image."""
  42.     global logo_img, original_logo_img, logo_position
  43.     original_logo_img = Image.open(filepath)
  44.     logo_img = Image.open(filepath)
  45.     logo_position = (img_with_logo.width // 2 - logo_img.width // 2,
  46.                      img_with_logo.height // 2 - logo_img.height // 2)
  47.     display_image()
  48.  
  49. def apply_logo():
  50.     """Apply the logo to the loaded image at its current position."""
  51.     global img_with_logo
  52.     if img_with_logo is not None and logo_img is not None:
  53.         img_with_logo = img.copy()
  54.         img_with_logo.paste(logo_img, logo_position, logo_img.convert('RGBA'))
  55.         display_image()
  56.  
  57. def display_image(scale_percent=100):
  58.     """Display the image on the canvas with a specified scale percentage."""
  59.     global tk_img
  60.     if img_with_logo is not None:
  61.         img_resized = img_with_logo.resize((int(img_with_logo.width * scale_percent / 100),
  62.                                             int(img_with_logo.height * scale_percent / 100)))
  63.         tk_img = ImageTk.PhotoImage(img_resized)
  64.         canvas.delete("all")
  65.         canvas.create_image(0, 0, anchor="nw", image=tk_img)
  66.         canvas.config(scrollregion=(0, 0, img_resized.width, img_resized.height))
  67.         # Draw the text after updating the image
  68.         draw_text()
  69.  
  70. def update_zoom(value):
  71.     """Update the zoom level based on the scale widget."""
  72.     display_image(scale_percent=int(value))
  73.  
  74. def save_image():
  75.     """Save the modified image with the logo and text."""
  76.     save_filepath = filedialog.asksaveasfilename(defaultextension=".png",
  77.                                                  filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*")])
  78.     if save_filepath:
  79.         img_with_logo.save(save_filepath)
  80.  
  81. def start_move_logo(event):
  82.     """Start moving the logo when mouse is pressed on the canvas."""
  83.     global move_start_x, move_start_y
  84.     move_start_x, move_start_y = event.x, event.y
  85.  
  86. def move_logo(event):
  87.     """Move the logo according to the mouse dragging."""
  88.     global logo_position
  89.     dx = event.x - move_start_x
  90.     dy = event.y - move_start_y
  91.     logo_position = (logo_position[0] + dx, logo_position[1] + dy)
  92.     apply_logo()
  93.     start_move_logo(event)
  94.  
  95. def start_move_text(event):
  96.     """Start moving the text when mouse is pressed on the canvas."""
  97.     global move_text_start_x, move_text_start_y
  98.     move_text_start_x, move_text_start_y = event.x, event.y
  99.  
  100. def move_text(event):
  101.     """Move the text according to the mouse dragging."""
  102.     global text_position
  103.     dx = event.x - move_text_start_x
  104.     dy = event.y - move_text_start_y
  105.     text_position = (text_position[0] + dx, text_position[1] + dy)
  106.     display_image()  # Redisplay to show text at new position
  107.     start_move_text(event)
  108.  
  109. def resize_logo(event):
  110.     """Resize the logo using the mouse wheel."""
  111.     global logo_img, logo_size
  112.     if original_logo_img is not None:
  113.         if event.delta > 0:
  114.             logo_size += 10
  115.         else:
  116.             logo_size = max(20, logo_size - 10)
  117.         logo_img = original_logo_img.resize((logo_size, logo_size), Image.ANTIALIAS)
  118.         apply_logo()
  119.  
  120. def load_fonts():
  121.     """Load font names from a directory and return them in a list."""
  122.     fonts_dir = os.path.join(os.getcwd(), 'Fonts')
  123.     if not os.path.exists(fonts_dir):
  124.         os.makedirs(fonts_dir)
  125.     return [f for f in os.listdir(fonts_dir) if f.endswith(".ttf") or f.endswith(".otf")]
  126.  
  127. def apply_text():
  128.     """Apply the text with the selected font to the image."""
  129.     global img_with_logo
  130.     if img_with_logo is not None and selected_font is not None:
  131.         draw = ImageDraw.Draw(img_with_logo)
  132.         text = text_entry.get()
  133.         font_size = int(font_size_entry.get())
  134.         font_path = os.path.join(os.getcwd(), 'Fonts', selected_font.get())
  135.         font_style = ImageFont.truetype(font_path, font_size)
  136.         draw.text(text_position, text, font=font_style, fill="white")
  137.         display_image()
  138.  
  139. def draw_text():
  140.     """Draw the text on the canvas at the current text position."""
  141.     global img_with_logo
  142.     if img_with_logo is not None and selected_font is not None:
  143.         draw = ImageDraw.Draw(img_with_logo)
  144.         text = text_entry.get()
  145.         font_size = int(font_size_entry.get())
  146.         font_path = os.path.join(os.getcwd(), 'Fonts', selected_font.get())
  147.         font_style = ImageFont.truetype(font_path, font_size)
  148.         draw.text(text_position, text, font=font_style, fill="white")
  149.  
  150. # Create the main window
  151. root = tk.Tk()
  152. root.title("Image Viewer with Zoom, Logo, and Text")
  153. root.geometry("900x700")
  154. root.resizable(False, False)
  155.  
  156. # Top part: Heading, Text Field, and Buttons
  157. top_frame = tk.Frame(root)
  158. top_frame.pack(side=tk.TOP, fill=tk.X, pady=10)
  159.  
  160. # Input for the image file
  161. original_entry = tk.Entry(top_frame, width=30)
  162. original_entry.pack(side=tk.LEFT, padx=5)
  163. browse_button = tk.Button(top_frame, text="Browse Image", command=browse_image)
  164. browse_button.pack(side=tk.LEFT, padx=5)
  165.  
  166. # Input for the logo file
  167. logo_entry = tk.Entry(top_frame, width=30)
  168. logo_entry.pack(side=tk.LEFT, padx=5)
  169. logo_browse_button = tk.Button(top_frame, text="Browse Logo", command=browse_logo)
  170. logo_browse_button.pack(side=tk.LEFT, padx=5)
  171.  
  172. # Apply Logo button
  173. apply_logo_button = tk.Button(top_frame, text="Apply Logo", command=apply_logo)
  174. apply_logo_button.pack(side=tk.LEFT, padx=5)
  175.  
  176. # Save Image button
  177. save_button = tk.Button(top_frame, text="Save", command=save_image)
  178. save_button.pack(side=tk.LEFT, padx=5)
  179.  
  180. # Text and font section
  181. text_frame = tk.Frame(root)
  182. text_frame.pack(side=tk.TOP, fill=tk.X, pady=10)
  183.  
  184. # Text input
  185. text_entry = tk.Entry(text_frame, width=30)
  186. text_entry.pack(side=tk.LEFT, padx=5)
  187. text_label = tk.Label(text_frame, text="Text:")
  188. text_label.pack(side=tk.LEFT, padx=5)
  189.  
  190. # Font size input
  191. font_size_entry = tk.Entry(text_frame, width=5)
  192. font_size_entry.pack(side=tk.LEFT, padx=5)
  193. font_size_label = tk.Label(text_frame, text="Font Size:")
  194. font_size_label.pack(side=tk.LEFT, padx=5)
  195.  
  196. # Dropdown list for font selection
  197. fonts = load_fonts()
  198. selected_font = tk.StringVar()
  199. font_menu = tk.OptionMenu(text_frame, selected_font, *fonts)
  200. font_menu.pack(side=tk.LEFT, padx=5)
  201.  
  202. # Apply Text button
  203. apply_text_button = tk.Button(text_frame, text="Apply Text", command=apply_text)
  204. apply_text_button.pack(side=tk.LEFT, padx=5)
  205.  
  206. # Bottom part: Canvas for Image and Scrollbars
  207. canvas_frame = tk.Frame(root)
  208. canvas_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
  209.  
  210. canvas = Canvas(canvas_frame, bg="white")
  211. canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  212.  
  213. scroll_x = Scrollbar(root, orient=tk.HORIZONTAL, command=canvas.xview)
  214. scroll_x.pack(side=tk.BOTTOM, fill=tk.X)
  215. scroll_y = Scrollbar(canvas_frame, orient=tk.VERTICAL, command=canvas.yview)
  216. scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
  217.  
  218. canvas.config(xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
  219.  
  220. # Bind mouse events for moving the logo and text
  221. canvas.bind("<Button-1>", start_move_logo)  # Start moving logo
  222. canvas.bind("<B1-Motion>", move_logo)  # Move logo
  223.  
  224. canvas.bind("<Button-3>", start_move_text)  # Start moving text (right click)
  225. canvas.bind("<B3-Motion>", move_text)  # Move text
  226.  
  227. canvas.bind("<MouseWheel>", resize_logo)  # Mouse wheel for resizing logo
  228.  
  229. # Bind zoom slider
  230. scale = tk.Scale(root, from_=10, to=200, orient=tk.HORIZONTAL, command=update_zoom)
  231. scale.set(100)
  232. scale.pack(fill=tk.X, padx=10)
  233.  
  234. root.mainloop()
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement