Record some code

Crop the rectangle image
def clipImage(file, p1, p2):
    img = cv2.imread(file, cv2.IMREAD_COLOR)
    return img.copy()[p1[0]:p2[0], p1[1]:p2[1]]


Crop the rectangle image
def clipMat(mat, p1, p2):
    return mat.copy()[p1[0]:p2[0], p1[1]:p2[1]]

p1 = (817, 898)
p2 = (2183 , 1666)
# Video read, display
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])
        # cv2.imshow('',out)
        # cv2.waitKey(100)
        videoWriter.write(out)
        print(i)
        i += 1
cap.release()
cv2.destroyAllWindows()
Copy the code

The center coordinate system becomes the upper-left coordinate system

# -*- coding: utf-8 -*-

import json
import math
import time
' ''Encapsulate transform data as a method'' '



# Angle turns radians
def degreeToRad(r):
    return r * math.pi / 180
# Angle changed to within 360
def degreeConvertIn360(r):
    return r%360

Get the size after scaling
def get_size(_x, _y, scale):
    return (_x*scale[0], _y*scale[1])



X: footage width, y: footage height, anchor: anchor in the image,rotate: Angle, sacle: zoom, anchor_pos: anchor position
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
    # coordinates of the top left corner
    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("Converted coordinates: (%s, %s), Angle: %s" % (after[0], after[1],_r))
    return (after[0], after[1])


A: target point A0: rotation point, r: rotation Angle (counterclockwise rotation)
def ransform(a0, a, r):
    x0 = a[0] - a0[0]
    y0 = a[1] - a0[1]
    # rad = degreeToRad(r)
    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__":
    # import numpy as np
    # from moviepy.editor import *
    # import playutil
    #
    # bg = np.zeros((500, 500, 3), np.uint8)
    # bg1 = np.zeros((400, 10, 3), np.uint8)
    # bg1.fill(255)
    # def get_pos(t):
    # h, w = bg1.shape[:2]
    # print('w: %s, h: %s' % (w, h))
    # return get_postion(w, h, (w/2, h/2), math.sin(t/3)*360, (1, 1), (250, 250))
    # # return transformutil.get_postion(w, h, (w/2, h/2), 45, (1, 1), (w/2, h/2))
    #
    # bg_clip = ImageClip(bg).set_duration(200).set_fps(15)
    # bg_clip1 = ImageClip(bg1).set_position(get_pos).set_fps(55).set_duration(20).rotate(lambda t:math.sin(t/3)*360)
    #
    # out = CompositeVideoClip([bg_clip, bg_clip1]).set_duration(20)
    # playutil.play(out, False)Pos = get_postion (100, 100, (50, 50), and 0, (0.2, 0.2), (50, 50))print(pos)
Copy the code

Image cropping

The background is blurred with the original image
# w, h: Processed image size
def convertImage(file, w, h):
    img = cv2.imread(file, cv2.IMREAD_COLOR)
    img_h, img_w = img.shape[:2]
    # img = ImageClip(file)
    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
    # result = result.set_start(0).set_duration(4)
    # result.write_videofile("result.mp4", fps=24, codec="libx264", bitrate="1000000")
    return bgImg
Copy the code

Write on pictures

# coding:utf-8
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
    # print(type(iter))
    for frame in iter:
        frame = frame.copy().astype(np.uint8)
        print(frame [0, 0])# frame = make_frame(0)
        print(frame.shape)
        img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        # img = frame
        istr = str(i1)
        h, w = img.shape[:2]
        ifLen hasframecount: text = writeText (100, 50 * (istr), istr, 100, (0, 255),'.. /aiqingtuya/ss.ttf')
            th, tw = text.shape[:2]
            img[0:th, 0:tw] = text
        # cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
        # cv2.resizeWindow("frame", (200 ,200));
        cv2.imshow('frame', img)
        key = cv2.waitKey(300)
        if key == ord('q') :break
        i1 = i1 +1
    cv2.destroyAllWindows()

# implement writing in the specified area
# w: the width of the writing area, h: the height of the writing area, x: the x coordinate of the upper left corner of the writing area,y: the same thing: color: the color of the writing area, font: TTF file path of the font,
# x1,y1: Coordinates of the upper left corner of the writing region (not shown beyond the writing region)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)) :# Fix a bug where the captured area is out of the picture
    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)
    # showim(out)
    # cv2.imshow('b', out)
    canvas[y1+h:y1 + 2*h, x1+w:x1 + 2*w] = out
    Rectangle (canvas, (x1, y1), (x1+w, y1+h), (0,0,255), thickness=1)
    return canvas[h:h+c_h, w:w+c_w]

# Write on the pictureDef 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)
    # cv2.imshow("Video", cv2_text_im)
    # cv2.waitKey(0)
    return cv2_text_im




# Handle multiple lines of text centered
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)


# Check whether a single character is a Chinese character
def isChinese(ch):
    if '\u4e00' <= ch <= '\u9fff':
        return True

# @hubobo judgment method
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

# Determine the length of the string, 2 Chinese characters, 1 letter
def txtLenSelf(i) :
    l = 0
    for ch in i:
        if isChinese(ch):
            l = l + 2
        else:
            l = l + 1
    return l




# Achieve writing on the picture, transparent effectdef 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), 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__":
#
#
#
    # 1, write transparent font
    # text = handlerTextLine(' Hello ', 'wahaha ',' Jianghan so beautiful ')
    # # Write transparent words
    # for i in range(10):
    # out = writeTextWeighted(text, 80, 360, img=cv2.imread('.. /test/cat.jpg', cv2.IMREAD_COLOR), x=0, y=0, x1=180,
    # y1=680, color=(255, 0, 0), weight=i / 10)
    # cv2.imshow('', out)
    # cv2.waitKey(0)

#
#
#
# # 2, word for word
# # TXT = 'last night stars last night wind, painting building west bank GUI Tangdong. \n The body has no colourful feng wing, mind acts upon mind. \n Across the hook spring wine warm, cao shot complex wax lamp red. N Jie Yu listen to drum should go to the officer, malantai class turn peng. '
# # n = txt.count('\n') + 1
# # # Write transparent words
# # for i in range(len(txt)+1):
# # input_txt = txt[:i]
# # m = input_txt.count('\n')
# # input_txt =input_txt + ((n-m)*"\n")
# # out = writeTextWeighted(input_txt, 200, 600, img=cv2.imread('.. / demo/a. pg ', cv2. IMREAD_COLOR), x = 0, y = 0, the x1 = 0, y1 = 0, color = (0, 255), weight = 1)
# # cv2.imshow('', out)
# # cv2.waitKey(100)
#
    # 3, write it down from the top
    txt1 = '李白'
    txt2 = 'du fu'
    txt1s = '\n'.join(list(txt1))
    txt2s = '\n'.join(list(txt2))

# out = pictureutil.writeTextWeighted(txt1s, 500, 800, img=cv2.imread( self.path + 'head.png', cv2.IMREAD_COLOR),
# x=0, y=0, x1=516,
# y1=526, color=(136,245,255), font_size=45, weight=1)
# cv2.namedWindow('a', cv2.WINDOW_NORMAL)
    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, the x1 = - 10, y1 = - 10, color = (89147255), font_size = 25)# out = writeTextWeighted(input_txt2s, 200, 600, img=out, x=0, y=20, x1=0,
        # y1=0, color=(89,147,255), font_size=25, weight=1)
        cv2.imshow('a', out)
        cv2.waitKey(1000)

Copy the code