I entered the code, I need to recognize and remove a certain word from the screenshot

40 Views Asked by At

need to be deleted en masse, let's say in my case the word©pomogalka.me from the screenshot, I tried everything, no, it doesn’t delete. uploaded a photo screenshot with an example, please help me, good people enter image description here The code itself

import os
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import cv2
import pytesseract
import re

# Set the path to the Tesseract OCR executable
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
custom_config = r'--oem 3 --psm 6 lang=eng'

def remove_text(input_folder, output_folder, text_to_remove, progress_var):
    images = os.listdir(input_folder)
    total_images = len(images)
    progress_step = 100 / total_images
    current_progress = 0

    for image_name in images:
        image_path = os.path.join(input_folder, image_name)
        output_path = os.path.join(output_folder, image_name)
        image = cv2.imread(image_path)

        if image is None:
            messagebox.showwarning("Warning", f"Failed to read image {image_name}")
            continue

        # Preprocess the image
        processed_image = preprocess_image(image)

        # Convert the image to text using pytesseract
        text = pytesseract.image_to_string(processed_image, config=custom_config)

        # Remove all occurrences of the specified word from the text using regular expressions
        modified_text = re.sub(r'\b' + re.escape(text_to_remove) + r'\b', '', text)

        # Convert back to image
        modified_image = image.copy()
        cv2.putText(modified_image, modified_text, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

        # Save the processed image
        cv2.imwrite(output_path, modified_image)

        current_progress += progress_step
        progress_var.set(current_progress)

def preprocess_image(image):
    # Apply blurring
    blurred = cv2.GaussianBlur(image, (5, 5), 0)
    return blurred

def select_input_folder():
    input_folder = filedialog.askdirectory()
    input_entry.delete(0, tk.END)
    input_entry.insert(0, input_folder)

def select_output_folder():
    output_folder = filedialog.askdirectory()
    output_entry.delete(0, tk.END)
    output_entry.insert(0, output_folder)

def process_images():
    input_folder = input_entry.get()
    output_folder = output_entry.get()
    text_to_remove = text_entry.get()

    if not input_folder or not output_folder:
        messagebox.showerror("Error", "Please select input and output folders.")
        return

    progress_var.set(0)

    remove_text(input_folder, output_folder, text_to_remove, progress_var)

    messagebox.showinfo("Info", "Images processed successfully.")

root = tk.Tk()
root.title("Image Text Remover")

frame = ttk.Frame(root, padding="20")
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))

input_label = ttk.Label(frame, text="Input Folder:")
input_label.grid(row=0, column=0, sticky=tk.W)

input_entry = ttk.Entry(frame, width=50)
input_entry.grid(row=0, column=1, padx=5, pady=5)

input_button = ttk.Button(frame, text="Browse", command=select_input_folder)
input_button.grid(row=0, column=2, padx=5, pady=5)

output_label = ttk.Label(frame, text="Output Folder:")
output_label.grid(row=1, column=0, sticky=tk.W)

output_entry = ttk.Entry(frame, width=50)
output_entry.grid(row=1, column=1, padx=5, pady=5)

output_button = ttk.Button(frame, text="Browse", command=select_output_folder)
output_button.grid(row=1, column=2, padx=5, pady=5)

text_label = ttk.Label(frame, text="Text to Remove:")
text_label.grid(row=2, column=0, sticky=tk.W)

text_entry = ttk.Entry(frame, width=50)
text_entry.grid(row=2, column=1, padx=5, pady=5)

process_button = ttk.Button(frame, text="Process Images", command=process_images)
process_button.grid(row=3, column=0, columnspan=3, pady=10)

progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(root, orient=tk.HORIZONTAL, length=200, mode='determinate', variable=progress_var)
progress_bar.grid(row=1, column=0, pady=10)

root.mainloop()

I want to gea screenshot of the word at the exit ©pomogalka.me

0

There are 0 best solutions below