In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.