在Python中调用 OpenCV 批量处理图片的功能是非常强大的,也是在日常工作与生活中应用非常广泛的。在如下的案例中,仅用了十几行代码就可以实现批量给图片在指定的区域添加指定的水印文字的功能
案例代码:
import cv2
import os
folder_name = input('请输入要批量添加水印的文件名:')
file_names = os.listdir(folder_name)
includes = ['.jpg', '.JPG', '.png', '.PNG', '.jpeg', '.JPEG', '.gif', '.GIF']
for name in file_names:
    for i in includes:
        if name.endswith(i):
            img = cv2.imread(folder_name + '/' + name)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, 'CAOVAN', (img.shape[1]-600, img.shape[0]-100), font, 4, (255, 0, 0), 4)
            # print(img.shape[0], img.shape[1])
            cv2.imwrite(folder_name + '/new_' + name, img)

原创文章,作者:朋远方,如若转载,请注明出处:https://caovan.com/opencvchangyongcaozuozhipilianggeitupianjiashuiyin/.html


 微信扫一扫
                                                    微信扫一扫                                            