Python Pytesseract - unable to read the image - python

I am new to pyteseract OCR. I was able to read simple pic and get the test message. I am unable to read test from attached png file. From the attached image, i am trying to extract ishSilver and (SLV) in text.
I would appreciate it if you could help on this. Thank you in advance.
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract-OCR\tesseract.exe' # actual path to tesseract
im = Image.open('Images/' + '1680.png')
# Size of the image in pixels (size of orginal image)
# (This is not mandatory)
width, height = im.size
# Setting the points for cropped image
left = width*.88
top = 0
right = width
bottom = height
# Cropped image of above dimension
# (It will not change orginal image)
im1 = im.crop((left, top, right, bottom))
# Capturing the image in image viewer and converting to text
text = pytesseract.image_to_string(im1, lang="eng")
print(text)
print("/n")
# Finding the "(" and ")" to the actual stock ticker and save the text in symbol for placeorder to access the file
ticker = text[text.find("(")+1:text.find(")")]
print(ticker)
enter image description here
enter image description here
enter image description here

Related

How to remove boxes around shx text without AutoCAD?

I try to use OCR (Optical Character Reader) for a lot of documents of the same type. I use pdf2image library for Python. But when it sees pdfs with AutoCAD shx text it captures the bounding boxes around text as well. At first they are not visible on pdf. You need to click on text to see the boxes. But they appear in jpg result after conversion.
Here's an image of a part of pdf document:
crop from actual pdf
And here's the output of conversion: pdf after conversion
I expect somethink like that:
pdf after conversion how it should be
Here's my function for pdf2image conversion:
def get_image_for_blueprint(path, dpi):
"""Create image for full blueprint from path
path: path to pdf file
"""
from pdf2image import convert_from_bytes
images = convert_from_bytes(open(path, 'rb').read(), dpi=dpi) # Actual conversion function
for i in images:
width, height = i.size
if width < height:
continue
else:
print(i.size) # tuple : (width, height)
image = i.resize((4096, int(height / width * 4096)))
enhancer = ImageEnhance.Sharpness(image)
image = enhancer.enhance(2) # Sharpness
enhancer = ImageEnhance.Color(image)
image = enhancer.enhance(0) # black and white
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(2) # Contrast
image = np.asarray(image) # array
image = image.astype(np.uint8)
return image
I found solutions to be made in AutoCAD before saving the document but I can not really find a way to get rid of these boxes having pdf only.(in python or c++)
Maybe it's possible to resolve using any other programming language library or additional software.

python pillow when I open a portrait (height greater than width) image, the image is rotated ccw 90 degrees

When I open an image which is higher than width (portrait), the image
is rotated 90 degrees ccw.
from PIL import ImageTk, Image
from pathlib import Path
wd = Path.cwd()
fn = wd / 'sample.jpg'
path = str(fn)
img = Image.open(path)
img.show()
A couple of things. Apparently show saves the image as a .png temp file, and that save loses the orientation information that is in the exif data of the jpg image. I also had trouble with rotate. There is some inconsistency in the Pillow image functions. e.g. thumbnail updates the supplied image and return None.
rotate rotates the image and returns the changed image.
fimage.thumbnail((700,700), Image.ANTIALIAS)
simage = fimage.rotate(270)
# update. Transpose will use EXIF data if available to
# rotate the image to the correct orientation
from PIL import ImageOps
img = ImageOps.exif_transpose(img)
Reference: https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.exif_transpose

How to find longest uninterrupted edge using canny?

