How to convert printed text to handwritten image using pywhatkit and opencv

Question:

I have a printed text file input.txt I want to read each line from a txt file and convert it to handwritten format using any existing methods like pywhatkit package following this post https://thecleverprogrammer.com/2021/06/26/text-to-handwriting-using-python/ Using Google Colab and Jupyter

sample input text: input.txt

The Fulton County Grand Jury said Friday that an investigation 
of Atlanta's recent primary election produced "no evidence" that 

The expected output:
out_dir contains 2 items (image_folder , lines.txt)

image_folder :

enter image description here

enter image description here

lines.txt
  | image_1.png     The Fulton County Grand Jury said Friday that an investigation
  | image_2.png     of Atlanta's recent primary election produced "no evidence" that

Snapt from the script :

!pip install pywhatkit
import os
os.environ['DISPLAY'] = ':0'
import pywhatkit as kit
import cv2

import pywhatkit as kit
import cv2

kit.text_to_handwriting("Hope you are doing well", save_to="handwriting.png")
img = cv2.imread("handwriting.png")
cv2.imshow("Text to Handwriting", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The first issue :

/usr/lib/python3.8/os.py in __getitem__(self, key)
    673         except KeyError:
    674             # raise KeyError with the original key value
--> 675             raise KeyError(key) from None
    676         return self.decodevalue(value)
    677 

KeyError: 'DISPLAY'

After running :

import os
os.environ['DISPLAY'] = ':0'

The second issue :

ConnectionRefusedError                    Traceback (most recent call last)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

    DisplayConnectionError                    Traceback (most recent call last)
    /usr/local/lib/python3.8/dist-packages/Xlib/support/unix_connect.py in get_socket(dname, protocol, host, dno)
        110                     raise
        111     except socket.error as val:
    --> 112         raise error.DisplayConnectionError(dname, str(val))
        113 
        114     # Make sure that the connection isn't inherited in child processes.
    
    DisplayConnectionError: Can't connect to display ":0": [Errno 111] Connection refused
Asked By: Mohammed

||

Answers:

After using different methods I find to beneficial tools to do this task perfectly

Answered By: Mohammed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.