5
I wanted to create a text in an image, with python, to show a variable (whatever the format), e.g.: i have an image showing an A = 0, then I change a variable, refocus the image and it appears A = 15, it’s like I do something like?
5
I wanted to create a text in an image, with python, to show a variable (whatever the format), e.g.: i have an image showing an A = 0, then I change a variable, refocus the image and it appears A = 15, it’s like I do something like?
7
There are several ways to do it. You can use the package PIL (Python Image Library), can use the package matplotlib, can use the package scikit-image and so on. You can also use screen creation packages (the Pyqt comes to mind - and if your intention is to build a graphical interface, this is the best approach). Anyway, you have many options.
Despite the Opencv be something useful for far more convoluted things, I find his approach to creating images very simple. Here’s an example of code:
import numpy as np
import cv2
def mostraVariavel(nome, valor):
# Cria uma imagem nova (tamanho 400x200 e 3 canais RGB)
largura = 400
altura = 200
imagem = np.zeros((altura, largura, 3), dtype=np.uint8)
# Preenche o fundo de amarelo
cv2.rectangle(imagem, (0, 0), (largura, altura), (0, 255, 255), -1)
# Desenha uma borda azul
cv2.rectangle(imagem, (0, 0), (largura-5, altura-5), (255, 0, 0), 5)
# Desenha o texto com a variavel em preto, no centro
texto = '{} = {}'.format(nome, valor)
fonte = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
escala = 2
grossura = 3
# Pega o tamanho (altura e largura) do texto em pixels
tamanho, _ = cv2.getTextSize(texto, fonte, escala, grossura)
# Desenha o texto no centro
cv2.putText(imagem, texto, (int(largura / 2 - tamanho[0] / 2), int(altura / 2 + tamanho[1] / 2)), fonte, escala, (0, 0, 0), grossura)
# Exibe a imagem
cv2.imshow("Imagem", imagem)
cv2.waitKey(0)
if __name__ == '__main__':
a = 15
mostraVariavel('a', a)
Which produces the following result:
You could use a background image?
@Ezequieltavares Has yes. Open the image instead of creating one. Opencv’s documentation is full of examples of how to open an image.
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
Um... why does it need to be an "image"? A
print
does not serve? What is your intention (would only debug the contents of the variable)?– Luiz Vieira
This is for a bot from a site called Discord. I have a database there for people to use in RPG. It showing an image with the profile of the person would be cool, just for that reason
– DoutorWhite