I wrote a simple function in my app, which converts certain very simple images containing numbers into numbers . thats it ! . but if image has 8 its show's 3 . any help ? this is image example
also i tried convert my image also to sharpness . its not work perfectly for other numbers . how i can fix it ?
import tkinter
from tkinter import font
from pyasn1.type.univ import Null
import requests
from tkinter import *
import tkinter as tk
import sys
import os
from PIL import Image
from pytesseract import pytesseract
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
path_to_tesseract =resource_path(r"Tesseract-OCR\\tesseract.exe")
Startbalance_window = Tk()
#Setting title of screen
Startbalance_window.title("SS")
#setting height and width of screen
Startbalance_window.geometry("500x180")
Startbalance_window.configure(background=("#091c2d"))
path_to_tesseract =resource_path(r"Tesseract-OCR\\tesseract.exe")
pytesseract.tesseract_cmd = path_to_tesseract
def get_wallet():
urlentryGP_text = urlentry_GP.get()
pytesseract.tesseract_cmd = path_to_tesseract
imgURL_GP= Image.open(requests.get(urlentryGP_text, stream=True).raw)
response = requests.get(urlentryGP_text)
width, height = imgURL_GP.size # get image size
imgURL_GP = imgURL_GP.resize((800, int(800/(width/height))), Image.ANTIALIAS)
im_s_1 = imgURL_GP.convert("L") #black&white
custom_config = r'--oem 3 --psm 6 outputbase digits'
query = pytesseract.image_to_string(im_s_1, lang='eng+fra',nice=1,config=custom_config) # getting all text in the image
image_list2 = query.split()
OSBALANCE_image21 = image_list2[-1].replace(',',"") #getting last value and remove comma from it
OSBALANCE_image21 = OSBALANCE_image21.replace('#',"0")
OSBALANCE_image21 = OSBALANCE_image21.replace(';',"")
OSBALANCE_image21 = OSBALANCE_image21.replace(':',"")
OSBALANCE_image21 = OSBALANCE_image21.replace('/',"")
OSBALANCE_image21 = OSBALANCE_image21.replace('L',"")
OSBALANCE_image21 = OSBALANCE_image21.replace('J',"")
OSBALANCE_image21 = OSBALANCE_image21.replace('I',"")
print(OSBALANCE_image21)
sum_balance_text.set(f" Your Balance is: {OSBALANCE_image21} ")
sum_balance.place(x=265,y=120)
global urlentry_GP
urlentry_GP = StringVar()
URL_Entry_GP = Entry(Startbalance_window, textvariable=urlentry_GP).place(x=300,y=50)
submit_imagebutton= Button(Startbalance_window, text="Extract Balance", width=10, height=1, bg="orange",command=get_wallet,font='lato 8 bold',cursor="hand2")
submit_imagebutton.place(x=290,y=80, width=148,height=20)
global sum_balance_text
sum_balance_text = StringVar()
sum_balance=Label(Startbalance_window,textvariable=sum_balance_text,bg="#091c2d",fg="white",font=("default",9,"bold"))
Startbalance_window.resizable(0,0)
Startbalance_window.mainloop()
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'bin/tesseract.exe'
tessdata = 'tessdata'
filename = 'WENWg.png'
im = Image.open(filename)
width, height = im.size
scale_factor = 3
im_scaled = im.resize((width*scale_factor, height*scale_factor))
print(pytesseract.image_to_string(im_scaled, config=f'--psm 6 --tessdata-dir "{tessdata}"').replace('\n\f', ''))
Output:
Total quide price:
123,802,858
Related
I am trying to write a program that reads in a folder of photos, analyses their heights and widths, and resizes them accordingly, then sends them to a word document. I keep getting this error and I am unsure what is causing it:
from docx import Document
import cv2
from PIL import Image
import glob
import os
import numpy
document = Document()
img_dir = "C:/Users/27832/Desktop/Report Images"
data_path = os.path.join(img_dir,'*g')
files = glob.glob(data_path)
photos = []
for pic in files:
imagg = cv2.imread(pic)
photos.append(imagg)
for i in range(0, len(photos)):
if 0.85*(photos[i].shape[0]) < (photos[i].shape[1]) < 1.15*(photos[i].shape[0]):
resized_image = photos[i].resize((314, 314))
document.add_picture(resized_image)
elif (photos[i].shape[1]) >= 0.85*(photos[i].shape[0]):
resized_image = photos[i].resize((257, 382))
document.add_picture(resized_image)
elif (photos[i].shape[1]) <= 1.15*(photos[i].shape[0]):
resized_image = photos[i].resize((401, 325))
document.add_picture(resized_image)
document.save("C:/Users/27832/Desktop/Word Macro Program/Report.docx")
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
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
I am very new to Python. I just started today.
I am trying desperately to save an image to a fixed path, such as:
/Users/myname/Sites/Tester/images/
So if I have an image, called "1.jpg", it will be placed here:
/Users/myname/Sites/Tester/images/1.jpg
This is my script:
from PIL import Image
import tempfile
def set_image_dpi(file_path):
im = Image.open(file_path)
length_x, width_y = im.size
factor = min(1, float(1024.0 / length_x))
size = int(factor * length_x), int(factor * width_y)
im_resized = im.resize(size, Image.ANTIALIAS)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
temp_filename = temp_file.name
im_resized.save(temp_filename, dpi=(300, 300))
return temp_filename
However, this saves the file in:
/var/folders/1n/hdyfv8z96v5_hcb9tsgvt7cr0000gn/T/tmp91rams5v.jpg
How can I do, so it will save in the path I specifies?
"MY_PATH / temp_filename"
No need for the tempfile module here, you just need to specify the path when calling .save():
from PIL import Image
import os
def set_image_dpi(file_path, save_folder):
im = Image.open(file_path)
length_x, width_y = im.size
factor = min(1, float(1024.0 / length_x))
size = int(factor * length_x), int(factor * width_y)
im_resized = im.resize(size, Image.ANTIALIAS)
save_path = os.path.join(save_folder,'test.png')
# creates path: C:\Users\User\Pictures\test.png
im_resized.save(save_path, dpi=(300, 300))
set_image_dpi('test.png','C:\\Users\\User\\Pictures')
I'm guessing your main problem is building the result path from the original path (something like /where/images/are/taken/from/1.jpg) and your destination directory (/Users/myname/Sites/Tester/images/). The methods in the os.path package can help (see https://docs.python.org/3/library/os.path.html):
import os
dest_dir = '/Users/myname/Sites/Tester/images/'
...
base = os.path.basename(file_path) # this will be '1.jpg'
dest_path = os.path.join(dest_dir, base) # this will be the full path
im_resized.save(dest_path)
As others have said in the comments, you don't need tempfile here.
I have program which converts pdf to excel, Now i want add multiple inputs i.e. multiple pdfs to be converted one by one.
my code is below:
from PIL import Image
import io
import pytesseract
from wand.image import Image as wi
import os
import cv2
import pandas as pd
import re
import numpy as np
import os
pdf = wi(filename= "pdfs/jaalna.pdf", resolution =300)
pdfImage = pdf.convert("jpg")
imageBlobs = []
for img in pdfImage.sequence:
imgPage = wi(image = img)
#img.filter(ImageFilter.EDGE_ENHANCE_MORE )
imageBlobs.append(imgPage.make_blob('jpg'))
recognized_text = []
for imgBlob in imageBlobs:
im = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(im, lang = 'eng1+mar1')
recognized_text.append(text)
newfile = open('aama.txt','w')
newfile.write(",".join(recognized_text))
#add a folder as input.
you can use loop
for name in ["pdfs/jaalna.pdf", "other/file.pdf"]:
pdf = wi(filename=name, resolution=300)
# rest of code
or you can use sys.argv to get names as
script.py pdfs/jaalna.pdf other/file.pdf other/third.pdf
and code
import sys
for name in sys.argv[1:]:
pdf = wi(filename=name, resolution=300)
# rest of code
Try the code below. This will loop through every PDF file in the folder directory you define. Be sure to update your file_path to be where your PDFs are saved, making sure you use double backslashs in place of single backslashes.
from PIL import Image
import io
import pytesseract
from wand.image import Image as wi
import cv2
import pandas as pd
import re
import numpy as np
import os
file_path = "C:\\Users\\..."
for file in os.listdir(file_path):
if file.endswith(".pdf"):
pdf = wi(file, resolution =300)
pdfImage = pdf.convert("jpg")
imageBlobs = []
for img in pdfImage.sequence:
imgPage = wi(image = img)
#img.filter(ImageFilter.EDGE_ENHANCE_MORE )
imageBlobs.append(imgPage.make_blob('jpg'))
recognized_text = []
for imgBlob in imageBlobs:
im = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(im, lang = 'eng1+mar1')
recognized_text.append(text)
newfile = open(file+'.txt','w')
newfile.write(",".join(recognized_text))
#add a folder as input.