I have a problem with canny edge detection in python.
my starting picture is a normal picture of a bill like I have added in the post.
I detect the edges with canny without any problems but I want to know where the longest detected edges are in the picture.
Here is the input image
and here is the output image.
Now I want to know the longest edge in the output image to cut of the unnecessary parts of the picture, but I do not know how...
My Code:
# Read reference image
#refFilename = RefRefFileCalculation(row)
refFilename = "C:\\Maturaprojekt\\Test 1\\30Zeilen.JPG"
print("Reading reference image : ", refFilename)
imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)
# Read image to be aligned
imFilename = save_path
print("Reading image to align : ", imFilename);
im = cv2.imread(imFilename, cv2.IMREAD_COLOR)
print("Aligning images ...")
# Registered image will be resotred in imReg.
# The estimated homography will be stored in h.
imReg, h = alignImages(im, imReference)
# Write aligned image to disk.
outFilename = "C:\\Maturaprojekt\\Test 1\\gedreht\\" + sample_names[i]
print("Saving aligned image : ", outFilename);
cv2.imwrite(outFilename, imReg)
# Load the image file
image = cv2.imread(save_path)
# Check if image was loaded improperly and exit if so
if image is None:
sys.exit('Failed to load image')
# Detect edges in the image. The parameters control the thresholds
edges = cv2.Canny(image, 100, 700, apertureSize=5)
# Display the output in a window
cv2.imwrite("C:\\Maturaprojekt\\Edge Detection Test 1\\cropped\\" + sample_names[i], edges)
i = i+1

Python error while cropping images: tile cannot extend outside image

I have a folder of JPG images having size 2048x1536. In every image date, time, temperature are given in the top and camera model name is given at the end. I want to crop only upper part and lower part of those image.
Image sample: https://drive.google.com/file/d/1TefkFws5l2RBnI2iH22EI5vkje8JbeBk/view?usp=sharing
With the below code, I am getting error -tile cannot extend outside image **for any size I provide, for example (500,500,500,500) **. My target it (1500, 2000, 1500, 2000)
from PIL import Image
import os
#Create an Image Object from an Image
dir=r"C:\\Users\\Desktop\\crop1"
output_dir = r"C:\\Users\\Desktop\\crop2"
file_names = os.listdir(dir)
for file_name in file_names:
file_path = dir +"\{}".format(file_name)
im = Image.open(r"{}".format(file_path))
cropped = im.crop((2000,1500,2000,1500))
output_file= output_dir+"\{}".format(file_name)
cropped.save(r"{}".format(output_file))
"(2000,1500,2000,1500)" is an empty box, which could explain why crop fails even if the "tile cannot extend outside image" error isn't exactly fitting. The 4-tuple argument to crop has the meaning "(left, upper, right, lower)". Example from the Image.crop() documentation:
from PIL import Image
im = Image.open("hopper.jpg")
# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (20, 20, 100, 100)
# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))

how can I put multiple images and texts on a background image using Python PIL such that texts don't overlap with image?

I'm trying to put multiple images and texts on top of the background image. Making use of python PIL image library. The number of images and text printing won't need to be the same always. Text and image printing has to be separate. The text has to be printed outside anywhere image bounding box.
I'm using below code to make this happen
from PIL import Image
import os, random
with open('C:/Users/nike/Desktop/namelist.txt', "r") as word_list:
words = list(word_list)
k=[]
for i in words:
j = i.replace(' ','').replace('\n','')
k.append(j)
folder=r"C:/Users/nike/Desktop/imagefolder"
a=random.choice(os.listdir(folder))
file = folder+'//'+a
random_text=random.choice(k)
img = Image.open(file)
img_w, img_h = img.size
background = Image.open('C:/Users/nike/Desktop/backgroundimages/back.jpeg','r')
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
draw = ImageDraw.Draw(background)
background.paste(img, offset)
font = ImageFont.truetype("C:/Users/nike/Desktop/open-sans/abc.ttf", 16)
draw.text((0, 0),random_text,(255,255,255),font=font)
background.save('out.png')
above code does printing of one image at the center of background image and text at the (0,0) coordinate of background image. How can I make multiple text and images to paste on background images such that text(x,y) do not print on image (x,y). Any suggestion will be helpful.
Example:
expected result
on the background image, I need to copy images and texts such that they won't overlap eachother.

Categories

Resources