I'm written python code to convert some images into string. I have the images of some mobile numbers in png format.
But i got only one converted into text but others are not converting.
Here is my code :
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import os
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'images/3564.jpg')
def ocr_core(filename):
"""
This function will handle the core OCR processing of images.
"""
text = pytesseract.image_to_string(Image.open(filename)) # We'll use Pillow's Image class to open the image and pytesseract to detect the string in the image
return text
for x in range(24):
number = ocr_core('/Users/evilslab/Documents/Websites/www.futurepoint.dev.cc/dobuyme/python/images/'+str(x)+'.png')
print ("The number is "+number)
I have 24 images and i'm getting the value of the 9th image.
This image is working :
This not working
Why it happens ?
Sometimes image need some work to improve its quality.
See Tesseract Wiki: Improving the quality of the output
In your example I had to only resize image at least 120% to get numbers.
from PIL import Image
import pytesseract
import os
folder = os.path.dirname(os.path.abspath(__file__))
def ocr_core(filename):
image = Image.open(filename)
w, h = image.size
#image = image.resize((int(w*1.2), int(h*1.2))) # 120%
image = image.resize((w*2, h*2)) # 200%
#text = pytesseract.image_to_string(image, config='-c tessedit_char_whitelist="0123456789+"')
text = pytesseract.image_to_string(image)
text = text.replace(' ', '')
return text
for filename in os.listdir(folder):
if filename.endswith('.png'):
number = ocr_core(os.path.join(folder, filename))
print("number:", number)
EDIT: it recognizes number even without resizing when I use option pms=7 which means "Treat the image as a single text line." (see Page segmentation method)
text = pytesseract.image_to_string(image, config='--psm 7')
Related
I need to provide AADHAAR card photo and need to extract the Data on AADHAAR and display in JSON with masking partial Unique AADHAAR Number
from PIL import Image
from pytesseract import pytesseract
#Define path to tessaract.exe
path_to_tesseract = r'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
#Define path to image
path_to_image = 'C:/Users/PJ185101/Documents/images/sample2.jpg'
#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract
#Open image with PIL
img = Image.open(path_to_image)
#Extract text from image
text = pytesseract.image_to_string(img)
print(text)
I'd like to convert multi image TIFF to PDF in python.
I wrote like this code. How ever this code dose not work. How should I change it?
images = []
img = Image.open('multipage.tif')
for i in range(4):
try:
img.seek(i)
images.append(img)
except EOFError:
# Not enough frames in img
break
images[0].save('multipage.pdf',save_all=True,append_images=images[1:])
I solved this problem. You can convert tiff to pdf easily by this function.
from PIL import Image, ImageSequence
import os
def tiff_to_pdf(tiff_path: str) -> str:
pdf_path = tiff_path.replace('.tiff', '.pdf')
if not os.path.exists(tiff_path): raise Exception(f'{tiff_path} does not find.')
image = Image.open(tiff_path)
images = []
for i, page in enumerate(ImageSequence.Iterator(image)):
page = page.convert("RGB")
images.append(page)
if len(images) == 1:
images[0].save(pdf_path)
else:
images[0].save(pdf_path, save_all=True,append_images=images[1:])
return pdf_path
You need install Pillow, when you use this function.
In a part of my project for creating a dataset, I have a text file containing a list of a bunch of latex equations . Now I want to convert them into images through python in diffrent font sizes. But i dont know how to do it. Please help.
This is the list of latex symbols I am using:- https://docs.mathpix.com/#vocabulary
#
import shutil
import os
from pdflatex import PDFLaTeX
from pdf2image import convert_from_path
from PIL import Image
def crop(file):
img = Image.open(file)
area = (300,300, 800, 800)
cropped_img = img.crop(area)
cropped_img.save(file)
def save_images(images_names,pdf_path,images_path=""):
# Store Pdf with convert_from_path function
images = convert_from_path(pdf_path)
if len(images_names)==0:
print("names is empty")
return
i=0
for img in images:
img.save(images_path+"/"+images_names[i]+".jpg", 'JPEG')
crop(images_path+"/"+images_names[i]+".jpg")
i+=1
print("Successfully converted")
def create_image_from_latex(image_name,latex):
if "rough" not in os.listdir():
os.mkdir("rough")
if "images_from_latex" not in os.listdir():
os.mkdir("images_from_latex")
f=open("rough/a.tex","w+")
f.write("\\documentclass{article}\n\\usepackage{chemfig}\n\\begin{document}\n")
f.write(latex+"\n")
f.write(r"\end{document}")
f.close()
#print(os.getcwd()+"/a.tex")
#tex="/a.tex"
pdfl = PDFLaTeX.from_texfile('rough/a.tex')
pdf, log, completed_process = pdfl.create_pdf(keep_pdf_file=True, keep_log_file=False)
f=open("rough/a.pdf","wb")
f.write(pdf)
f.close()
save_images([image_name],"a.pdf","images_from_latex")
os.remove("rough/a.pdf")
shutil.rmtree("rough")
#create_image_from_latex("new_image",lat)
def create_images_from_text_file_with_latexes(text_file):
with open(text_file) as f:
latexes=f.readlines()
ind=1
for lat in latexes:
create_image_from_latex("%0.3d_"%ind,lat)
ind+=1
#
While trying to split the path to get a name, I get the traceback:
TypeError: expected str, bytes or os.PathLike object, not JpegImageFile . How can I solve it or is there other methods?
I try to save resized images with the same name to a different direction. For this reason, I use os.path.split() functions.
import glob
from PIL import Image
import os
images = glob.glob("/Users/marialavrovskaa/Desktop/6_1/*")
path = "/Users/marialavrovskaa/Desktop/2.2/"
quality_val=95
for image in images:
image = Image.open(image)
image.thumbnail((640, 428), Image.ANTIALIAS)
image_path_and_name = os.path.split(image)
image_name_and_ext = os.path.splitext(image[0])
name = image_name_and_ext[0] + '.png'
name = os.path.splitext(image)[0] + '.png'
file_path = os.path.join(path, name)
image.save(file_path, quality=quality_val)
import glob
from PIL import Image
import os
images = glob.glob("Source_path")
path = r"Destination_path"
quality_val=95
for image in images:
img = Image.open(image)
img.thumbnail((640, 428), Image.ANTIALIAS)
name = os.path.split(image)
file_path = os.path.join(path, name[1])
img.save(file_path, quality=quality_val)
The primary problem with your code was that you were using a variable and an object under the same name image. Which was causing problems.
LOGICAL ERRORS:-
image_path_and_name is a needless variable in the code, as it is
used for nothing.
name has been initialized with totally different values twice,
instead use name = os.path.split(image) which serves the purpose
of both.
you should not try to explicitly define the extension of each image
as .png as it might create issues when dealing with other image
formats.
for image in images:
image = Image.open(image)
image.thumbnail((640, 428), Image.ANTIALIAS)
image_path_and_name = os.path.split(image)
When you say image = Image.open(image), you're overwriting the loop variable also named image, and it's no longer a splittable string.
Change one of the image variables to a different name.
firstly,
image_name_and_ext = os.path.splitext(image[0])
should be
image_name_and_ext = os.path.splitext(image_path_and_name[1])
because image is string so image[0] just get first character of image not useless in this case
secondly,
name = os.path.splitext(image)[0] + '.png' equal image
name = os.path.splitext(image)[0] should return path of image not include extention
to solve this problem, you can try:
for image in images:
img = Image.open(image)
img.thumbnail((640, 428), Image.ANTIALIAS)
name = os.path.split(image)
file_path = os.path.join(path, name[1])
image.save(file_path, quality=quality_val)
By the code above you will get image in thumbnails I recommend to use resize function because from resize you can maintain you aspect ratio that helps you in getting better results.
image.thumbnail((640, 428), Image.ANTIALIAS)
this line of code is converting your thumbnail.which is not good approach of resizing. Try the code below.
import os
from PIL import Image
PATH = "F:\\FYP DATASET\\images\\outliers Dataset\\Not Outliers\\"
Copy_to_path="F:\\FYP DATASET\\images\\outliers Dataset\\"
for filename in os.listdir(PATH):
img = Image.open(os.path.join(PATH, filename)) # images are color images
img = img.resize((224,224), Image.ANTIALIAS)
img.save(Copy_to_path+filename+'.jpeg')
In this code you are taking images directly from folder resize them and saving them to other location with same name. All the images are processing one by one So you need not worry to load all the images at once into the memory.
I am using the following code to read multiple images from a folder and to take a specific crop from all of them and also write them at the end. The cropping part does not seem to work meaning the cropped parts are not getting written.
# import the necessary packages
import cv2
import os, os.path
#image path and valid extensions
imageDir = "C:\\Users\\Sayak\\Desktop\\Training\\eye\\1" #specify your path here
image_path_list = []
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your vald extensions here
valid_image_extensions = [item.lower() for item in valid_image_extensions]
#create a list all files in directory and
#append files with a vaild extention to image_path_list
for file in os.listdir(imageDir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(imageDir, file))
#loop through image_path_list to open each image
for imagePath in image_path_list:
image = cv2.imread(imagePath)
# display the image on screen with imshow()
# after checking that it loaded
if image is not None:
cv2.imshow(imagePath, image)
elif image is None:
print ("Error loading: " + imagePath)
#end this loop iteration and move on to next image
continue
crop_img = image[200:400, 100:300]
cv2.imwrite('pic{:>05}.jpg'.format(imagePath), crop_img)
# wait time in milliseconds
# this is required to show the image
# 0 = wait indefinitely
# exit when escape key is pressed
key = cv2.waitKey(0)
if key == 27: # escape
break
# close any open windows
cv2.destroyAllWindows()
I have modified your code posted above. The problem lies with your string formatting used in cv2.imwrite(). The first argument must be an absolute path to where the file should be saved, but your code is passing in something like this.
pic{:>05}.jpg => pic/home/ubuntu/Downloads/10_page.png.jpg. When I used a valid file name there is no problem saving the cropped image at all.
#loop through image_path_list to open each image
for i,imagePath in enumerate(image_path_list):
image = cv2.imread(imagePath)
# display the image on screen with imshow()
# after checking that it loaded
if image is not None:
cv2.imshow(imagePath, image)
elif image is None:
print ("Error loading: " + imagePath)
#end this loop iteration and move on to next image
continue
# Please check if the size of original image is larger than the pixels you are trying to crop.
(height, width, channels) = image.shape
if height >= 400 and width >= 300:
plt.imshow(image)
plt.title('Original')
plt.show()
crop_img = image[100:400, 100:400]
# Problem lies with this path. Ambiguous like pic/home/ubuntu/Downloads/10_page.png.jpg
print('pic{:>05}.jpg'.format(imagePath))
# Try a proper path. A dirname + file_name.extension as below, you will have no problem saving.
new_image = os.path.join(imageDir,'{}.png'.format(str(i)))
cv2.imwrite(new_image, crop_img)
plt.imshow(crop_img)
plt.title('Cropped')
plt.show()