Der Ausgabestream von Android und das Beschneiden von Python implementieren die folgenden Prinzipien

Sie müssen zuerst die offizielle Beschreibung von Google lesen

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

Weil es eine Bildverarbeitung durchführen und auf Android setzen muss, um den Videostream abzufangen

also schau mal

Nicht viel zu sagen, gehen Sie einfach zum 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()

erfordert Aufmerksamkeit:

Der Schnittbereich ist 4:3 16:9 1:1

Videobereich hat auch 4:3 16:9 1:1

Es wird also ein Urteil geben, das größer als 1,5 ist und kleiner als 1,5 gleich 1 ist

Die Kombination zwischen ihnen ist der obige Code

Für Bilder einfach die Bilddatei in der entsprechenden Größe generieren, worauf warten Sie, beeilen Sie sich und treten Sie der Gruppendiskussion bei, der neu erstellten Gruppe,

Die ersten paar sind voll, willkommen bei sososo

 

Acho que você gosta

Origin blog.csdn.net/m0_38124502/article/details/122176742
Recomendado
Clasificación