Created
November 10, 2025 10:27
-
-
Save luxu/48ed7b22c802ffc4fa677ed99f5400d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io | |
| import os | |
| import re | |
| import string | |
| import FreeSimpleGUI as sg | |
| try: | |
| from PIL import Image, ImageEnhance | |
| except ImportError: | |
| import Image | |
| import pytesseract as ocr | |
| """ | |
| For Windows Only | |
| 1 - You need to have Tesseract OCR installed on your computer. | |
| get it from here. https://github.com/UB-Mannheim/tesseract/wiki | |
| Download the suitable version. | |
| 2 - Add Tesseract path to your System Environment. i.e. Edit system variables. | |
| 3 - Run pip install pytesseract and pip install tesseract | |
| 4 - Add this line to your python script every time | |
| pytesseract.pytesseract.tesseract_cmd = 'C:/OCR/Tesseract-OCR/tesseract.exe' # your path may be different | |
| 5 - Run the code. | |
| """ | |
| class Gui: | |
| def __init__(self): | |
| self.file_types = [ | |
| ("JPG (*.jpg)", "*.jpg"), | |
| ("JPEG (*.jpeg)", "*.jpeg"), | |
| ("PNG (*.png)", "*.png"), | |
| ("All files (*.*)", "*.*"), | |
| ] | |
| def list_folder(self): | |
| layout = [ | |
| [sg.T("Escolha a IMAGEM")], | |
| [sg.Image(key="-IMAGE-")], | |
| [ | |
| sg.Input(size=(25, 1), key="-FILE-"), | |
| sg.FileBrowse(file_types=self.file_types), | |
| sg.Button("Preview", key="preview"), | |
| ], | |
| [sg.Button("Submit"), sg.Exit()], | |
| [ | |
| sg.Multiline( | |
| font="Calibri", | |
| key="resultado_final", | |
| size=(100, 30), | |
| disabled=True, | |
| border_width=0, | |
| ) | |
| ], | |
| ] | |
| window = sg.Window("Transformar IMG em Texto", grab_anywhere=False).Layout(layout) | |
| while True: | |
| event, values = window.read() | |
| if "Submit" in event: | |
| if values["-FILE-"]: | |
| img_selected = values["-FILE-"] | |
| if os.name != "posix": # Windows | |
| text_in_the_image = self.get_text_to_imgr_by_windows(img_selected) | |
| else: | |
| text_in_the_image = self.get_text_to_imgr_by_linux(img_selected) | |
| # ngrams = self.cleanInput(text_in_the_image) | |
| # window["resultado_final"].update(f"{' '.join(ngrams)}") | |
| window["resultado_final"].update(f"{' '.join(text_in_the_image)}") | |
| elif "preview" in event: | |
| filename = values["-FILE-"] | |
| if os.path.exists(filename): | |
| image = Image.open(values["-FILE-"]) | |
| image.thumbnail((400, 600)) | |
| bio = io.BytesIO() | |
| image.save(bio, format="PNG") | |
| window["-IMAGE-"].update(data=bio.getvalue()) | |
| elif event == sg.WIN_CLOSED or "Exit" in event: | |
| sg.popup_auto_close("Exit...", auto_close_duration=0.5) | |
| break | |
| window.close() | |
| def cleanInput(self, input): | |
| input = re.sub(r"\n", " ", input) | |
| input = re.sub(r"\[[0-9]*\]", "", input) | |
| input = re.sub(" +", " ", input) | |
| input = bytes(input, "UTF-8") | |
| input = input.decode("ascii", "ignore") | |
| cleanInput = [] | |
| input = input.split(" ") | |
| for item in input: | |
| item1 = item.strip(string.punctuation) | |
| if len(item1) > 1 or item1.lower() in ["a", "i"]: | |
| cleanInput.append(item1.lower()) | |
| return cleanInput | |
| def get_text_to_imgr_by_windows(self, filename): | |
| homepath = os.path.expanduser(os.getenv("USERPROFILE")) | |
| desktoppath = "Desktop" | |
| # 'C:/Users/zicad/OneDrive/Área de Trabalho/deere.png' | |
| path_to_desktop = os.path.join(homepath, desktoppath) | |
| os.chdir(path_to_desktop) | |
| # path_to_img = os.path.abspath("fpython/ImageToText/img") | |
| # path = os.path.join(path_to_img, filename) | |
| path = os.path.join(path_to_desktop, filename) | |
| # ocr.pytesseract.tesseract_cmd = "C:/Users/luxu/AppData/Local/Programs/Tesseract-OCR/tesseract.exe" | |
| ocr.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR/tesseract.exe" | |
| with Image.open(path) as img: | |
| img.verify() # Retorna None se estiver tudo bem | |
| print("A imagem é válida.") | |
| # Carrega a imagem novamente para manipulação | |
| img = Image.open(path) | |
| img = img.convert("RGB") # Caso precise converter o formato | |
| print("A imagem foi carregada e está pronta para uso.") | |
| # Melhorar o contraste | |
| enhancer = ImageEnhance.Contrast(img) | |
| img = enhancer.enhance(2) # Aumenta o contraste, ajuste conforme necessário | |
| print("A imagem teve aumento de constraste.") | |
| img = img.convert("L") # Converte para tons de cinza | |
| try: | |
| # img = Image.open(path) | |
| # is_img_valid = img.verify() | |
| image = ocr.image_to_string(img, lang="por") | |
| # image = ocr.image_to_string(img) | |
| tamanho = len(image) | |
| if tamanho > 0: | |
| print("A imagem foi carregada com sucesso.") | |
| else: | |
| print("Texto está sem nada.") | |
| return image | |
| except Exception as e: | |
| raise ValueError(f"Error: {e}") from e | |
| if __name__ == "__main__": | |
| tela = Gui() | |
| img = tela.list_folder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment