[PYTHON] Add lines and text on the image

Execution environment

Google Colaboratory

Preparing to load images with Google Colaboratory


from google.colab import files
from google.colab import drive
img = cv2.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")

Loading the required libraries


import cv2 #opencv
import matplotlib.pyplot as plt #I want the coordinates to check the result, so I use it
%matplotlib inline

Put a line on the image


#Write information settings
pt1 = (50,50)
pt2 = (200,50)
color = (255,0,0)
fontcolor = (0,0,255)
width = 5
fontsize = 0.7
fontwidth = 2
r = 10
fontface = cv2.FONT_HERSHEY_SIMPLEX#Font type

#Write a line on the image (setting is up)
cv2.line(img, pt1, pt2, color,width)

#Write a circle
cv2.circle(img,pt1,r,color,width)#width is-If it is 1, it will be filled.

#Write letters
cv2.putText(img,'Girl is Lenna!',pt1,fontface,fontsize,fontcolor,fontwidth)

#Check the image(Since the RGB order is different between opencv and matplot, it is converted and displayed.)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))



What I found

The width of the line is literally the same width, not the plus or minus width. The diameter of the circle must be specified by the radius. The coordinate start point of the character is the lower left of the character by default. If you want to change it, specify bottomLeftOrigin = True The starting point is on the upper left. However, the letters are also turned over.

Recommended Posts

Add lines and text on the image
Django-Overview the tutorial app on Qiita and add features (1)
Judge the extension and download the image
Text mining: Probability density distribution on the hypersphere and text clustering in KMeans
Download the image from the text file containing the URL
Use AppSync on the front and back ends
Get the module itself and add members dynamically
Response the resized image using Flask and PILImage
Image display and video playback on Neovim Terminal
In SQLite3, add just two lines and use the extension library (extended SQL function)!
Run the flask app on Cloud9 and Apache Httpd
Extract and list personal names and place names in the text
Python text reading for multiple lines and one line
Specify the volume on linux and make a sound
Automatically determine and process the encoding of the text file
Notes on HDR and RAW image processing with Python
[2020 version] Scraping and processing the text from Aozora Bunko
Enable zfs on the Springdale Linux 8.3 installation ISO image
Execute the command on the web server and display the result
Rotate and scale the image before cropping [python] [OpenCV]
POST the image with json and receive it with flask
Install django on python + anaconda and start the server
Resize the image to the specified size and blacken the margins
Python OpenCV tried to display the image in text.
Add the original context_processor
Extracted text from image
POST the image selected on the website with multipart / form-data and save it to Amazon S3! !!
Generate image text together
Notes on building TinyEMU and booting the Linux kernel on Emscripten
[pyqtgraph] Add region to the graph and link it with the graph region
Use pyOCR to convert the description on the card into text
Count the number of characters in the text on the clipboard on mac
How to add new data (lines and plots) using matplotlib
Find the most F-word commit on Linux (git and later)
Image Optimize on the server side using TinyPNG's Web API