I am taking a screenshot, and then I need to reference the shot I just took so I can translate what's inside it.
When I directly pass a file location, e.g "filex.png", to readtext, it works, but I just need it to pass the written file into it.
import easyocr
import pyautogui
import time
import cv2
import numpy as np
reader = easyocr.Reader(['en'])
tag = 1
for i in range(2):
time.sleep(4)
image = pyautogui.screenshot(region=(630,400,650,130))
image = cv2.cvtColor(np.array(image),
cv2.COLOR_RGB2BGR)
tag+=1
img = cv2.imwrite(f"image{tag}.png", image)
results = reader.readtext(img)
text=""
for result in results:
text += result[1] + " "
print(text)
In answer to your specific question, I think you're looking for something like:
import easyocr
import pyautogui
import time
import cv2
import numpy as np
reader = easyocr.Reader(['en'])
tag = 1
for i in range(2):
time.sleep(4)
image = pyautogui.screenshot(region=(630, 400, 650, 130))
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
tag += 1
f_name = f"image{tag}.png"
cv2.imwrite(f_name, image)
results = reader.readtext(f_name)
text = ""
for result in results:
text += result[1] + " "
print(text)
You can just store your file name in a variable in pass it to both imwrite and readtext
There are other options as well, depending on what information you need access to within the program, and how quickly you need to process your data.
Option: Pass the np.array directly to readtext
image = pyautogui.screenshot(region=(630, 400, 650, 130))
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
results = reader.readtext(image)
Option: Pass the data from the written file to the readtext function.
f_name = f"image{tag}.png"
cv2.imwrite(f_name, image)
with open(f_name, 'rb') as f:
results = reader.readtext(f.read())
import pyautogui
import easyocr
import numpy as np
reader = easyocr.Reader(['en'],gpu=False)
im = pyautogui.screenshot(region=(630,400,650,130)
result = reader.readtext(np.array(im),detail = 0)
just pass the pyautogui image as np.array
Related
i am trying to do a program where it detects the video and capture every 100th frame then display the output. the problem that i faced here is that there will be duplication for example, the current pattern for the output is (1,1,1,2,2,3,3,2,1) but the result that i want to show is (1,2,3,2,1). To sum up, not displaying the same text continuously but allowed to be displayed when the previous image is different with the current ones. I have tried coming up with an idea of removing duplicate images but it will not give the expected result as it will delete all the images.
from PIL import Image
import pytesseract
from wand.image import Image as Img
import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
import numpy as np
import os
import cv2
image_frames = 'image_frames'
def files():
#auto create image frames file
try:
os.remove(image_frames)
except OSError:
pass
if not os.path.exists(image_frames):
os.makedirs(image_frames)
# specify the source video path
src_vid = cv2.VideoCapture('15sec.mp4')
return(src_vid)
def process(src_vid):
# Use an index to integer-name the files
index = 0
while (src_vid.isOpened()):
ret, frame = src_vid.read()
if not ret:
break
# name each frame and save as png / jpeg
name = './image_frames/frame' + str(index) + '.jpeg'
if index % 100 == 0:
print('Extracting frames...' + name)
cv2.imwrite(name, frame)
index = index + 1
if cv2.waitKey(10) & 0xFF == ord('q'):
break
src_vid.release()
cv2.destroyAllWindows()
# do image to text on each png
def get_text():
for i in os.listdir(image_frames):
print(str(i))
my_image = Image.open(image_frames + "/" + i)
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USERS\AppData\Local\Tesseract-OCR\tesseract.exe'
text = pytesseract.image_to_string(my_image, lang='eng')
print(text)
# main driver
if __name__ == '__main__':
vid = files()
process(vid)
get_text()
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 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.
I want to use pillow for saving Image and loading it.
I know I can do Image.save(imagename.xxx).
But I want to save as the contents of images.
and I want to reuse it .
from PIL import Image
import numpy as np
filename = 'any_image.png'
import pickle
im = Image.open(filename)
data = list(im.getdata())
f = open("test_file.dat","wb")
dumps = pickle.dump(data,f)
f = open("test_file.dat","rb")
tumps = pickle.load(f)
print(np.asarray(tumps))
#here
re_im = Image.Image.putdata(tumps)
re_im.show()
I want to show re_im Image Object, it is the same contents I saved before.
I could save & load of the same contents by pickle.
But I don't know where I send the contents.
I want to show the same as original image by re_im.show()
please help me.
I could make it.
first, to omit 'data = list(im.getdata())'
second, to insert 'np.array(im)'
from PIL import Image
import numpy as np
filename = 'any_data.png'
import pickle
im = Image.open(filename)
data = np.array(im)
f = open("test_file.dat","wb")
print(type(data))
dumps = pickle.dump(data,f)
f = open("test_file.dat","rb")
tumps = pickle.load(f)
array = Image.fromarray(tumps)
array.show()
Good day.
I am trying to create a for loop to read the lines of a file until a condition is met so it can write these lines in a image and i plan to do this with every line of the file, like the example below:
Number: 123456789
Connecting to Database
no rows selected
Disconnecting from Database
Number: 9876543211
Connecting to Database
1111;48446511911986;Helen;Thursday
2222;48498489489489;Helen;Friday
3333;84545221185986;Helen;Monday
Disconnecting from Database
Number: 963852741
Connecting to Database
1234;123456789456123;Clyde;Friday
4321;123456789456123;Clyde;Thuesday
1423;123456789456123;Clyde;Sunday
2341;123456789456123;Clyde;Friday
Disconnecting from Database
Number: 456987321
Connecting to Database
no rows selected
Disconnecting from Database
As you can see every time the word Database shows up for the second time the next line is about a new number information, so i tried using the word database as a parameter for the loop below.
import os
import PIL
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
img = Image.open("C:/Users/dir/image/black_background.png")
draw = ImageDraw.Draw(img)
fonts_dir = os.path.join(os.environ['WINDIR'], 'Fonts')
font_name = 'consolab.ttf'
font = ImageFont.truetype(os.path.join(fonts_dir, font_name), 15)
x = 2
y = 0
next_print_count = 0
filename = "info.txt"
Number = ""
for line in open(filename):
if 'Number:' in line:
Number= line.split(" ",1)[1].strip()
if 'Testing ' in line:
line = ""
draw.text((x, y),line,(200,200,200),font=font)
y += 15
img.save(Number + ".png")
Problem is that every time it starts a new file it also prints the information from the previous lines as well. How do i avoid that?
I also tried to use NUMBER as a parameter as well but it didn't work.
you need to delete the current img and draw object every time line is "Disconnecting from Database" and make new objects after deleting them. In your original code, you were also saving the image every line, which is not good either. See the code below.
import os
import PIL
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
fonts_dir = os.path.join(os.environ['WINDIR'], 'Fonts')
font_name = 'consolab.ttf'
font = ImageFont.truetype(os.path.join(fonts_dir, font_name), 15)
x = 2
y = 0
next_print_count = 0
filename = r'info.txt'
Number = ""
with open(filename) as f:
img = Image.open("C:\Users\Public\Pictures\Sample Pictures/Chrysanthemum.jpg")
draw = ImageDraw.Draw(img)
for line in f:
if 'Number:' in line:
Number= line.split(" ",1)[1].strip()
if 'Testing ' in line:
line = ""
draw.text((x, y),line,(200,200,200),font=font)
y += 15
if 'Disconnecting from Database' in line:
img.save(Number + ".png")
del draw, img
img = Image.open("C:\Users\Public\Pictures\Sample Pictures/Chrysanthemum.jpg")
draw = ImageDraw.Draw(img)
y=0
results in (only showing two samples images here, but 4 are created)