Android's output stream and clipping python implement the following principles

You must first read the official description of Google

https://source.android.google.cn/devices/camera/images/crop-region-43-ratio.png?hl=zh-cn

Because it needs to do image processing and put it on android to intercept the video stream

so take a look

Not much to say, just go to the code

from PIL import Image, ImageDraw, ImageFont
# 2000:15000 300w像素
image = Image.new(mode='RGBA', size=(2000, 1500), color=(255, 55, 55))
# 如果视频流的宽高比大于剪裁区域,则该视频流应该在垂直方向上进一步剪裁,
# 如果视频流的宽高比小于剪裁区域,则该视频流应该在水平方向上进一步剪裁。
# 剪裁区域:(500、375、1000、750)(宽高比为 4:3)
# 640x480 视频流剪裁:(500、375、1000、750)(与剪裁区域相同)
# 1280x720 视频流剪裁:(500、469、1000、562)
# up_img = Image.new('L', (1280, 720), 'white')  # 制作宽1024,长12的白条
# up_img.save('1280_720.png')
# up_img = Image.new(mode='RGBA', size=(1000,750),  color=(0,191,255))  # 制作宽1024,长12的白条
# up_img.save('1000_750.png')
import math
crop_r_x = 500
crop_r_y = 375
crop_r_w = 1000
crop_r_h = 750
# crop_r_w = 1333
# crop_r_w = 750
# 剪辑区域比例 1000 750 (4:3)
# 剪辑区域比例 1333 750 (16:9)
# 剪辑区域比例 750 750 (1:1)
def crop_region(im):
    video_w,video_h=im.size
    new_x = 0
    new_w = 0
    new_y = 0
    new_h = 0
    # 剪辑区域比例
    b_crop= crop_r_w / crop_r_h
    b_video = video_w / video_h
    # 剪辑区域比1:1
    if b_crop==1:
        print('1.1')
        if b_video==1:
            new_x = int(crop_r_x)
            new_w = int(crop_r_w)
            new_h = int(crop_r_h)
            new_y = int(crop_r_y)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground, new_x, new_y, new_w, new_h
        if b_video < 1.5:
            # 视频4:3
            new_x = int(crop_r_x)
            new_w = int(crop_r_w)
            new_h = int(math.floor(crop_r_w / 4 * 3))
            new_y = int(crop_r_y + (crop_r_h - new_h) / 2)
            imBackground = im.resize((new_w, new_h))
            print(new_x,new_y,new_w,new_h)
            return imBackground, new_x, new_y, new_w, new_h
        # 16:9
        elif b_video > 1.5:
            new_x = int(crop_r_x)
            new_w = int(crop_r_w)
            new_h = int(math.floor(crop_r_w / 16 * 9))
            new_y = int(crop_r_y + (crop_r_h - new_h) / 2)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground, new_x, new_y, new_w, new_h


    # 剪辑区域比4:3
    if b_crop<1.5:
        # 如果视频流的宽高比大于剪裁区域,则该视频流应该在垂直方向上进一步剪裁,
        # 16:9
        # 视频比例1:1  剪辑区域4:3
        if b_video==1 and b_video<b_crop:
            new_x = int(crop_r_x+(crop_r_w-crop_r_h)/2)
            new_w = int(crop_r_h)
            new_h = int(crop_r_h)
            new_y = int(crop_r_y)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground, new_x, new_y, new_w, new_h
        if b_video>b_crop:
            new_x = int(crop_r_x)
            new_w = int(crop_r_w)
            new_h = int(math.floor(crop_r_w/16*9))
            new_y =int(crop_r_y+ (crop_r_h-new_h)/2)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground,new_x,new_y,new_w,new_h
        else:
            new_x = int(crop_r_x)
            new_w = int(crop_r_w)
            new_h = int(crop_r_h)
            new_y = int(crop_r_y)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground,new_x, new_y, new_w, new_h
    # 剪辑区域比16:9
    if b_crop>1.5:
        if b_video<b_crop:
            print(1)
            new_h = int(crop_r_h)

            new_w = int(math.floor(crop_r_h/3*4))
            new_x = int(crop_r_x + (crop_r_w - new_w) / 2)
            new_y =int(crop_r_y)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground,new_x,new_y,new_w,new_h
        else:
            new_x = int(crop_r_x)
            new_w = int(crop_r_w)
            new_h = int(crop_r_h)
            new_y = int(crop_r_y)
            imBackground = im.resize((new_w, new_h))
            print(new_x, new_y, new_w, new_h)
            return imBackground,new_x, new_y, new_w, new_h
if __name__ == '__main__':
    image_16_9 = Image.open(r"E:\scanNumber\视频\1280_720.png")
    image_4_3 = Image.open(r"E:\scanNumber\视频\640_480.png")
    image_1_1 = Image.open(r"E:\scanNumber\视频\1024_1024.png")
    #image_1000_750 = Image.open(r"E:\scanNumber\视频\1000_750.png")
    # image_1_1 = Image.open(r"E:\scanNumber\视频\750_750.png")
    im ,new_x,new_y,new_w,new_h = crop_region(image_4_3)
    image.paste(im, (new_x, new_y))
    # im ,new_x,new_y,new_w,new_h = crop_region(image_1000_750)
    # image.paste(im, (new_x, new_y))
    im ,new_x,new_y,new_w,new_h = crop_region(image_1_1)
    image.paste(im, (new_x, new_y))
    im ,new_x,new_y,new_w,new_h = crop_region(image_16_9)
    image.paste(im, (new_x, new_y))
    # image.show()

requires attention:

The clipping area is 4:3 16:9 1:1

Video area also has 4:3 16:9 1:1

So there will be a judgment that is greater than 1.5 and less than 1.5 is equal to 1

The combination between them is the above code

For pictures, just generate the corresponding size picture file, what are you waiting for, hurry up and enter the group discussion, the newly created group,

The first few are full, welcome to sososo

 

Guess you like

Origin blog.csdn.net/m0_38124502/article/details/122176742