Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import filedialog, Canvas, Scrollbar, font
- from PIL import Image, ImageTk, ImageDraw, ImageFont
- # Initialize global variables
- img = None
- img_with_logo = None
- logo_img = None
- original_logo_img = None # Original logo image for high-quality resizing
- logo_position = (0, 0)
- logo_size = 100
- text_position = (50, 50) # Initial text position
- selected_font = None
- move_text_start_x, move_text_start_y = 0, 0 # For moving text
- 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)
- 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)
- 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()
- 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)
- logo_img = Image.open(filepath)
- logo_position = (img_with_logo.width // 2 - logo_img.width // 2,
- img_with_logo.height // 2 - logo_img.height // 2)
- display_image()
- 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:
- img_with_logo = img.copy()
- img_with_logo.paste(logo_img, logo_position, logo_img.convert('RGBA'))
- display_image()
- 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)
- canvas.delete("all")
- canvas.create_image(0, 0, anchor="nw", image=tk_img)
- canvas.config(scrollregion=(0, 0, img_resized.width, img_resized.height))
- # Draw the text after updating the image
- draw_text()
- 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 and text."""
- 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_logo(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()
- start_move_logo(event)
- def start_move_text(event):
- """Start moving the text when mouse is pressed on the canvas."""
- global move_text_start_x, move_text_start_y
- move_text_start_x, move_text_start_y = event.x, event.y
- def move_text(event):
- """Move the text according to the mouse dragging."""
- global text_position
- dx = event.x - move_text_start_x
- dy = event.y - move_text_start_y
- text_position = (text_position[0] + dx, text_position[1] + dy)
- display_image() # Redisplay to show text at new position
- start_move_text(event)
- def resize_logo(event):
- """Resize the logo using the mouse wheel."""
- global logo_img, logo_size
- if original_logo_img is not None:
- if event.delta > 0:
- logo_size += 10
- else:
- logo_size = max(20, logo_size - 10)
- logo_img = original_logo_img.resize((logo_size, logo_size), Image.ANTIALIAS)
- apply_logo()
- def load_fonts():
- """Load font names from a directory and return them in a list."""
- fonts_dir = os.path.join(os.getcwd(), 'Fonts')
- if not os.path.exists(fonts_dir):
- os.makedirs(fonts_dir)
- return [f for f in os.listdir(fonts_dir) if f.endswith(".ttf") or f.endswith(".otf")]
- def apply_text():
- """Apply the text with the selected font to the image."""
- global img_with_logo
- if img_with_logo is not None and selected_font is not None:
- draw = ImageDraw.Draw(img_with_logo)
- text = text_entry.get()
- font_size = int(font_size_entry.get())
- font_path = os.path.join(os.getcwd(), 'Fonts', selected_font.get())
- font_style = ImageFont.truetype(font_path, font_size)
- draw.text(text_position, text, font=font_style, fill="white")
- display_image()
- def draw_text():
- """Draw the text on the canvas at the current text position."""
- global img_with_logo
- if img_with_logo is not None and selected_font is not None:
- draw = ImageDraw.Draw(img_with_logo)
- text = text_entry.get()
- font_size = int(font_size_entry.get())
- font_path = os.path.join(os.getcwd(), 'Fonts', selected_font.get())
- font_style = ImageFont.truetype(font_path, font_size)
- draw.text(text_position, text, font=font_style, fill="white")
- # Create the main window
- root = tk.Tk()
- root.title("Image Viewer with Zoom, Logo, and Text")
- root.geometry("900x700")
- root.resizable(False, False)
- # 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=30)
- 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=30)
- 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)
- # Text and font section
- text_frame = tk.Frame(root)
- text_frame.pack(side=tk.TOP, fill=tk.X, pady=10)
- # Text input
- text_entry = tk.Entry(text_frame, width=30)
- text_entry.pack(side=tk.LEFT, padx=5)
- text_label = tk.Label(text_frame, text="Text:")
- text_label.pack(side=tk.LEFT, padx=5)
- # Font size input
- font_size_entry = tk.Entry(text_frame, width=5)
- font_size_entry.pack(side=tk.LEFT, padx=5)
- font_size_label = tk.Label(text_frame, text="Font Size:")
- font_size_label.pack(side=tk.LEFT, padx=5)
- # Dropdown list for font selection
- fonts = load_fonts()
- selected_font = tk.StringVar()
- font_menu = tk.OptionMenu(text_frame, selected_font, *fonts)
- font_menu.pack(side=tk.LEFT, padx=5)
- # Apply Text button
- apply_text_button = tk.Button(text_frame, text="Apply Text", command=apply_text)
- apply_text_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 = Canvas(canvas_frame, bg="white")
- canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- 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)
- canvas.config(xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
- # Bind mouse events for moving the logo and text
- canvas.bind("<Button-1>", start_move_logo) # Start moving logo
- canvas.bind("<B1-Motion>", move_logo) # Move logo
- canvas.bind("<Button-3>", start_move_text) # Start moving text (right click)
- canvas.bind("<B3-Motion>", move_text) # Move text
- canvas.bind("<MouseWheel>", resize_logo) # Mouse wheel for resizing logo
- # Bind zoom slider
- scale = tk.Scale(root, from_=10, to=200, orient=tk.HORIZONTAL, command=update_zoom)
- scale.set(100)
- scale.pack(fill=tk.X, padx=10)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement