Reputation: 3267
I am very new to opencv. Here I am trying to create an Image which will be a shape made of a string with 0's and 1's (The function random_shape generates that string). So the string is to be typed on top of a white background . Here I am able to create a White Background Image but unable to add text in front of it. It just creates a blank image. It also gives a segmentation fault if I give it different coordinates. Any Help is Appreciated .
font = cv2.FONT_HERSHEY_SIMPLEX
blank_image = np.zeros((100,100,3), np.uint8)
cv2.putText(blank_image,random_shape(100,100),(0,0), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0))
blank_image[:,0:1*100] = (255,255,255)
image=cv2.cv.fromarray(blank_image)
cv2.cv.SaveImage('pic.jpg', image)
Update : I have changed my code to
blank_image = np.zeros((512,512,3), np.uint8)
blank_image[:,0:512] = (255,255,255)
cv2.putText(blank_image,random_shape(100,100)[0:100],(0,0), cv2.FONT_HERSHEY_\
SIMPLEX, 0.1,(0,0,0),cv2.CV_AA)
image=cv2.cv.fromarray(blank_image)
cv2.cv.SaveImage('pic.jpg', image)
It seems to be something garbled (its some sort of text with a line) instead of numbers. The size of the text also appears large.Any Help !!
Upvotes: 1
Views: 3178
Reputation: 3267
It seems I needed to remove cv2.CV_AA and also in case of multiple lines of output there needs to be sufficient vertical spacing.
blank_image = np.zeros((512,512,3), np.uint8)
blank_image[:,0:512] = (255,255,255)
cv2.putText(blank_image,random_shape(100,100)[0:100],(10,10), cv2.FONT_HERSHEY_\
SIMPLEX, 0.2,(0,0,0))
image=cv2.cv.fromarray(blank_image)
cv2.cv.SaveImage('pic.jpg', image)
I managed to create something like this adding the above code in a function. Need to work a bit more on it.
Upvotes: 0
Reputation: 10219
First, you can just save an image using cv2.imwrite("filename.jpg", blank_image)
.
The docstring for cv2.putText(...)
is:
putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> None
The second argument of cv2.putText
, "text", is a string. Chances are, random_shape
in your code does not store "valid" characters. So you are passing values to cv2.putText
that cannot be "translated" to proper characters that can be displayed.
Why don't you debug by manually specifying a string in place of the values obtained from random_shape
? That way, you can test whether the problem is with your code or with the libraries that OpenCV uses print characters or write to the image file etc.
Upvotes: 1