Convert Text Image to Hand Written Text Image using Python - GeeksforGeeks

29 Jun, 2021
In this article, we are going to see how to convert text images to handwritten text images using PyWhatkit, Pillow, and Tesseract in Python.
Pytesseract: Sometimes known as Python-tesseract, is a Python-based optical character recognition (OCR) program. It can read and recognize text in photos, license plates, and other documents. To interpret the words from the provided image, we’ll utilize the tesseract software.
pip install pytesseract
Pywhatkit: It is a library that may be used for a variety of things, including sending WhatsApp messages, watching YouTube videos, searching Google, and writing handwritten text.
pip install pywhatkit
Pillow: This module adds more features, operates on all major operating systems, and has Python 3 support. It supports a broad range of image formats, including “jpeg,” “png,” “bmp,” “gif,” “ppm,” and “tiff.” With the pillow module, you can do nearly anything with digital photographs.
pip install Pillow
Note: Visit and Install Tesseract; scroll down to find the latest installers for 32-bit and 64-bit systems; download them as needed.
Step 1: Import the following modules.
Python3
import pytesseract from PIL import Image import osimport pywhatkit as kit
Step 2: Navigate to the path where the picture is located, the chdir function in the OS module can be used to alter the directory.
Python3
os.chdir(r"C:\Users\Dell\Downloads")
Step 3: Copy the installation path for Tesseract, paste it here, or verify the directory of Tesseract and copy the entire path.
Python3
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\Dell\AppData\Local\/Tesseract-OCR\tesseract.exe"
Step 4: First, open the image with the image function, then use pytesseract to get all the image’s data, and store all the text in a variable.
Python3
img = Image.open("GFG.png")text = pytesseract.image_to_string(img)
Step 5: Use the text _to_handwriting function from pywhatkit to convert text to the specified RGB color; in this case, the RGB for blue is 0, 0, 250.
Python3
kit.text_to_handwriting(text, rgb=[0, 0, 250])
Below is the complete implementation:
Python3
# Import the following modulesimport pytesseractfrom PIL import Imageimport osimport pywhatkit as kit  # Change the directory to the# location where image is presentos.chdir(r"C:\Users\Dell\Downloads")  # Set the Path of Tesseractpytesseract.pytesseract.tesseract_cmd = r"C:\Users\Dell\AppData\/Local\Tesseract-OCR\tesseract.exe"  # Load the Imageimg = Image.open("GFG.png")    # Convert Image to Texttext = pytesseract.image_to_string(img)    # Convert Text to Hand Written Textkit.text_to_handwriting(text, rgb=[0, 0, 250])
Output:
Image-Processing
python-modules
Python-pil
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
Defaultdict in Python
Selecting rows in pandas DataFrame based on conditions
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()