Skip to content

Instantly share code, notes, and snippets.

@xcsrz
Created March 8, 2017 00:17
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save xcsrz/8938a5d4a47976c745407fe2788c813a to your computer and use it in GitHub Desktop.
Save xcsrz/8938a5d4a47976c745407fe2788c813a to your computer and use it in GitHub Desktop.
Center text on an image with Python and OpenCV. Had to come up with it myself as no one was spelling this out anywhere (or google couldn't find it)
#!/usr/bin/env python
import numpy as np
import cv2
from time import sleep
# create blank image - y, x
img = np.zeros((600, 1000, 3), np.uint8)
# setup text
font = cv2.FONT_HERSHEY_SIMPLEX
text = "Hello Joseph!!"
# get boundary of this text
textsize = cv2.getTextSize(text, font, 1, 2)[0]
# get coords based on boundary
textX = (img.shape[1] - textsize[0]) / 2
textY = (img.shape[0] + textsize[1]) / 2
# add text centered on image
cv2.putText(img, text, (textX, textY ), font, 1, (255, 255, 255), 2)
# display image
cv2.imshow('image', img)
# wait so you can see the image
sleep(25)
# cleanup
cv2.destroyAllWindows()
@Kristjan-Kiolein
Copy link

Thanks for the code.
Think textX and textY should use integer division though. At least the version of opencv I'm using requires them to be integers.

@Deepayan137
Copy link

Thanks a lot, made my life easier 👍

@evilmtv
Copy link

evilmtv commented Apr 20, 2018

Thank you so much @xcsrz

@zclongpop123
Copy link

In line28, sleep funcion maked python can't disply the image, It shuld be use - cv2.waitKey(0)

@Lait-au-Cafe
Copy link

Great work. This gist saved me a lot of time.

@AdoreIt
Copy link

AdoreIt commented Jun 20, 2019

Nice 👍

@fzyzcjy
Copy link

fzyzcjy commented Jul 7, 2019

Thanks!

@JoshChima
Copy link

Thank you. This helped me out a ton.

@johnkylecooper
Copy link

Thank you so much!

@haiderasad
Copy link

TypeError: integer argument expected, got float

can you please tell how to solve this?

@haiderasad
Copy link

Thanks for the amazing code! much appreciate it

@xcsrz
Copy link
Author

xcsrz commented Nov 12, 2020

@haiderasad does that mean you got around your type error? I haven't encountered that but my guess is you need 18-19 to round:

textX = round((img.shape[1] - textsize[0]) / 2)
textY = round((img.shape[0] + textsize[1]) / 2)

@teshanshanuka
Copy link

Or just do

textX = (img.shape[1] - textsize[0]) // 2
textY = (img.shape[0] + textsize[1]) // 2

@ArashRabbani
Copy link

Thanks it works great

@OskarZyg
Copy link

for future people using this: cast to int and use python_naming_conventions

@lior-abadi
Copy link

The method

 # get coords based on boundary
textX = (img.shape[1] - textsize[0]) / 2
textY = (img.shape[0] + textsize[1]) / 2

can lead to values with decimal placed and a decimal of a pixel is not yet defined. Suggest to round them or //2 as the guys said!

Besides that, great and simple implementation of this tool!

@sharkdeng
Copy link

good

@Carlos144Green
Copy link

C++ users do this


    int textX = (resizedSize.x - size.width) / 2;
    int textY = (resizedSize.y + size.height) / 2;

@Erol444
Copy link

Erol444 commented Sep 15, 2022

https://gist.github.com/Erol444/962bf3d2bd5813d1475d03d154c9966b <- A script that adds a bit more options, like printing text at specific location on frame (topLeft, middle, bottomRight...) and text padding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment