-3
Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Temp\tempCodeRunnerFile.python", line 2, in <module>
    pywhatkit.text_to_handwriting("""
  File "C:\Users\HP\AppData\Roaming\Python\Python311\site-packages\pywhatkit\handwriting.py", line 22, in text_to_handwriting
    raise exceptions.UnableToAccessApi("Unable to access Pywhatkit api right now")
pywhatkit.core.exceptions.UnableToAccessApi: Unable to access Pywhatkit api right now

Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Temp\tempCodeRunnerFile.python", line 2, in <module>
    pywhatkit.text_to_handwriting("""
  File "C:\Users\HP\AppData\Roaming\Python\Python311\site-packages\pywhatkit\handwriting.py", line 22, in text_to_handwriting
    raise exceptions.UnableToAccessApi("Unable to access Pywhatkit api right now")
pywhatkit.core.exceptions.UnableToAccessApi: Unable to access Pywhatkit api right now
D Malan
  • 10,272
  • 3
  • 25
  • 50
balaji
  • 1
  • 1

1 Answers1

1

Someone reported this issue on Github. The maintainers aren't hosting the API on Heroku (or anywhere else) at the moment.

They've made their source code for the API function available here though.

I've extracted the text_to_handwriting function below:

import urllib.request
import string
import numpy as np
from PIL import Image
import cv2

char = string.ascii_lowercase
file_code_name = {}

width = 50
height = 0
newwidth = 0
arr = string.ascii_letters
arr = arr + string.digits + "+,.-? "
letss = string.ascii_letters


def getimg(case, col):
    global width, height, back
    try:
        url = (
            "https://raw.githubusercontent.com/Ankit404butfound/HomeworkMachine/master/Image/%s.png"
            % case
        )
        imglink = urllib.request.urlopen(url)
    except:
        url = (
            "https://raw.githubusercontent.com/Ankit404butfound/HomeworkMachine/master/Image/%s.PNG"
            % case
        )
        imglink = urllib.request.urlopen(url)
    imgNp = np.array(bytearray(imglink.read()))
    img = cv2.imdecode(imgNp, -1)
    cv2.imwrite(r"%s.png" % case, img)
    img = cv2.imread("%s.png" % case)
    img[np.where((img != [255, 255, 255]).all(axis=2))] = col
    cv2.imwrite("chr.png", img)
    cases = Image.open("chr.png")
    back.paste(cases, (width, height))
    newwidth = cases.width
    width = width + newwidth


def text_to_handwriting(string, rgb=[0, 0, 138], save_to: str = "pywhatkit.png"):
    """Convert the texts passed into handwritten characters"""
    global arr, width, height, back
    try:
        back = Image.open("zback.png")
    except:
        url = "https://raw.githubusercontent.com/Ankit404butfound/HomeworkMachine/master/Image/zback.png"
        imglink = urllib.request.urlopen(url)
        imgNp = np.array(bytearray(imglink.read()))
        img = cv2.imdecode(imgNp, -1)
        cv2.imwrite("zback.png", img)
        back = Image.open("zback.png")
    rgb = [rgb[2], rgb[1], rgb[0]]
    count = -1
    lst = string.split()
    for letter in string:
        if width + 150 >= back.width or ord(letter) == 10:
            height = height + 227
            width = 50
        if letter in arr:
            if letter == " ":
                count += 1
                letter = "zspace"
                wrdlen = len(lst[count + 1])
                if wrdlen * 110 >= back.width - width:
                    width = 50
                    height = height + 227

            elif letter.isupper():
                letter = "c" + letter.lower()
            elif letter == ",":
                letter = "coma"
            elif letter == ".":
                letter = "fs"
            elif letter == "?":
                letter = "que"

            getimg(letter, rgb)

    back.save(f"{save_to}")
    back.close()
    back = Image.open("zback.png")
    width = 50
    height = 0
    return save_to


text_to_handwriting("hello, world!", save_to="myimage.png")
D Malan
  • 10,272
  • 3
  • 25
  • 50
  • @DavidMakogon code would've been useful, but it's clear from the traceback what function they called. – D Malan Feb 16 '23 at 13:16
  • Good point on the link-only answer. I'll add the code from the link to the answer. There are lots of API-related questions on SO with high scores, like [this one](https://stackoverflow.com/questions/39559689/where-do-i-find-the-project-id-for-the-gitlab-api) - so this question is within the scope of SO in my view. Definitely not a well-formed or thorough one, but still not off-topic. – D Malan Feb 16 '23 at 13:36