Keep getting "AttributeError: Read" from pytesseract - python

I'm trying to take a screen grab with PILLOW and reading text from it using pytesseract but I keep seeing a "AttributeError: read". I've tried to read the documentation and google but haven't found anything.
from PIL import ImageGrab
from PIL import Image
import PIL
snapShot = PIL.ImageGrab.grab(0, 0, 500, 500) #takes screenshot and stores it temporarily
pyt.pytesseract.tesseract_cmd = r'C:\Users\user\Desktop\project\venv\Scripts\pytesseract.exe' #locate pytesseract exe
im = Image.open(snapShot)
text = pyt.image_to_string(im)
print(text)
Error code:
File "C:\Users\user\Desktop\project\venv\lib\site-packages\PIL\Image.py", line 519, in __getattr__
raise AttributeError(name)
AttributeError: read

As a comment pointed out the use of 'Image.open()' on a Image object causes the error. Removing this gave another error however 'PermissionError: [WinError 5] Access is denied' which was resolved by installing the tesseract client on top of the pytesseract package and pointing to it.
from PIL import ImageGrab
import PIL
snapShot = PIL.ImageGrab.grab(0, 0, 500, 500) #takes screenshot and stores it temporarily
pyt.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract\tesseract.exe' #default path to tesseract client executable
text = pyt.image_to_string(snapshot)
print(text)

Related

How to print image to a pdf in python without saving image to disk first

I am making a scraper for my personal use that takes screenshots and saves them to a pdf. I am using pyscreenshot to take the screenshot and this returns a PIL image (PIL.PngImagePlugin.PngImageFile to be precise). I am using FPDF to make the pdf and can use the image method to add an image to the pdf.
The trouble is, this takes in either a file path or a URL. It is my understanding that saving many things to the disk will cause wear, so ideally I would like to save the image to the pdf straight from the PIL object instead of saving the image first. I'll be taking around 1500 screenshots, is this an unnecessary worry?
I am using Ubuntu if that changes anything. Here are the docs for the image method: https://pyfpdf.readthedocs.io/en/latest/reference/image/index.html
Here are the relevant parts of my code.
import pyscreenshot as ImageGrab
from fpdf import FPDF
def get_page_image():
page_image = ImageGrab.grab(bbox = half_page_bbox)
page_image.save("Test image 1.png")
return image
half_page_bbox = (249, 1, 1672, 1028)
pdf = FPDF("P", "mm", "A4")
pdf.add_page()
page_image = get_page_image()
pdf.image("Test image 1.png", 10, 10, 100)
pdf.output("My pdf.pdf")
I have tried using io to save it in memory, but this isn't working. Here is my code followed by the error.
import io
tmpFile = io.BytesIO()
image.save(tmpFile, format='png')
tmpFile.seek(0)
pdf.image(tmpFile, 10, 10, 100)
in <module>
pdf.image(tmpFile, 10, 10, 100)
File "/home/henry/.local/lib/python3.8/site-packages/fpdf/fpdf.py", line 150, in wrapper
return fn(self, *args, **kwargs)
File "/home/henry/.local/lib/python3.8/site-packages/fpdf/fpdf.py", line 963, in image
pos=name.rfind('.')
AttributeError: '_io.BytesIO' object has no attribute 'rfind'
You can do the following, note that I'm using fpdf2 instead of fpdf
pip install fpdf2
import pyscreenshot as ImageGrab
from fpdf import FPDF
img = ImageGrab.grab()
pdf = FPDF("P", "mm", "A4")
pdf.add_page()
pdf.image(img, 10, 10, 100)
pdf.output('My pdf.pdf')

PIL: OSError: Unknown File Format

Attempted to use a piece of code from other questions on Stack Overflow. Ran into this piece of code:
from PIL import ImageFont
from urllib.request import urlopen
truetype_url = 'https://github.com/googlefonts/roboto/blob/main/src/hinted/Roboto-Black.ttf'
font = ImageFont.truetype(urlopen(truetype_url), size=10)
I got this error:
OSError: unknown file format
I tried other suggestions such as reinstalling PIL, using requests.get and I receive the same error. I checked the link and it does take you to the item in question. Are there other suggestions I can try?
My Goal:
Be able to take a font from a link so I do not have to do this locally on my computer.
Thanks!
You can do it like this:
from PIL import Image, ImageFont, ImageDraw
import requests
import io
# Load font from URI
truetype_url = 'https://github.com/googlefonts/roboto/blob/main/src/hinted/Roboto-Black.ttf?raw=true'
r = requests.get(truetype_url, allow_redirects=True)
font = ImageFont.truetype(io.BytesIO(r.content), size=24)
# Create a black canvas and get drawing context
canvas = Image.new('RGB', (300,180))
draw = ImageDraw.Draw(canvas)
# Write in our font
draw.text((10, 10), "Got that crazy font", font=font, fill=(255,255,255))
canvas.save('result.png')
As Karl points out in the comments, you can do it with urllib as you were originally intending like this:
from urllib.request import urlopen
truetype_url = 'https://github.com/googlefonts/roboto/blob/main/src/hinted/Roboto-Black.ttf?raw=true'
font = ImageFont.truetype(urlopen(truetype_url), size=10)

Tesseract OCR fails on TIFF files

I have a multiple page .tif file, I am trying to extract text from it using Tesseract OCR but I am getting this error
TypeError: Unsupported image object
Code
from PIL import Image
import pytesseract
img = Image.open('Group 1/1_CHE_MDC_1.tif')
text = pytesseract.image_to_string(img.seek(0)) # OCR on 1st Page
text = ' '.join(text.split())
print(text)
ERROR
Any idea why its happening
Image.seek does not have a return value so you're essentially running:
pytesseract.image_to_string(None)
Instead do:
img.seek(0)
text = pytesseract.image_to_string(img)
I had a same question and i have tried below code and it worked for me :-
import glob
import pytesseract
import os
os.chdir("Set your Tesseract-OCR .exe file path")
b = ''
for i in glob.glob('Fullpath of your image directory/*.tif'): <-- you can give *.jpg extension in case of jpg image
if glob.glob('*.tif'):
b = b + (pytesseract.image_to_string(i))
print(b)
Happy learning !

Get text from image

I need to use pytesseract to extract text from this picture: enter image description here
However, i used pytesseract. It wont work.Here is my code:
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('1.png')))
you have to install textract using this we can extract text from any extension.
# some python file
import textract
text = textract.process("path/to/file.extension")
you can provide file as image or pdf or any docs.
for your code
text = textract.process("1.png")

python - unable to add image to the GUI(tkinter) on windows

I am using python( my version is 2.7 ). I want to add an image to GUI (Tkinter) and then convert into executable format using pyinstaller.
I did followed as on SO, and also as said on ActiveState
When i mention the image's path on the code, it works only if i run it directly. If i convert it to exe it doesnt open.
Changing the code as mentioned from other solutions, like by converting it into encoded string, it runs fine on linux. But on windows it throws error
code:
from Tkinter import *
from PIL import ImageTk, Image
logo = '''
----- encoded string -----
'''
root = Tk()
logoimage = Tkinter.PhotoImage(master=root, data=logo)
Label(root, image=logoimage).pack()
root.mainloop()
Change 1:
The above code works on linux. On windows i get error on the line logoimage = Tkinter.PhotoImage(master=root, data=logo) as
NameError: name 'Tkinter' is not defined
Change 2:
So i tries changing the line as logoimage = ImageTk.PhotoImage(master=root, data=logo). The error i get is
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 88, in __init__
image = Image.open(BytesIO(kw["data"]))
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2330, in open
% (filename if filename else fp))
IOError: cannot identify image file <_io.BytesIO object at 0x00000000024BB150>
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x00000000024D49E8>> ignored
Change 3:
But, if i change the line as iconImage= ImageTk.PhotoImage(Image.open('path_to_image.png')). It works only if i run directly. If i convert it to executable, then console opens for 2-3 seconds and displaying error something like Unable to locate the image file
Doing the decoding and converting explicitly may be more robust than what you're currently doing. This code works on Python 2.6.6 on Linux.
import io, base64
from Tkinter import *
from PIL import ImageTk, Image
#A simple 64x64 PNG fading from orange in the top left corner
# to red in the bottom right, encoded in base64
logo_b64 = '''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIA
AAAlC+aJAAAA/0lEQVR4nO3Zyw7CMAxEUdP//+W2rCqBoJA2noclS1kn9yjLeex7xKY76+
wNS+l6KSCjXgdIqhcB8uoVgNR6OiC7ngsA1BMBmHoWAFZPASDr8QBwPRiAr0cCKPUwAKse
AyDWAwDc+mwAvT4VoKjPA4jqkwC6+gyAtD7WSYC6fu4HDOonAB71dwE29bcATvXXAWb1Fw
F+9VcAlvXDANf6MYBx/QDAu/4fwL7+J6BC/TmgSP0JoE79N0Cp+g9Atfp3QMH6F0DN+gNQ
tj62WErXB2PgQNZLAb3U6wC91OsAvdTrAL3U6wC91OsAvdTrAL3U6wC91OsAvdTrAL3Uz7
z+BNmX4gqbppsaAAAAAElFTkSuQmCC
'''
#Decode the PNG data & "wrap" it into a file-like object
fh = io.BytesIO(base64.b64decode(logo_b64))
#Create a PIL image from the PNG data
img = Image.open(fh, mode='r')
#We must open the window before calling ImageTk.PhotoImage
root = Tk()
photo = ImageTk.PhotoImage(image=img)
Label(root, image=photo).pack()
Label(root, text='An embedded\nbase64-encoded PNG').pack()
root.mainloop()
For reference, here's what that embedded PNG looks like.
from Tkinter import *
#...
logoimage = Tkinter.PhotoImage(master=root, data=logo)
If you dump the Tkinter module straight into the global scope using import *, then you shouldn't prefix class and function names with the module name. Either remove the prefix, or remove the import *.
import Tkinter
#...
logoimage = Tkinter.PhotoImage(master=root, data=logo)
Or
from Tkinter import *
#...
logoimage = PhotoImage(master=root, data=logo)
I suspect you're not getting the error in Linux because your version of Python imports common modules automatically. Effectively, there's an invisible import Tkinter at the top of all your scripts.

Categories