I am taking a screenshot of a specific region and i want to extract the screenshot image to string but it is empty
Here is the code for grabing and extracting it to text:
import pyscreenshot as ImageGrab
from PIL import Image, ImageEnhance, ImageFilter
import pytesseract
Set the path to Tesseract executable
pytesseract.pytesseract.tesseract_cmd = r’C:/Program Files/Tesseract-OCR/tesseract.exe’
class MyUtils:
ROBOT_LIBRARY_SCOPE = ‘Global’
ROBOT_LIBRARY_VERSION = 1.0
def __init__(self):
print("Initializing MyUtils")
def image_grabs(self, x1, y1, x2, y2, image_name):
"""
Capture a screenshot of a specified region and save it to a file.
"""
im = ImageGrab.grab(bbox=(int(x1), int(y1), int(x2), int(y2)))
im.save(f"./Temp/{image_name}.png")
print(f"Saved screenshot as {image_name}.png")
def enhance_and_extract_text(self, image_path):
"""
Enhance the contrast of the image and extract text using OCR (Tesseract).
"""
try:
# Open the image
img = Image.open(image_path)
print(f"Original image mode: {img.mode}")
# Convert to grayscale
img = img.convert('L')
print(f"Converted image mode: {img.mode}")
# Enhance image contrast
enhancer = ImageEnhance.Contrast(img)
enhanced_img = enhancer.enhance(2.0) # Enhance contrast by a factor of 2
# Apply filter to remove noise
enhanced_img = enhanced_img.filter(ImageFilter.MedianFilter())
enhanced_img = enhanced_img.filter(ImageFilter.SHARPEN)
# Save enhanced image temporarily
enhanced_img_path = image_path.replace('.png', '_enhanced.png')
enhanced_img.save(enhanced_img_path)
print(f"Enhanced image saved to: {enhanced_img_path}")
# Extract text using Tesseract OCR
extracted_text = pytesseract.image_to_string(enhanced_img, config='--psm 6 -l eng') # Use page segmentation mode 6 (Assume a single uniform block of text)
cleaned_text = extracted_text.strip()
print(f"Extracted text: {cleaned_text}")
# Return cleaned text
return cleaned_text
except Exception as e:
print(f"Error occurred: {e}")
return ""
Any suggestion?
I take a screenshot enhanced the screenshot image and then extracted to text using OCR
Faiz Imran Cotongan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.