• opencv-图片水印


    1. import numpy as np
    2. import cv2
    3. import matplotlib.pyplot as plt
    4. img1 = cv2.imread(r'C:\Users\Administrator\Desktop\d0239b1005e063e5e7028963bfb8d1f.png',1)
    5. # img2 = cv2.imread(r'C:\Users\Administrator\Desktop\20231121134301.png',1)
    6. img2 = cv2.imread(r'C:\Users\Administrator\Desktop\600d7fe.png',1)
    7. img2=img2[120:240,240:380]
    8. h, w, c = img2.shape
    9. roi = img1[0:h, 0:w]
    10. img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
    11. ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
    12. mask_inv = cv2.bitwise_not(mask)
    13. img1_bg = cv2.bitwise_and(roi,img2,mask = mask_inv)
    14. img2_fg = cv2.bitwise_and(roi,img2,mask = mask)
    15. dst = cv2.add(img2_fg, img1_bg)
    16. img1[0:h, 0:w ] = dst
    17. # test1=cv2.bitwise_or(dst1,dst2,mask=None)
    18. cv2.imshow('1',img1)
    19. cv2.waitKey(0)
    20. cv2.destroyAllWindows()

     效果图

     原图

     

     

    原理逻辑

     其实就是将需要放置log的位置进行log覆盖,注意用

    bitwise_and进行叠加,mask进行替换
    

    opencv 强大的功能

     

    1. # import numpy as np
    2. # import cv2
    3. # '''图像水印'''
    4. # import matplotlib.pyplot as plt
    5. # img1 = cv2.imread(r'C:\Users\Administrator\Desktop\d0239b1005e063e5e7028963bfb8d1f.png',1)
    6. # # img2 = cv2.imread(r'C:\Users\Administrator\Desktop\20231121134301.png',1)
    7. # img2 = cv2.imread(r'C:\Users\Administrator\Desktop\600d7fe.png',1)
    8. # img2=img2[120:240,240:380]
    9. # # h, w, c = img2.shape
    10. # # roi = img1[0:h, 0:w]
    11. # img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
    12. # ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
    13. # # mask_inv = cv2.bitwise_not(mask)
    14. # # img1_bg = cv2.bitwise_and(roi,img2,mask = mask_inv)
    15. # # img2_fg = cv2.bitwise_and(roi,img2,mask = mask)
    16. # # dst = cv2.add(img2_fg, img1_bg)
    17. # # img1[0:h, 0:w ] = dst
    18. # # txt="Lionel Messi"
    19. # # font = cv2.FONT_HERSHEY_SIMPLEX
    20. # # cv2.putText(img1,txt,(50,500), font, 2,(255,255,255),2,cv2.LINE_AA)
    21. # # cv2.imshow('111',img1)
    22. # # cv2.waitKey(0)
    23. # # cv2.destroyAllWindows()
    24. # # cv2.imwrite('./test.png',img1)
    25. #
    26. #
    27. #
    28. # # 图像副本灰度图轮廓识别
    29. # img = cv2.imread('test.png',1)
    30. # cv2.imshow('Original',img)
    31. # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    32. # template=img2gray
    33. # cv2.imshow('Template',template)
    34. # w,h = template.shape[0], template.shape[1]
    35. # matched = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)
    36. # threshold = 0.5
    37. # loc = np.where( matched >= threshold)
    38. # for pt in zip(*loc[::-1]):
    39. # cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
    40. # cv2.imshow('Matched with Template',img)
    41. # cv2.waitKey(0)
    42. # cv2.destroyAllWindows()
    43. # video 播放
    44. import cv2
    45. from ffpyplayer.player import MediaPlayer
    46. file=r"C:\Users\Administrator\Desktop\coverr-road-in-vermont-8779-1080p.mp4"
    47. file1=r'D:\xy_fs_try\silverArk\video_and_pic\opencv_train\video.mp4'
    48. video=cv2.VideoCapture(file1)
    49. player = MediaPlayer(file1)
    50. while True:
    51. ret, frame=video.read()
    52. audio_frame, val = player.get_frame()
    53. if not ret:
    54. print("End of video")
    55. break
    56. if cv2.waitKey(1) == ord("q"):
    57. break
    58. cv2.imshow("Video", frame)
    59. if val != 'eof' and audio_frame is not None:
    60. #audio
    61. img, t = audio_frame
    62. video.release()
    63. cv2.destroyAllWindows()
    64. # import cv2
    65. # import os
    66. # #OpenCV Python - 从视频中提取图像
    67. # file=r"C:\Users\Administrator\Desktop\coverr-road-in-vermont-8779-1080p.mp4"
    68. # file1=r'D:\xy_fs_try\silverArk\video_and_pic\opencv_train\video.mp4'
    69. # cam = cv2.VideoCapture(file1)
    70. #
    71. # frameno = 0
    72. # while(True):
    73. # ret,frame = cam.read()
    74. # if ret:
    75. # # if video is still left continue creating images
    76. # name = './image/'+ str(frameno) + '.jpg'
    77. # print ('new frame captured...' + name)
    78. #
    79. # cv2.imwrite(name, frame)
    80. # frameno += 1
    81. # else:
    82. # break
    83. #
    84. # cam.release()
    85. # cv2.destroyAllWindows()
    86. # #读取图片成视频
    87. # import cv2
    88. # import numpy as np
    89. # import glob
    90. #
    91. # img_array = []
    92. # for filename in glob.glob('./image/*.jpg'):
    93. # img = cv2.imread(filename)
    94. # height, width, layers = img.shape
    95. # size = (width,height)
    96. # img_array.append(img)
    97. #
    98. # out = cv2.VideoWriter('video.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
    99. #
    100. # for i in range(len(img_array)):
    101. # out.write(img_array[i])
    102. # out.release()

    学习链接

    OpenCV Python - 图片视频 

  • 相关阅读:
    [软件工具][原创]yolov7快速训练助手使用教程傻瓜式训练不需要写代码配置参数
    提高效率:VMLogin浏览器免密码同时登录多个Facebook账号
    【毕业设计】15-基于单片机的交通灯系统设计(原理图+仿真+论文)
    设计模式之代理模式
    Centos中如何删除带有特殊符号的乱码文件_rz命令产生的乱码文件如何删除_使用文件号删除乱码文件---Linux运维工作笔记058
    【STM32】IAP升级03关闭总中断,检测栈顶指针
    5分钟上手Python爬虫:从干饭开始,轻松掌握技巧
    面试突击88:加入事务和嵌套事务有什么区别?
    C# 成为2023年度编程语言之王
    C++打印CPU和内存实时使用情况
  • 原文地址:https://blog.csdn.net/Steven_yang_1/article/details/134531670