📜  使用Python将文本图像转换为手写文本图像

📅  最后修改于: 2022-05-13 01:54:31.881000             🧑  作者: Mango

使用Python将文本图像转换为手写文本图像

在本文中,我们将看到如何在Python使用 PyWhatkit、Pillow 和 Tesseract 将文本图像转换为手写文本图像。

需要的模块:

Pytesseract:有时也称为Python-tesseract ,是一种基于 Python 的光学字符识别 ( OCR ) 程序。它可以读取和识别照片、车牌和其他文档中的文本。为了解释所提供图像中的单词,我们将使用 tesseract 软件。

pip install pytesseract

Pywhatkit:这是一个可用于多种用途的库,包括发送 WhatsApp 消息、观看 YouTube 视频、搜索 Google 和编写手写文本。

pip install pywhatkit

Pillow 该模块添加了更多功能,可在所有主要操作系统上运行,并支持Python 3。它支持广泛的图像格式,包括“jpeg”、“png”、“bmp”、“gif”、“ppm”和“tiff”。使用枕头模块,您几乎可以对数码照片进行任何操作。

pip install Pillow

注意:访问并安装Tesseract ;向下滚动以查找32 位64 位系统的最新安装程序;根据需要下载它们。



分步实施:

第一步:导入以下模块。

Python3
import pytesseract 
from PIL import Image 
import os
import pywhatkit as kit


Python3
os.chdir(r"C:\Users\Dell\Downloads")


Python3
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\Dell\AppData\Local\/
Tesseract-OCR\tesseract.exe"


Python3
img = Image.open("GFG.png")
text = pytesseract.image_to_string(img)


Python3
kit.text_to_handwriting(text, rgb=[0, 0, 250])


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])


第二步:导航到图片所在的路径,可以使用OS模块中的chdir函数来修改目录。

蟒蛇3

os.chdir(r"C:\Users\Dell\Downloads")

第三步:复制Tesseract的安装路径,粘贴到这里,或者验证Tesseract的目录,复制整个路径。

蟒蛇3

pytesseract.pytesseract.tesseract_cmd = r"C:\Users\Dell\AppData\Local\/
Tesseract-OCR\tesseract.exe"

第四步:首先打开图片 image函数,然后使用pytesseract获取所有图像的数据,并将所有文本存储在一个变量中。

蟒蛇3



img = Image.open("GFG.png")
text = pytesseract.image_to_string(img)

第5步:使用文本pywhatkit将文本转换为指定的RGB颜色_to_handwriting函数;在这种情况下,蓝色的 RGB 是0 , 0 , 250

蟒蛇3

kit.text_to_handwriting(text, rgb=[0, 0, 250])

下面是完整的实现:

蟒蛇3

# 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])

输出: