Skip to content
Related Articles

Related Articles

Improve Article

Convert Text Image to Hand Written Text Image using Python

  • Difficulty Level : Medium
  • Last Updated : 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.

Module needed:

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.



Stepwise implementation:

Step 1: Import the following modules.

Python3




import pytesseract 
from PIL import Image 
import os
import 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 modules
import pytesseract
from PIL import Image
import os
import pywhatkit as kit
  
# Change the directory to the
# location where image is present
os.chdir(r"C:\Users\Dell\Downloads")
  
# Set the Path of Tesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\Dell\AppData\/
Local\Tesseract-OCR\tesseract.exe"
  
# Load the Image
img = Image.open("GFG.png")  
  
# Convert Image to Text
text = pytesseract.image_to_string(img)  
  
# Convert Text to Hand Written Text
kit.text_to_handwriting(text, rgb=[0, 0, 250])

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :