Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog, Canvas, Scrollbar
- from PIL import Image, ImageTk
- # Initialize global variables
- img = None
- img_with_logo = None
- logo_img = None # Logo at current size
- original_logo_img = None # Original logo image for high-quality resizing
- logo_position = (0, 0)
- logo_size = 100
- def browse_image():
- """Open a file dialog to choose an image file."""
- filepath = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
- if filepath:
- original_entry.delete(0, tk.END) # Clear any existing text in the entry
- original_entry.insert(0, filepath)
- load_image(filepath)
- def browse_logo():
- """Open a file dialog to choose a logo file."""
- filepath = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
- if filepath:
- logo_entry.delete(0, tk.END) # Clear any existing text in the entry
- logo_entry.insert(0, filepath)
- load_logo(filepath)
- def load_image(filepath):
- """Load the image into the canvas and allow zooming."""
- global img, img_with_logo
- img = Image.open(filepath)
- img_with_logo = img.copy() # Create a copy for adding the logo
- display_image()
- def load_logo(filepath):
- """Load the logo image and prepare it for adding to the main image."""
- global logo_img, original_logo_img, logo_position
- original_logo_img = Image.open(filepath) # Load the original logo
- #logo_img = original_logo_img.resize((logo_size, logo_size), Image.ANTIALIAS) # Resize for the first time
- logo_img = Image.open(filepath) # Load the logo in its original size
- logo_position = (img_with_logo.width // 2 - logo_img.width // 2,
- img_with_logo.height // 2 - logo_img.height // 2) # Center the logo
- display_image() # Redraw the image with the logo (even if it's not yet applied)
- def apply_logo():
- """Apply the logo to the loaded image at its current position."""
- global img_with_logo
- if img_with_logo is not None and logo_img is not None: # Ensure both images are loaded
- img_with_logo = img.copy() # Reset the image before applying logo
- img_with_logo.paste(logo_img, logo_position, logo_img.convert('RGBA')) # Apply with transparency
- display_image() # Refresh the display with the logo applied
- def display_image(scale_percent=100):
- """Display the image on the canvas with a specified scale percentage."""
- global tk_img
- if img_with_logo is not None:
- img_resized = img_with_logo.resize((int(img_with_logo.width * scale_percent / 100),
- int(img_with_logo.height * scale_percent / 100)))
- tk_img = ImageTk.PhotoImage(img_resized)
- # Clear the canvas and redraw the image
- canvas.delete("all")
- canvas.create_image(0, 0, anchor="nw", image=tk_img)
- # Update the scroll region to match the resized image dimensions
- canvas.config(scrollregion=(0, 0, img_resized.width, img_resized.height))
- def update_zoom(value):
- """Update the zoom level based on the scale widget."""
- display_image(scale_percent=int(value))
- def save_image():
- """Save the modified image with the logo."""
- save_filepath = filedialog.asksaveasfilename(defaultextension=".png",
- filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*")])
- if save_filepath:
- img_with_logo.save(save_filepath)
- def start_move(event):
- """Start moving the logo when mouse is pressed on the canvas."""
- global move_start_x, move_start_y
- move_start_x, move_start_y = event.x, event.y
- def move_logo(event):
- """Move the logo according to the mouse dragging."""
- global logo_position
- dx = event.x - move_start_x
- dy = event.y - move_start_y
- logo_position = (logo_position[0] + dx, logo_position[1] + dy)
- apply_logo() # Redraw the image with the logo in the new position
- start_move(event) # Reset the starting position
- def resize_logo(event):
- """Resize the logo using the mouse wheel."""
- global logo_img, logo_size
- if original_logo_img is not None: # Ensure original logo is loaded
- if event.delta > 0:
- logo_size += 10 # Increase size
- else:
- logo_size = max(20, logo_size - 10) # Decrease size but not below 20px
- logo_img = original_logo_img.resize((logo_size, logo_size), Image.ANTIALIAS) # Always resize from the original
- apply_logo() # Redraw the image with resized logo
- # Create the main window
- root = tk.Tk()
- root.title("Najeeb Image Viewer with Zoom and Logo")
- root.geometry("900x700")
- # Top part: Heading, Text Field, and Buttons
- top_frame = tk.Frame(root)
- top_frame.pack(side=tk.TOP, fill=tk.X, pady=10)
- # Input for the image file
- original_entry = tk.Entry(top_frame, width=40)
- original_entry.pack(side=tk.LEFT, padx=5)
- browse_button = tk.Button(top_frame, text="Browse Image", command=browse_image)
- browse_button.pack(side=tk.LEFT, padx=5)
- # Input for the logo file
- logo_entry = tk.Entry(top_frame, width=40)
- logo_entry.pack(side=tk.LEFT, padx=5)
- logo_browse_button = tk.Button(top_frame, text="Browse Logo", command=browse_logo)
- logo_browse_button.pack(side=tk.LEFT, padx=5)
- # Apply Logo button
- apply_logo_button = tk.Button(top_frame, text="Apply Logo", command=apply_logo)
- apply_logo_button.pack(side=tk.LEFT, padx=5)
- # Save Image button
- save_button = tk.Button(top_frame, text="Save", command=save_image)
- save_button.pack(side=tk.LEFT, padx=5)
- # Bottom part: Canvas for Image and Scrollbars
- canvas_frame = tk.Frame(root)
- canvas_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
- # Canvas with Scrollbars
- canvas = Canvas(canvas_frame, bg="white")
- canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- # Horizontal and vertical scrollbars
- scroll_x = Scrollbar(root, orient=tk.HORIZONTAL, command=canvas.xview)
- scroll_x.pack(side=tk.BOTTOM, fill=tk.X)
- scroll_y = Scrollbar(canvas_frame, orient=tk.VERTICAL, command=canvas.yview)
- scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
- # Configure canvas scrolling
- canvas.config(xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
- # Zoom Slider
- zoom_slider = tk.Scale(root, from_=50, to=300, orient=tk.HORIZONTAL, command=update_zoom)
- zoom_slider.pack()
- # Bind mouse events for moving and resizing the logo
- canvas.bind("<Button-1>", start_move)
- canvas.bind("<B1-Motion>", move_logo)
- root.bind("<MouseWheel>", resize_logo)
- # Run the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement