I am new to Pytesseract and currently in learning phase.
Here is the image:
from which I am trying to extract the text. The smaller text "movlmento, destmo" that I thought would be difficult to extract was easily extracted but the more bigger and visible text "TO" is not being extracted. I have searched all over but was not able to figure out a solution. Any help would be appreciated. Thank You!
Here is my code:
from PIL import Image
import pytesseract as p
p.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
img = Image.open('sample.jpg')
img = img.convert('1', dither=Image.NONE)
img2 = p.image_to_string(img)
print(img2)
Related
I am working on a template kind of thing where the input are some images and text while the result being an image.
Here one of the input is a QR code generated using pyqrcode. I am using PIL library to paste images and text. Problem here is google lens is not recognizing the qr code.
What to do to solve this issue.
The code I written to generate image
from PIL import Image, ImageFont, ImageDraw
import pyqrcode
#Read the two images
image1 = Image.open('./10.jpg')
#image1.show()
image2 = Image.open('./2.jpg')
#image2.show()
#resize, first image,second image
image1 = image1.resize((320,640))
image2=image2.resize((144,144))
image1_size = image1.size
image2_size = image2.size
#pasting image2 on image1
image1.paste(image2,(100,220))
#Adding text
font1=ImageFont.truetype('./1.ttf',30)
font2=ImageFont.truetype('./1.ttf',20)
text1="My Home Jewels"
image3=ImageDraw.Draw(image1)
image3.text((30,30),text1,(237,230,211),font=font1)
#Adding QR code
link1="www.google.com"
url=pyqrcode.QRCode(link1,error='L')
url.png('test.png',scale=2)
img=Image.open('test.png')
#img.save('./qr.jpg',quality=100)
#img=img.resize((72,72))
image1.paste(img,(30,500))
#Adding link as text
image3=ImageDraw.Draw(image1)
image3.text((120,520),link1,(237,230,211),font=font2)
image1.show()
image1.save('./3.jpg',quality=100)
I cannot reproduce. This image works for me.
I am trying to ready Energy Efficiency Rating from EPC certificate using python. Usually EPC certificate comes in PDF format. I have converted PDF into image already and using pytesseract to get text from image. However I am not getting expected results.
Sample Image:
Expected output:
Current rating : 79, Potential rating : 79
What I have tried so far:
from pdf2image import convert_from_path
import pytesseract
from PIL import Image
pages = convert_from_path(r'my_file.pdf', 500)
img =pages[0].save(r'F:\Freelancer\EPC rating\fwdepcs\out.jpg', 'JPEG')
text = pytesseract.image_to_string(Image.open(r'F:\Freelancer\EPC rating\fwdepcs\out.jpg'))
However text does not capture 79.
I also tried cv2 pattern matching and shape detection, but those not worked for other reasons.
You say that you have convert this pdf to image file.
Use PIL(.crop()) or opencv to crop picture.And crop it like this:
And use PIL Image.convert("1"),maybe tesseract can catch this number.
If not,I think you can use jTessBoxEditor to train tesseract.
I'm using tesseract to extract text from an image, an image of a license plate that I got using a text detector
from PIL import Image
import pytesseract
import cv2
img= cv2.imread('text0.jpg')
print (pytesseract.image_to_string(th))
However, it doesn't give the exact text, are there any filters I can use to improve the quality of the image?
Kindly review and give feedback.
U should make sure the text horizantal, and i hope this modificatons will help
from PIL import Image
import pytesseract
import cv2
img= cv2.imread('text0.jpg',0)
h,w= img.shape
img= cv2.resize(img, (w*2,h*2))
retval2,th = cv2.threshold(img,35,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print (pytesseract.image_to_string(th))
there are other approches u can try, like blurring and changing the contrast.
I have read mountains of posts on pytesseract, but I cannot get it to read text off a dead simple image; It returns an empty string.
Here is the image:
I have tried scaling it, grayscaling it, and adjusting the contrast, thresholding, blurring, everything it says in other posts, but my problem is that I don't know what the OCR wants to work better. Does it want blurry text? High contrast?
Code to try:
import pytesseract
from PIL import Image
print pytesseract.image_to_string(Image.open(IMAGE FILE))
As you can see in my code, the image is stored locally on my computer, hence Image.open()
Trying something along the lines of
import pytesseract
from PIL import Image
import requests
import io
response = requests.get('https://i.stack.imgur.com/J2ojU.png')
img = Image.open(io.BytesIO(response.content))
text = pytesseract.image_to_string(img, lang='eng', config='--psm 7')
print(text)
with --psm values equal or larger than 6 did yield "Gm" for me.
If the image is stored locally (and in your working directory), just drop the response variable and change the definition of text with the lines
image_name = "J2ojU.png" # or whatever appropriate
text = pytesseract.image_to_string(Image.open(image_name), lang='eng', config='--psm 7')
There are several reasons:
Edges are not sharp and continuous (By sharp I mean smooth, not with teeth)
Image is too small, you need to resize
Font is missing (not mandatory, but trained font incredibly improve possibility of recognition)
Based on points 1) and 2) I was able to recognize text.
1) I resized image 3x and 2) I blurred the image to make edges smooth
import pytesseract
import cv2
import numpy as np
import urllib
import requests
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
from PIL import Image
def url_to_image(url):
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
url = 'https://i.stack.imgur.com/J2ojU.png'
img = url_to_image(url)
retval, img = cv2.threshold(img,200,255, cv2.THRESH_BINARY)
img = cv2.resize(img,(0,0),fx=3,fy=3)
img = cv2.GaussianBlur(img,(11,11),0)
img = cv2.medianBlur(img,9)
cv2.imshow('asd',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
txt = pytesseract.image_to_string(img)
print('recognition:', txt)
>> recognition: Gm
Note:
This script is good for testing any image on web
Note 2:
All processing is based on your posted image
Note 3:
Text recognition is not easy. Every recognition requires special processing. If you try this steps with different image, it may not work at all. Important is to try a lot of recognition on images so you understand what tesseract wants
I am trying to extract the characters in the x-ray, I have tried using pytesseract to extract but couldn't succeed, I used a canny edge to remove the noise and extract, but still, I am not able to extract the text/chars. Can you please help/guide me to extract the text/chars
Try this tuotrial to locate the text:
https://www.pyimagesearch.com/2018/08/20/opencv-text-detection-east-text-detector/
Then once you locate you can isolate and use tesseract to recognize it.
If it's a DICOM file, you could use gdcm to get the attribute. It's available on python too.
pytesseract should be sufficient, if the file is in 'png' or 'jpg' form.
now suppose image is the name of your image. Please write the below code.
from PIL import Image
from pytesseract import image_to_string
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
im = Image.open('F:/kush/invert.jpg')
pytesseract.image_to_string(im, lang = 'eng')