How to implement OPenCV and PIL Library with text watermarking in python
This article will explain in detail how python implements OPenCV and PIL libraries with text watermarks for pictures. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
In python, we can use openCV to add watermarks to pictures. Note here that openCV cannot add Chinese character watermarks. To add Chinese character watermarks, you can use PIL library to add watermarks to pictures.
One: openCV adds watermarks to pictures.
1: install openCV
Pip install opencv-python
2: add a watermark to the image using openCV:
#-*-coding: utf-8-*-import cv2# load Breakthrough img = cv2.imread ('test.jpg') # add text watermark to picture # cv2.putText (image, need to add string, coordinates to be drawn, font type, font size, font color, font weight) img2 = cv2.putText (img,' test', (100,100), cv2.LINE_AA, 2, (249249249) 4) # Save the picture cv2.imwrite ('wj.jpg', img2) # below is to display the generated picture in the pop-up window cv2.imshow (' img',img2) cv2.waitKey (0) cv2.destoryAllWindows ('img')
The watermark image generated according to the above code is as follows:
Second, use PIL to add watermarks to images #-*-coding: utf-8-*-import cv2import numpyfrom PIL import Image, ImageDraw, ImageFontimg = cv2.imread ('test.jpg') # to determine whether it is an openCV image type if (isinstance (img, numpy.ndarray)): # converted to PIL type img = Image.fromarray (cv2.cvtColor (img) Cv2.COLOR_BGR2RGB) draw = ImageDraw.Draw (img) # font format textSize = 80fontStyle = ImageFont.truetype ("font/simsun.ttc", textSize, encoding= "utf-8") # draw text left = 100top = 'China' textColor = (168,121,103) draw.text ((left, top), text, textColor, font=fontStyle) # convert back to OpenCV type img2 = cv2.cvtColor (numpy.asarray (img) Cv2.COLOR_RGB2BGR) # Save picture cv2.imwrite ('wj.jpg', img2) cv2.imshow (' img',img2) cv2.waitKey (0) cv2.destoryAllWindows ('img')
According to the above code phenomenon is as follows:
On "python how to achieve image watermarking OPenCV and PIL library" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.