Advertisement
steve-shambles-2109

194-Centre Text On Image

Oct 31st, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. """
  2. Python code snippets vol 39:
  3. stevepython.wordpress.com
  4.  
  5. 194-Centre Text On Image
  6.  
  7. requirements:
  8. pip3 install python-opencv
  9. pip3 install numpy
  10.  
  11. source
  12. https://gist.github.com/xcsrz/8938a5d4a47976c745407fe2788c813a
  13. """
  14. import cv2
  15. import numpy as np
  16.  
  17. # create blank image - y, x
  18. img = np.zeros((600, 1000, 3), np.uint8)
  19.  
  20. # setup text
  21. font = cv2.FONT_HERSHEY_SIMPLEX
  22. text = "stevepython.wordpress.com"
  23.  
  24. # get boundary of this text
  25. textsize = cv2.getTextSize(text, font, 1, 2)[0]
  26.  
  27. # get coords based on boundary
  28. textX = int((img.shape[1] - textsize[0]) / 2)
  29. textY = int((img.shape[0] + textsize[1]) / 2)
  30.  
  31. # add text centered on image
  32. cv2.putText(img, text, (textX, textY), font, 1, (255, 255, 255), 2)
  33.  
  34. # display image
  35. cv2.imshow('image', img)
  36.  
  37. # wait so you can see the image
  38. cv2.waitKey(0)
  39.  
  40. # cleanup
  41. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement