turn text into image python code example
Example 1: python image to text
import cv2
import pytesseract
import numpy as np
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\yourname\AppData\Local\Tesseract-OCR\tesseract.exe'
img = cv2.imread('your_img.png')
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray, img_bin = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
gray = cv2.bitwise_not(img_bin)
kernel = np.ones((2, 1), np.uint8)
img = cv2.erode(gray, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=1)
out_below = pytesseract.image_to_string(img)
print(out_below)
Example 2: put text on image python
from PIL import Image, ImageFont, ImageDraw
my_image = Image.open("image.jpg")
title_font = ImageFont.truetype('font.ttf', 200)
image_editable = ImageDraw.Draw(my_image)
image_editable.text((15,15), "Text goes here", (237, 230, 211), font=title_font)
my_image.save("image-text.jpg")