Hướng dẫn python convert picture to text - python chuyển đổi hình ảnh thành văn bản
Xem thảo luận Show
Cải thiện bài viết Lưu bài viết Xem thảo luận Cải thiện bài viết Lưu bài viết Đọc Bàn luận
Examples: Input : We Have an image with some text Output: THE TEXT FROM THE IMAGE IS EXTRACTED AND A VOICE WILL SPEAK THE TEXT This is the first line of this text example. This is the second line of the same text. Translated(src=en, dest=de, text=Dies ist die erste Zeile von Dieses Textbeispiel. Dies ist die zweite Zeile desselben Textes., pronunciation=None, extra_data="{'translat..." Thư viện hình ảnh Python (PIL): Nó thêm khả năng xử lý hình ảnh vào trình thông dịch Python của bạn Googletrans: Đây là một thư viện Python miễn phí thực hiện API Google Dịch. Mã: Mã Python để chuyển đổi văn bản thành bài phát biểu
pip install pytesseract pip install pillow1
pip install pytesseract pip install pillow3 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)5 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)6 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)7 pip install pytesseract pip install pillow9 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)9
pip install pytesseract pip install pillow5 import pip install pytesseract pip install pillow7 Sample Text 13 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)1 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)2 Sample Text 16 Sample Text 17 pip install pytesseract pip install pillow9 Sample Text 19 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)0 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)1 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)2 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)3 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)1 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)5 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)3 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)5 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)8 pip install pytesseract pip install pillow8 pip install pytesseract pip install pillow9 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)0 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)1 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)2223 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)4 Sample Text 10 pip install pytesseract pip install pillow9 Sample Text 12 from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text)5 Sample Text 1 Sample Text 2 Sample Text 39 from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text)9 pip install pytesseract pip install pillow9 Sample Text 1 Sample Text 2 Sample Text 31
Bài viết này được xuất bản lần đầu tiên trên Pyshark, và vui lòng đóng góp cho những người viết blog Python. (Bạn có thể báo cáo vấn đề về nội dung trên trang này tại đây) Bạn muốn chia sẻ nội dung của bạn trên Python-Bloggers? bấm vào đây. PyShark , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Trong hướng dẫn này, chúng tôi sẽ khám phá cách trích xuất văn bản từ hình ảnh bằng Python. Mục lục
Giới thiệuHình ảnh mẫu Trích xuất văn bản từ một hình ảnh duy nhất bằng Python Trích xuất văn bản từ nhiều hình ảnh bằng Python
Hai thư viện Python: pytesseract cái gối Tesseract là một công cụ OCR (Nhận dạng ký tự quang học) nguồn mở cho phép trích xuất văn bản từ hình ảnh. Để sử dụng nó trong Python, chúng tôi cũng sẽ cần thư viện PyTesseract, một trình bao bọc cho động cơ Tesseract. pip install pytesseract pip install pillow Hình ảnh mẫuTrích xuất văn bản từ một hình ảnh duy nhất bằng Python Trích xuất văn bản từ nhiều hình ảnh bằng Python
Trích xuất văn bản từ hình ảnh là một nhiệm vụ rất phổ biến trong các đơn vị hoạt động của doanh nghiệp (trích xuất thông tin từ hóa đơn và biên lai) cũng như trong các lĩnh vực khác. OCR (Nhận dạng ký tự quang học) là một cách tiếp cận dựa trên máy tính điện tử để chuyển đổi hình ảnh của văn bản thành văn bản được mã hóa bằng máy, sau đó có thể được trích xuất và sử dụng ở định dạng văn bản. Để tiếp tục làm theo hướng dẫn này, chúng tôi sẽ cần:sampletext1-ocr.png Tesseract Hai thư viện Python:images and the code resides in main.py pytesseract cái gốitessaract.exe which was created after the installation. On Windows it should reside in: C:\Program Files\Tesseract-OCR\tesseract.exe Bây giờ chúng ta có mọi thứ chúng ta cần và có thể dễ dàng trích xuất văn bản từ hình ảnh bằng Python: from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open(path_to_image) #Extract text from image text = pytesseract.image_to_string(img) print(text) Và bạn nên nhận được: Sample Text 1 Trong phần này, chúng tôi sẽ khám phá cách trích xuất văn bản từ nhiều hình ảnh bằng Python. Chúng tôi biết rằng tất cả các hình ảnh được đặt trong hình ảnh thư mục và mã nằm trong Main.pyimages and the code resides in main.py Một cách trích xuất văn bản từ mỗi hình ảnh sẽ là sử dụng tên tệp của mỗi hình ảnh và trích xuất văn bản từ từng hình ảnh. Nhưng nếu chúng ta có 100 hình ảnh trong thư mục? Sử dụng thư viện OS, chúng tôi có thể truy cập tất cả các tệp trong một thư mục nhất định. Khi chúng tôi có quyền truy cập vào tất cả các tệp hình ảnh trong thư mục hình ảnh, chúng tôi sẽ lặp lại chúng và trích xuất văn bản từ mỗi hình ảnh bằng Python:images folder, we will iterate over them and extract text from each image using Python: from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk(path_to_images): #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open(path_to_images + file_name) #Extract text from image text = pytesseract.image_to_string(img) print(text) Và bạn nên nhận được: Sample Text 1 Sample Text 2 Sample Text 3 Trong phần này, chúng tôi sẽ khám phá cách trích xuất văn bản từ nhiều hình ảnh bằng Python. Chúng tôi biết rằng tất cả các hình ảnh được đặt trong hình ảnh thư mục và mã nằm trong Main.pyMột cách trích xuất văn bản từ mỗi hình ảnh sẽ là sử dụng tên tệp của mỗi hình ảnh và trích xuất văn bản từ từng hình ảnh. Nhưng nếu chúng ta có 100 hình ảnh trong thư mục? Sử dụng thư viện OS, chúng tôi có thể truy cập tất cả các tệp trong một thư mục nhất định. Khi chúng tôi có quyền truy cập vào tất cả các tệp hình ảnh trong thư mục hình ảnh, chúng tôi sẽ lặp lại chúng và trích xuất văn bản từ mỗi hình ảnh bằng Python: Đó chính xác là văn bản chúng ta có trong các hình ảnh.leave a comment for the author, please follow the link and comment on their blog: PyShark . Sự kết luận Làm cách nào để nhận văn bản từ một hình ảnh trong Python?Bây giờ chúng ta có mọi thứ chúng ta cần và có thể dễ dàng trích xuất văn bản từ hình ảnh bằng Python:.. Từ hình ảnh nhập khẩu PIL .. từ pytesseract nhập pytesseract .. img = hình ảnh. Mở (path_to_image). Text = pytesseract. Image_to_String (IMG). print(text). Làm cách nào để chuyển đổi hình ảnh thành mã văn bản?Chuyển đổi hình ảnh thành văn bản bằng phần mềm OCR: Nhấp vào bên trong khu vực thả tệp để tải lên tệp hình ảnh hoặc kéo và thả tệp hình ảnh. Nhấp vào quét "Quét hình ảnh" để bắt đầu quá trình OCR. Kết quả nhận dạng sẽ được hiển thị và có thể được sao chép.Click inside the file drop area to upload an image file or drag & drop an image file. Click the Scan "Scan Image" to start the OCR process. Recognition results will be displayed and can be copied.
Làm cách nào để chuyển đổi hình ảnh thành một chuỗi trong Python?Chuyển đổi hình ảnh thành chuỗi.. Ở đây trước tiên, chúng tôi nhập phương thức Base Base64 để mã hóa hình ảnh đã cho .. Tiếp theo, chúng tôi đã mở tệp hình ảnh của mình ở chế độ RB được đọc ở chế độ nhị phân .. Chúng tôi đọc hình ảnh của chúng tôi với Image2.read () đọc hình ảnh và mã hóa nó bằng b64encode () Đó là phương thức được sử dụng để mã hóa dữ liệu thành base64 .. Làm cách nào để trích xuất văn bản từ một hình ảnh?Cách trích xuất văn bản hoặc dữ liệu từ hình ảnh.. Bước 1: Tạo mô hình OCR của riêng bạn .. Bước 2: Tải lên các tệp/hình ảnh đào tạo .. Bước 3: Chú thích văn bản trên các tệp/hình ảnh .. Bước 4: Đào tạo mô hình OCR tùy chỉnh .. Bước 5: Kiểm tra và xác minh dữ liệu .. |