I'm a beginner in Python, I bought a book, and I'm currently learning while looking at information on the Web.
While looking at the information on the Web, use OCR for multiple image files saved in the folder screenshot, and output the results as one file (.txt, .word) or one file and another ocred screenshot. I'm trying, but it doesn't work.
Corresponding source code
Exception occurred: AttributeError
'str' object has no attribute'save'
File "C: \ Users \ admin \ Documents \ Python \ ocr_eng_test.py", line 24, in
text.save (os.path.join (new_dir_name, file))
What I tried
import os from PIL import Image import pyocr #Add the path of the installed Tesseract-OCR to the environment variable "PATH". If i set it in #OS itself, the following two lines are unnecessary path ='C: \\ Program Files \\ Tesseract-OCR' os.environ ['PATH'] = os.environ ['PATH'] + path Specify the OCR engine to be used for #pyocr in Tesseract. pyocr.tesseract.TESSERACT_CMD = r'C: \ Program Files \ Tesseract-OCR \ tesseract.exe' tools = pyocr.get_available_tools () tool = tools  dir_name = "screenshot" new_dir_name = "ocred screenshot" files = os.listdir (dir_name) for file in files: img = Image.open (os.path.join (dir_name, file)) builder = pyocr.builders.TextBuilder (tesseract_layout = 6) text = tool.image_to_string (img, lang = "eng", builder = builder) text.save (os.path.join (new_dir_name, file))
part of text.save (os.path.join (new_dir_name, file))
I changed it to and confirmed that all image files are translated.
From the error result, I understand that the syntax and grammar are incorrect, but I would appreciate it if you could give me some advice.
Please provide more detailed information here.
Answer # 1
I haven't investigated it in detail,
The part of text.save (os.path.join (new_dir_name, file))
with open (os.path.join (new_dir_name, file), "w") as f: f.write (text)
Shouldn't it be changed to?
If you want to change the name from .jpg to .txt, do the following:
with open (os.path.join (new_dir_name, file) [: -3] + "txt", "w") as f: f.write (text)
- python - i want to create a contour image
- python - tkinter photo image question
- python - i want to restore the result of calculating the inner product with numpy
- about for syntax i want to know the reason why the result is different depending on where the initial value is defined python
- python - image recognition using cnn keras multiple inputs
- python - i want to read an image and display it
- python - i want to divide the read image into pixels and label them
- python - i want to get the same result as assigning a variable and writing it directly in a function
- python - image is not saved even though there is no opencv error
- python - i want to display an image with pysimplegui, but an error occurs
- excel python i want to get the display result instead of the conditional statement
- python - i want to convert a black and white image to an arbitrary color image
- python - i want to display the scraping result in the browser
- python - error in image binarization using cv2adaptivethreshold function
- about image output of python
- python 3x - the output result of the numerical value obtained by web scraping becomes 0
- python - about image deletion in django
- python - image recognition with keras
- i want to adjust the execution result using the while statement in python as expected
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- i want to call a child component method from a parent in vuejs
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- python 3x - typeerror: 'method' object is not subscriptable