I have a small python code that detects text from images like:
import cv2
image = cv2.imread("sample.jpg")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13)
_, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for contour in contours:
[x,y,w,h] = cv2.boundingRect(contour)
if h>300 and w>300:
continue
if h<40 or w<40:
continue
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
cv2.imwrite("contoured.jpg", image)
Thus, the output is a new image with rectangles above the detected text. I also have a function that encodes text from a static image and shows the encoded result on the console, the function is shown below:
from pytesseract import image_to_string
val_1 = sys.argv[1]
text = image_to_string(Image.open(''+val_1+''))
def encode(key, string):
encode = []
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
encode.append(encoded_c)
encoded_string = "".join(encode)
return base64.urlsafe_b64encode(encoded_string)
encry = encode(key,text)
#print encry
So, for example, if I give him an image containing text, he will extract the text, encode it (if we give him the key) and print the encoded line on the console. However, is it possible to encode text on top of the image itself, instead of printing it on the console.