记录一些代码
def clipImage(file, p1, p2):
img = cv2.imread(file, cv2.IMREAD_COLOR)
return img.copy()[p1[0]:p2[0], p1[1]:p2[1]]
def clipMat(mat, p1, p2):
return mat.copy()[p1[0]:p2[0], p1[1]:p2[1]]
p1 = (817, 898)
p2 = (2183 , 1666)
cap = cv2.VideoCapture('D:/crawler/code/MoviePy/donggandimo_01/mask1.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
videoWriter = cv2.VideoWriter('D:/crawler/code/MoviePy/donggandimo_01/mask1_clip.mp4', fourcc, 15.0, (768, 1366))
i = 0
ret = True
while cap.isOpened() and ret:
ret, frame = cap.read()
if ret:
out = clipMat(frame, p1, p2)
print(out.shape[:2])
videoWriter.write(out)
print(i)
i += 1
cap.release()
cv2.destroyAllWindows()
中心点坐标系变为左上角坐标系
import json
import math
import time
'''
将transform数据封装成方法
'''
def degreeToRad(r):
return r * math.pi / 180
def degreeConvertIn360(r):
return r%360
def get_size(_x, _y, scale):
return (_x*scale[0], _y*scale[1])
def get_postion(_x, _y, anchor, rotate, scale, anchor_pos):
print((_x, _y, anchor, rotate, scale, anchor_pos))
p = anchor_pos
s = scale
a = anchor
_r = rotate
x = p[0] - a[0] * s[0]
y = p[1] - a[1] * s[1]
r = degreeConvertIn360(_r)
after = ransform((p[0], -p[1]), (x, -y), -_r)
if r >= 0 and r< 90:
after = (after[0] - _y* s[1]*math.sin(degreeToRad(r)), after[1])
elif r >= 90 and r < 180:
r = r - 90
after = (after[0] - _y * s[1] * math.cos(degreeToRad(r)) - _x*s[0]*math.sin(degreeToRad(r)), after[1] - _y * s[1] * math.sin(degreeToRad(r)))
elif r >= 180 and r < 270:
r = r - 180
after = (after[0] - _x * s[0] * math.cos(degreeToRad(r)), after[1]- _x * s[0] * math.sin(degreeToRad(r)) - _y * s[1] * math.cos(degreeToRad(r)))
elif r >= 270 and r < 360:
r = r - 270
after = (after[0], after[1] - _x * s[0] * math.cos(degreeToRad(r)))
else:
print("######### Error rotate: %s" % (_r))
print("转换后的坐标: (%s, %s), 角度为: %s" % (after[0], after[1],_r))
return (after[0], after[1])
def ransform(a0, a, r):
x0 = a[0] - a0[0]
y0 = a[1] - a0[1]
rad = math.radians(r)
return (x0*math.cos(rad)-y0* math.sin(rad) + a0[0], -(x0 * math.sin(rad)+ y0 *math.cos(rad) + a0[1]))
if __name__ == "__main__":
pos = get_postion(100, 100, (50, 50), 0, (0.2, 0.2), (50, 50))
print(pos)
图片裁剪
def convertImage(file, w, h):
img = cv2.imread(file, cv2.IMREAD_COLOR)
img_h, img_w = img.shape[:2]
bgImg = cv2.resize(img, (w, h))
bgImg = cv2.GaussianBlur(bgImg, (25, 25), 0)
if img_w / img_h > w/h:
print((img_w, img_h))
img = cv2.resize(img, (w,int(img_h/(img_w/w))))
else:
img = cv2.resize(img, (int(img_w/(img_h/h)),h))
img_h, img_w = img.shape[:2]
bgImg[int((h-img_h)/2):int((h-img_h)/2)+ img_h, int((w-img_w)/2):int((w-img_w)/2)+ img_w] = img
return bgImg
图片上写字
import cv2
import numpy as np
from PIL import ImageFont, Image, ImageDraw
def getFont():
return '../Engine/ttf/ss.ttf'
def showframe0(video):
frame = video.get_frame(0)
cv2.imshow('aa', frame)
cv2.waitKey(0)
def showim(frame):
h, w = frame.shape[:2]
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.resizeWindow("frame", (int(w / 2), int(h / 2)));
cv2.imshow('frame', frame)
cv2.waitKey(1000)
def play(video, hasframecount):
iter = video.iter_frames()
i1=0
for frame in iter:
frame = frame.copy().astype(np.uint8)
print(frame[0,0])
print(frame.shape)
img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
istr = str(i1)
h, w = img.shape[:2]
if hasframecount:
text = writeText(100,50*len(istr),istr,100,(255,0,0), '../aiqingtuya/ss.ttf')
th, tw = text.shape[:2]
img[0:th, 0:tw] = text
cv2.imshow('frame', img)
key = cv2.waitKey(300)
if key == ord('q'):
break
i1 = i1 +1
cv2.destroyAllWindows()
def getTextMask(text, h, w,img=None, x=0, y=0,x1 =0, y1=0, color=(255, 255, 255), font=getFont(),font_size=None, bg_color=(0,0,0)):
n = text.count('\n') + 1
if img is None:
canvas_bg = np.zeros((1080, 720, 3), np.uint8)
canvas_bg[:,:,0] = bg_color[0]
canvas_bg[:, :, 1] = bg_color[1]
canvas_bg[:, :, 2] = bg_color[2]
else:
canvas_bg = img.copy()
c_h, c_w = canvas_bg.shape[:2]
canvas = np.zeros((c_h+2*h, c_w+2*w, 3),np.uint8)
canvas[ h:h+c_h, w:w+c_w] = canvas_bg
if font_size is None:
font_size = int((h - 10) / n)
txt_area = canvas[y1+h:y1 + 2*h, x1+w:x1 + 2*w]
out = writeText(h, w, text, font_size, color, font,img=txt_area, x=x, y=y, bg_color=bg_color)
canvas[y1+h:y1 + 2*h, x1+w:x1 + 2*w] = out
return canvas[h:h+c_h, w:w+c_w]
def writeText(h, w,text, size, color, ttf=getFont(), img=None, x =None, y=None, bg_color=(0,0,0)):
color = (color[2], color[1], color[0])
if img is None:
img = np.zeros((h, w, 3), np.uint8)
img[:, :, 0] = bg_color[0]
img[:, :, 1] = bg_color[1]
img[:, :, 2] = bg_color[2]
if x is None:
x = 0
if y is None:
y=0
cv2_im = cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)
pil_im = Image.fromarray(cv2_im)
draw = ImageDraw.Draw(pil_im)
font = ImageFont.truetype(ttf, size, encoding="utf-8")
draw.text((x, y), text, color, font=font)
cv2_text_im = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)
return cv2_text_im
def handlerTextLine(*txt):
max = 0
s = []
for i in txt:
l = 0
for ch in i:
if isChinese(ch):
l = l +2
else:
l = l + 1
if l > max:
max = l
print(max)
for i in txt:
l = 0
for ch in i:
if isChinese(ch):
l = l + 2
else:
l = l + 1
space_num= int((max-l)/2)
print(str(i)+':'+str(space_num))
s.append(space_num*" "+ i)
return '\n'.join(s)
def isChinese(ch):
if '\u4e00' <= ch <= '\u9fff':
return True
def txtLen(i):
l = 0
for ch in i:
if isChinese(ch):
l = l + 2
elif ch.islower():
l = l + 1
else:
l = l + 1.5
return l
def txtLenSelf(i) :
l = 0
for ch in i:
if isChinese(ch):
l = l + 2
else:
l = l + 1
return l
def writeTextWeighted(imput_txt, h, w, img=None, x=0, y=0,x1 =0, y1=0, color=(255, 255, 255), font=getFont(), font_size=None, bg_color=(0,0,0), weight=1):
if img is None:
canvas = np.zeros((1080, 720, 3), np.uint8)
canvas[:, :, 0] = bg_color[0]
canvas[:, :, 1] = bg_color[1]
canvas[:, :, 2] = bg_color[2]
else:
canvas = img
img2 = canvas.copy()
text = getTextMask(imput_txt, h, w, img=img2, x=x, y=y, x1=x1, y1=y1, font_size=font_size, color=color)
out = cv2.addWeighted(img, (1-weight), text,weight, 0)
return out
if __name__ == "__main__":
txt1 = '李白'
txt2 = '杜甫'
txt1s = '\n'.join(list(txt1))
txt2s = '\n'.join(list(txt2))
for i in range(max(len(txt1s), len(txt2s))+1):
input_txt1s = txt1s[:i]
input_txt2s = txt2s[:i]
out = getTextMask(input_txt1s, 200, 600, img=cv2.imread('../test/cat.jpg', cv2.IMREAD_COLOR), x=0, y=0, x1=-10,
y1=-10,color=(89,147,255), font_size=25)
cv2.imshow('a', out)
cv2.waitKey(1000)