大家一定要先去看谷歌官方的说明
https://source.android.google.cn/devices/camera/images/crop-region-43-ratio.png?hl=zh-cn
因为需要做图片处理放在android上用来拦截视频流
所以看了一下
话不多说,直接上代码
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()
需要注意:
剪辑区域有 4:3 16:9 1:1
视频区域也有 4:3 16:9 1:1
所以才会有大于1.5小于1.5等于1的判断
他们之间的组合就是 上面的代码
图片的话生成相应的尺寸图片文件就行,还在等啥,赶紧进群讨论,新创建的群,
前面几个都满了,欢迎来sososo