How to get text from image using pytesseract in images - python

I need to use pytesseract to extract text from this picture. This is possible?
ex.images:
Code:
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("4.jpg") # the second one
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('4temp.jpg')
text = pytesseract.image_to_string(Image.open('4temp.jpg'))
print(text)
Decoded text:
1 - EN
2- f-ybizc
3- CALE NGS
4- [BLANK]
Thanks!

Related

Display Dicom image using PIL(PILLOW) Python Library

I am trying to read and display DICOM(.dcm) images using below code:-
import pydicom as dicom
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
from PIL.ImageQt import ImageQt
def display_dicom_images(self, folder_Path):
try:
# Image parameters
image_width = 382
image_height = 382
image_depth = 3
self.total_images_in_folder = len(glob.glob1(folder_Path,"*"))
# Select the center image for display
self.current_image_number = round(self.total_images_in_folder / 2)
self.display_number = self.current_image_number
image_dtype = np.uint8
pixel_array = np.ndarray([self.total_images_in_folder, image_height, image_width, image_depth]).astype(image_dtype)
# load images here, once better MR images are acquired
for image_index in range(0, self.total_images_in_folder):
# for DICOM
image_path = folder_Path + "/" + str(image_index) + ".dcm"
scan_image = dicom.dcmread(image_path)
scan_image = scan_image.pixel_array.astype(image_dtype)
pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1], :scan_image.shape[2]] = scan_image
return pixel_array
But getting error:-
IndexError('tuple index out of range',)
i am using pillow python library for image.
How do you know scan_image.shape is of length 3? MR images should only be monochrome, which would make image_depth = 1 and the length of scan_image.shape equal to 2.
C.8.3.1.1.3 Photometric Interpretation
Enumerated Values:
MONOCHROME1
MONOCHROME2

Cache error when using pytesseract for OCR

I am trying to use pytesseract OCR to extract text from all the PDFs in a directory, but I am getting an error message that there is not enough space on my device.
I would like to delete each image from the cache after it is no longer required, as this user was advised to do, but I can't find anything in the pytesseract documentation explaining how to do this.
Here is my code:
import io
from PIL import Image
import pytesseract
from wand.image import Image as wi
def extract_text_from_image(path):
pdfFile = wi(filename = path, resolution = 300)
image = pdfFile.convert('jpeg')
imageBlobs = []
for img in image.sequence:
imgPage = wi(image = img)
imageBlobs.append(imgPage.make_blob('jpeg'))
extract = []
for imgBlob in imageBlobs:
image = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(image, lang = 'eng')
extract.append(text)
return extract
Here is the error message:
CacheError: unable to extend cache 'C:/Users/b00kgrrl/AppData/Local/Temp/magick-11952ORBzkae3wXX_18': No space left on device # error/cache.c/OpenPixelCache/3889
I solved this myself using code found here and here:
import io
from PIL import Image
import pytesseract
from wand.image import Image as wi
import winshell
def extract_text_from_image(path):
pdfFile = wi(filename = path, resolution = 300)
image = pdfFile.convert('jpeg')
tempdir = r"C:\Users\b00kgrrl\AppData\Local\Temp"
cache = os.listdir( tempdir )
imageBlobs = []
for img in image.sequence:
imgPage = wi(image = img)
imageBlobs.append(imgPage.make_blob('jpeg'))
extract = []
for imgBlob in imageBlobs:
image = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(image, lang = 'eng')
extract.append(text)
for item in cache:
if item.endswith(".jpg") or item.startswith("magick-"):
os.remove( os.path.join( tempdir, item ) )
winshell.recycle_bin().empty(confirm=False, show_progress=False, sound=False)
return extract

Running OCR Python

I am trying to make some OCR with Python. I found this code on the internet, which did what I want to. But when I try to run it, I receive this error message.
Leave my code Here:
import io
from PIL import Image
import pytesseract
from wand.image import Image as wi
pdf = wi(filename="test1(citibank).pdf", resolution=300)
pdfImage = pdf.convert('jpeg')
imageBlobs = []
for img in pdfImage.sequence:
imgPage=wi(image=img)
imageBlobs.append(imgPage.make_blob('jpeg'))
recognisedtext = []
for imgBlob in imageBlobs:
im = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(im, lang='es')
recognisedtext.append(text)
print(recognisedtext[1])

OpenCv pytesseract for OCR

How to use opencv and pytesseract to extract text from image?
import cv2
import pytesseract
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
img = Image.open('test.jpg').convert('L')
img.show()
img.save('test','png')
img = cv2.imread('test.png',0)
edges = cv2.Canny(img,100,200)
#contour = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print pytesseract.image_to_string(Image.open(edges))
print pytesseract.image_to_string(edges)
But this is giving error-
Traceback (most recent call last):
File "open.py", line 14, in
print pytesseract.image_to_string(edges)
File "/home/sroy8091/.local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 143, in image_to_string
if len(image.split()) == 4:
AttributeError: 'NoneType' object has no attribute 'split'
If you like to do some pre-processing using opencv (like you did some edge detection) and later on if you wantto extract text, you can use this command,
# All the imports and other stuffs goes here
img = cv2.imread('test.png',0)
edges = cv2.Canny(img,100,200)
img_new = Image.fromarray(edges)
text = pytesseract.image_to_string(img_new, lang='eng')
print (text)
You cannot use directly Opencv objects with tesseract methods.
Try:
from PIL import Image
from pytesseract import *
image_file = 'test.png'
print(pytesseract.image_to_string(Image.open(image_file)))

Convert CGImageRef to PIL

How could I convert a CGImageRef to PIL without saving the image to disk on osx?
I though about getting the raw pixel data from the CGImageRef and using Image.fromstring() to make the PIL image by doing
import mss
import Quartz.CoreGraphics as CG
from PIL import Image
mss = mss.MSSMac()
for i, monitor in enumerate(mss.enum_display_monitors(0)):
imageRef = mss.get_pixels(monitor)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(imageRef))
img = Image.fromstring("RGB", (monitor[b'width'], monitor[b'height']), pixeldata)
img.show()
but this doesn't give me the correct image.
This is the image I expect:
and this is the image I get in PIL:
The screencapture from CG doesn't necessarily use the RGB colorspace. It may use RGBA or something else. Try changing:
img = Image.fromstring("RGB", (monitor[b'width'], monitor[b'height']), pixeldata)
to
img = Image.fromstring("RGBA", (monitor[b'width'], monitor[b'height']), pixeldata)
Here is how I detect which colorspace is actually being captured:
bpp = CG.CGImageGetBitsPerPixel(imageRef)
info = CG.CGImageGetBitmapInfo(imageRef)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(imageRef))
img = None
if bpp == 32:
alphaInfo = info & CG.kCGBitmapAlphaInfoMask
if alphaInfo == CG.kCGImageAlphaPremultipliedFirst or alphaInfo == CG.kCGImageAlphaFirst or alphaInfo == CG.kCGImageAlphaNoneSkipFirst:
img = Image.fromstring("RGBA", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata, "raw", "BGRA")
else:
img = Image.fromstring("RGBA", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata)
elif bpp == 24:
img = Image.fromstring("RGB", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata)
It was a bug I fixed some time ago. Here is how to achieve what you want using the latest mss version (2.0.22):
from mss.darwin import MSS
from PIL import Image
with MSS() as mss:
for monitor in mss.enum_display_monitors(0):
pixeldata = mss.get_pixels(monitor)
img = Image.frombytes('RGB', (mss.width, mss.height), pixeldata)
img.show()
Note that pixeldata is just a reference to mss.image, you can use it directly.

Categories