Advertisement
Najeebsk

IMAGE-LOGO.pyw

Oct 5th, 2024 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.56 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, Canvas, Scrollbar
  3. from PIL import Image, ImageTk
  4.  
  5. # Initialize global variables
  6. img = None
  7. img_with_logo = None
  8. logo_img = None  # Logo at current size
  9. original_logo_img = None  # Original logo image for high-quality resizing
  10. logo_position = (0, 0)
  11. logo_size = 100
  12.  
  13. def browse_image():
  14.     """Open a file dialog to choose an image file."""
  15.     filepath = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
  16.     if filepath:
  17.         original_entry.delete(0, tk.END)  # Clear any existing text in the entry
  18.         original_entry.insert(0, filepath)
  19.         load_image(filepath)
  20.  
  21. def browse_logo():
  22.     """Open a file dialog to choose a logo file."""
  23.     filepath = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
  24.     if filepath:
  25.         logo_entry.delete(0, tk.END)  # Clear any existing text in the entry
  26.         logo_entry.insert(0, filepath)
  27.         load_logo(filepath)
  28.  
  29. def load_image(filepath):
  30.     """Load the image into the canvas and allow zooming."""
  31.     global img, img_with_logo
  32.     img = Image.open(filepath)
  33.     img_with_logo = img.copy()  # Create a copy for adding the logo
  34.     display_image()
  35.  
  36. def load_logo(filepath):
  37.     """Load the logo image and prepare it for adding to the main image."""
  38.     global logo_img, original_logo_img, logo_position
  39.     original_logo_img = Image.open(filepath)  # Load the original logo
  40.     #logo_img = original_logo_img.resize((logo_size, logo_size), Image.ANTIALIAS)  # Resize for the first time
  41.     logo_img = Image.open(filepath)  # Load the logo in its original size
  42.     logo_position = (img_with_logo.width // 2 - logo_img.width // 2,
  43.                      img_with_logo.height // 2 - logo_img.height // 2)  # Center the logo
  44.     display_image()  # Redraw the image with the logo (even if it's not yet applied)
  45.  
  46. def apply_logo():
  47.     """Apply the logo to the loaded image at its current position."""
  48.     global img_with_logo
  49.     if img_with_logo is not None and logo_img is not None:  # Ensure both images are loaded
  50.         img_with_logo = img.copy()  # Reset the image before applying logo
  51.         img_with_logo.paste(logo_img, logo_position, logo_img.convert('RGBA'))  # Apply with transparency
  52.         display_image()  # Refresh the display with the logo applied
  53.  
  54. def display_image(scale_percent=100):
  55.     """Display the image on the canvas with a specified scale percentage."""
  56.     global tk_img
  57.     if img_with_logo is not None:
  58.         img_resized = img_with_logo.resize((int(img_with_logo.width * scale_percent / 100),
  59.                                             int(img_with_logo.height * scale_percent / 100)))
  60.         tk_img = ImageTk.PhotoImage(img_resized)
  61.        
  62.         # Clear the canvas and redraw the image
  63.         canvas.delete("all")
  64.         canvas.create_image(0, 0, anchor="nw", image=tk_img)
  65.        
  66.         # Update the scroll region to match the resized image dimensions
  67.         canvas.config(scrollregion=(0, 0, img_resized.width, img_resized.height))
  68.  
  69. def update_zoom(value):
  70.     """Update the zoom level based on the scale widget."""
  71.     display_image(scale_percent=int(value))
  72.  
  73. def save_image():
  74.     """Save the modified image with the logo."""
  75.     save_filepath = filedialog.asksaveasfilename(defaultextension=".png",
  76.                                                  filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*")])
  77.     if save_filepath:
  78.         img_with_logo.save(save_filepath)
  79.  
  80. def start_move(event):
  81.     """Start moving the logo when mouse is pressed on the canvas."""
  82.     global move_start_x, move_start_y
  83.     move_start_x, move_start_y = event.x, event.y
  84.  
  85. def move_logo(event):
  86.     """Move the logo according to the mouse dragging."""
  87.     global logo_position
  88.     dx = event.x - move_start_x
  89.     dy = event.y - move_start_y
  90.     logo_position = (logo_position[0] + dx, logo_position[1] + dy)
  91.     apply_logo()  # Redraw the image with the logo in the new position
  92.     start_move(event)  # Reset the starting position
  93.  
  94. def resize_logo(event):
  95.     """Resize the logo using the mouse wheel."""
  96.     global logo_img, logo_size
  97.     if original_logo_img is not None:  # Ensure original logo is loaded
  98.         if event.delta > 0:
  99.             logo_size += 10  # Increase size
  100.         else:
  101.             logo_size = max(20, logo_size - 10)  # Decrease size but not below 20px
  102.         logo_img = original_logo_img.resize((logo_size, logo_size), Image.ANTIALIAS)  # Always resize from the original
  103.         apply_logo()  # Redraw the image with resized logo
  104.  
  105. # Create the main window
  106. root = tk.Tk()
  107. root.title("Najeeb Image Viewer with Zoom and Logo")
  108. root.geometry("900x700")
  109.  
  110. # Top part: Heading, Text Field, and Buttons
  111. top_frame = tk.Frame(root)
  112. top_frame.pack(side=tk.TOP, fill=tk.X, pady=10)
  113.  
  114. # Input for the image file
  115. original_entry = tk.Entry(top_frame, width=40)
  116. original_entry.pack(side=tk.LEFT, padx=5)
  117. browse_button = tk.Button(top_frame, text="Browse Image", command=browse_image)
  118. browse_button.pack(side=tk.LEFT, padx=5)
  119.  
  120. # Input for the logo file
  121. logo_entry = tk.Entry(top_frame, width=40)
  122. logo_entry.pack(side=tk.LEFT, padx=5)
  123. logo_browse_button = tk.Button(top_frame, text="Browse Logo", command=browse_logo)
  124. logo_browse_button.pack(side=tk.LEFT, padx=5)
  125.  
  126. # Apply Logo button
  127. apply_logo_button = tk.Button(top_frame, text="Apply Logo", command=apply_logo)
  128. apply_logo_button.pack(side=tk.LEFT, padx=5)
  129.  
  130. # Save Image button
  131. save_button = tk.Button(top_frame, text="Save", command=save_image)
  132. save_button.pack(side=tk.LEFT, padx=5)
  133.  
  134. # Bottom part: Canvas for Image and Scrollbars
  135. canvas_frame = tk.Frame(root)
  136. canvas_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
  137.  
  138. # Canvas with Scrollbars
  139. canvas = Canvas(canvas_frame, bg="white")
  140. canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  141.  
  142. # Horizontal and vertical scrollbars
  143. scroll_x = Scrollbar(root, orient=tk.HORIZONTAL, command=canvas.xview)
  144. scroll_x.pack(side=tk.BOTTOM, fill=tk.X)
  145. scroll_y = Scrollbar(canvas_frame, orient=tk.VERTICAL, command=canvas.yview)
  146. scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
  147.  
  148. # Configure canvas scrolling
  149. canvas.config(xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
  150.  
  151. # Zoom Slider
  152. zoom_slider = tk.Scale(root, from_=50, to=300, orient=tk.HORIZONTAL, command=update_zoom)
  153. zoom_slider.pack()
  154.  
  155. # Bind mouse events for moving and resizing the logo
  156. canvas.bind("<Button-1>", start_move)
  157. canvas.bind("<B1-Motion>", move_logo)
  158. root.bind("<MouseWheel>", resize_logo)
  159.  
  160. # Run the Tkinter event loop
  161. root.mainloop()
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement