python如何在人物图中添加水印

这篇文章给大家分享的是有关python如何在人物图中添加水印的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

为兰西等地区用户提供了全套网页设计制作服务,及兰西网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、外贸网站建设、兰西网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

python有哪些常用库

python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

1、主要流程

素材准备

人脸检测与人脸关键点检测

调整大小,添加帽子

2、步骤

(1)用dlib的正脸检测器进行人脸检测,用dlib提供的模型提取人脸的五个关键点:

# dlib人脸关键点检测器
      predictor_path = "shape_predictor_5_face_landmarks.dat"
      predictor = dlib.shape_predictor(predictor_path)  
 
      # dlib正脸检测器
      detector = dlib.get_frontal_face_detector()
 
      # 正脸检测
      dets = detector(img, 1)
 
      # 如果检测到人脸
      if len(dets)>0:  
          for d in dets:
              x,y,w,h = d.left(),d.top(), d.right()-d.left(), d.bottom()-d.top()
              # x,y,w,h = faceRect  
              cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2,8,0)
 
              # 关键点检测,5个关键点
              shape = predictor(img, d)
              for point in shape.parts():
                  cv2.circle(img,(point.x,point.y),3,color=(0,255,0))
 
              cv2.imshow("image",img)
              cv2.waitKey()

(2)调整帽子的大小。

选择两个眼角点,找到中心作为放置帽子的X方向的参考坐标,Y方向的坐标用面框上线的Y坐标表示。然后我们根据检测到的人脸大小调整帽子的大小,让帽子的大小合适。

# 选取左右眼眼角的点
              point1 = shape.part(0)
              point2 = shape.part(2)
 
              # 求两点中心
              eyes_center = ((point1.x+point2.x)//2,(point1.y+point2.y)//2)
 
              # cv2.circle(img,eyes_center,3,color=(0,255,0))  
              # cv2.imshow("image",img)
              # cv2.waitKey()
 
              #  根据人脸大小调整帽子大小
              factor = 1.5
              resized_hat_h = int(round(rgb_hat.shape[0]*w/rgb_hat.shape[1]*factor))
              resized_hat_w = int(round(rgb_hat.shape[1]*w/rgb_hat.shape[1]*factor))
 
              if resized_hat_h > y:
                  resized_hat_h = y-1
 
              # 根据人脸大小调整帽子大小
              resized_hat = cv2.resize(rgb_hat,(resized_hat_w,resized_hat_h))

(3)添加小图标

当然有些同学的头像不是人物或不能准确的识别无关,所有添加了标识。(即在右下添加小图标)。

小图标避免单调,是从图标中随机选择一个:

图标位置也可以根据爱好调整大小和位置

layer.paste(logo, (img.size[0] - logo.size[0], img.size[1]-logo.size[1]))

代码如下:

# 水印图片
    num = random.randint(1, 5)
    logo = Image.open("img_icon/santa_" + str(num) + ".png")
    img = Image.open(imgPath)
    print(img.size, logo.size)
    # 图层
    layer = Image.new("RGBA", img.size, (255, 255, 255, 0))
    layer.paste(logo, (img.size[0] - logo.size[0], img.size[1]-logo.size[1]))
    # 覆盖
    img_after = Image.composite(layer, img, layer)
    # img_after.show()
    img_after.save(outImgePath)

感谢各位的阅读!关于“python如何在人物图中添加水印”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

本文名称:python如何在人物图中添加水印
文章URL:https://www.cdcxhl.com/article36/jhdhsg.html

成都网站建设公司_创新互联,为您提供外贸建站微信公众号云服务器网站收录网站策划移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设