I haven't written a blog for a while. Recently I wrote a simple picture to add a watermark because I need to add a watermark to the picture
python:3.6.7
pycharm
opencv is an open source library that is written in C ++, supports C ++, Python, Java, and MATLAB interfaces, and supports Windows, Linux, Android, and Mac OS in favor of visual applications.
Python used this time
First install the opencv module, pip install opencv-python
Internet downloads abroad are usually slow, so here is a domestic download address
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
After successful download, start writing code ~~.
First import CV2
import cv2
cv2.imread ('Prepare to add a watermark image') #Here is the specific information for reading the image that needs to be watermarked
Use the function cv2.imread (filepath, flags) to read in the picture
- filepath: the full path of the image to be read
- flags: read the flags of the picture
- cv2.IMREAD_COLOR: the default parameter, read a color picture, ignoring the alpha channel
- cv2.IMREAD_GRAYSCALE: read grayscale image
- cv2.IMREAD_UNCHANGED: As the name implies, read in the complete picture, including the alpha pass
The attribute value of the main picture of the data returned by cv2.imread [[[206 240 233] [201 242 234] [204 240 234] ......] is the resolution, color value, width, height, etc.
Only after returning the data can you do the following, if the return is None, it means that the reading has failed, and the subsequent operations will not be completed
Start adding watermarks below:
cv2.putText () # This is to add a watermark
cv2.putText(x,'text',(100,100),cv2.FONT_HERSHEY_SCRIPT_COMPLEX,1,(255,255,255),2)
x = 'Data parameter returned by cv2.imread', added watermark, watermark coordinates, watermark font, font size (the larger the value. The larger the font, decimals can appear), font color coordinate, font thickness (the higher the value. The bolder the font))
After the watermark is added successfully, the watermark image needs to be saved
In this case, cv2.imwrite () is required
cv2.imwrite ('path + name', w) # The path and name of the saved picture, the data returned by w: im.write.
At this time, the watermark is successfully added and saved under the specified file
If you want to see this picture, you can view it in the following small window
cv2.nameWindow ('Window name') #Create a window
cv2.imshow ('Window name', preview picture parameter) #Window name: the name of the window generated for nameWindow, preview picture: here you need to pass in the picture parameter generated by cv2.imread
cv2.waitKey () #Close window after waiting time, ms time
A rough code
x=cv2.imread(img_path2)
cv2.namedWindow('test')
w=cv2.putText(x,'text',(100,100),cv2.FONT_HERSHEY_SCRIPT_COMPLEX,1,(255,255,255),2)
cv2.imwrite('o.jpg',w)
cv2.imshow('test',x)
cv2.waitKey(2000)