import numpy as np
import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread(r'C:\Users\Administrator\Desktop\d0239b1005e063e5e7028963bfb8d1f.png',1)
# img2 = cv2.imread(r'C:\Users\Administrator\Desktop\20231121134301.png',1)
img2 = cv2.imread(r'C:\Users\Administrator\Desktop\600d7fe.png',1)
img2=img2[120:240,240:380]
h, w, c = img2.shape
roi = img1[0:h, 0:w]
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi,img2,mask = mask_inv)
img2_fg = cv2.bitwise_and(roi,img2,mask = mask)
dst = cv2.add(img2_fg, img1_bg)
img1[0:h, 0:w ] = dst
# test1=cv2.bitwise_or(dst1,dst2,mask=None)
cv2.imshow('1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果图
原图
原理逻辑
其实就是将需要放置log的位置进行log覆盖,注意用
bitwise_and进行叠加,mask进行替换
opencv 强大的功能
# import numpy as np
# import cv2
# '''图像水印'''
# import matplotlib.pyplot as plt
# img1 = cv2.imread(r'C:\Users\Administrator\Desktop\d0239b1005e063e5e7028963bfb8d1f.png',1)
# # img2 = cv2.imread(r'C:\Users\Administrator\Desktop\20231121134301.png',1)
# img2 = cv2.imread(r'C:\Users\Administrator\Desktop\600d7fe.png',1)
# img2=img2[120:240,240:380]
# # h, w, c = img2.shape
# # roi = img1[0:h, 0:w]
# img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
# # mask_inv = cv2.bitwise_not(mask)
# # img1_bg = cv2.bitwise_and(roi,img2,mask = mask_inv)
# # img2_fg = cv2.bitwise_and(roi,img2,mask = mask)
# # dst = cv2.add(img2_fg, img1_bg)
# # img1[0:h, 0:w ] = dst
# # txt="Lionel Messi"
# # font = cv2.FONT_HERSHEY_SIMPLEX
# # cv2.putText(img1,txt,(50,500), font, 2,(255,255,255),2,cv2.LINE_AA)
# # cv2.imshow('111',img1)
# # cv2.waitKey(0)
# # cv2.destroyAllWindows()
# # cv2.imwrite('./test.png',img1)
#
#
#
# # 图像副本灰度图轮廓识别
# img = cv2.imread('test.png',1)
# cv2.imshow('Original',img)
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# template=img2gray
# cv2.imshow('Template',template)
# w,h = template.shape[0], template.shape[1]
# matched = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)
# threshold = 0.5
# loc = np.where( matched >= threshold)
# for pt in zip(*loc[::-1]):
# cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
# cv2.imshow('Matched with Template',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# video 播放
import cv2
from ffpyplayer.player import MediaPlayer
file=r"C:\Users\Administrator\Desktop\coverr-road-in-vermont-8779-1080p.mp4"
file1=r'D:\xy_fs_try\silverArk\video_and_pic\opencv_train\video.mp4'
video=cv2.VideoCapture(file1)
player = MediaPlayer(file1)
while True:
ret, frame=video.read()
audio_frame, val = player.get_frame()
if not ret:
print("End of video")
break
if cv2.waitKey(1) == ord("q"):
break
cv2.imshow("Video", frame)
if val != 'eof' and audio_frame is not None:
#audio
img, t = audio_frame
video.release()
cv2.destroyAllWindows()
# import cv2
# import os
# #OpenCV Python - 从视频中提取图像
# file=r"C:\Users\Administrator\Desktop\coverr-road-in-vermont-8779-1080p.mp4"
# file1=r'D:\xy_fs_try\silverArk\video_and_pic\opencv_train\video.mp4'
# cam = cv2.VideoCapture(file1)
#
# frameno = 0
# while(True):
# ret,frame = cam.read()
# if ret:
# # if video is still left continue creating images
# name = './image/'+ str(frameno) + '.jpg'
# print ('new frame captured...' + name)
#
# cv2.imwrite(name, frame)
# frameno += 1
# else:
# break
#
# cam.release()
# cv2.destroyAllWindows()
# #读取图片成视频
# import cv2
# import numpy as np
# import glob
#
# img_array = []
# for filename in glob.glob('./image/*.jpg'):
# img = cv2.imread(filename)
# height, width, layers = img.shape
# size = (width,height)
# img_array.append(img)
#
# out = cv2.VideoWriter('video.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
#
# for i in range(len(img_array)):
# out.write(img_array[i])
# out.release()
学习链接
OpenCV Python - 图片视频