def people_Time_template(fgPath, txt): bgPath = "Time.png" bgImg = cv2.imread(bgPath, -1) fgImg = cv2.imread(fgPath, -1) h, w, channel = fgImg.shape if h / w < 7 / 5: fgImg = fgImg[0:h, int(w / 2 - h * 5.0 / 14.0):int(w / 2 + h * 5.0 / 14.0)] # crop background fgImg = cv2.resize(fgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background else: fgImg = cv2.resize(fgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background img = il.imgadd(bgImg, fgImg, [0.5, 0.5], 1080, 1080) hi, wi, channeli = img.shape # 生成文字的图像 ROI_txt = img[int(2 * hi / 3):hi, 0:int(wi / 2)] txtImage([txt, ''], "horizontal", ROI_txt) txt_img = cv2.imread(mainroot + "txt_image/img" + str(1) + ".png", -1) txtpos = [[0.05, 0.7, -1]] img = il.imgadd(txt_img, img, [txtpos[0][1], txtpos[0][0]], txt_img.shape[0], 1080, txtpos[0][-1]) cv2.imwrite(mainroot + "results/results_time.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 3])
def addallimage(ImgPath, txtpos, outputPath): logoPath = mainroot + "logo/logo.jpg" img = cv2.imread(ImgPath, -1) logoimg = cv2.imread(logoPath, -1) for ti in range(len(txtpos) - 1): txt_img_path = mainroot + "txt_image/img" + str(ti + 1) + ".png" txt_img = cv2.imread(txt_img_path, -1) os.remove(txt_img_path) txth, txtw, txtchannel = txt_img.shape img = il.imgadd(txt_img, img, [txtpos[ti][1], txtpos[ti][0]], txth, 1080, txtpos[ti][-1]) img = il.imgadd(logoimg, img, [txtpos[-1][1], txtpos[-1][0]], int(1080 / 15), 1080) cv2.imwrite(outputPath, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 3])
def template3(bgPath, fgPath, txt): bgImg = cv2.imread(bgPath, -1) fgImg = cv2.imread(fgPath, -1) h, w, channel = bgImg.shape if h / w < 7 / 5: bgImg = bgImg[0:h, int(w / 2 - h * 5.0 / 14.0):int(w / 2 + h * 5.0 / 14.0)] # crop background bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background else: bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background fgImg = il.img_circle(fgImg) img = il.imgadd(fgImg, bgImg, [0.5, 0.5], int(1 * 1080 / 2), 1080) hi, wi, channeli = img.shape ROI_txt = img[int(1 * hi / 9):int(8 * hi / 9), int(wi / 2):wi] txtImage(txt, "vertical", ROI_txt) pos = [[0.99, 1 / 9, 1], [0.1, 0.5, 0], [0.5, 0.95, 0]] print("template 3") return img, pos
def template2(bgPath, fgPath, txt): bgImg = cv2.imread(bgPath, -1) fgImg = cv2.imread(fgPath, -1) h, w, channel = bgImg.shape if h / w < 7 / 5: bgImg = bgImg[0:h, int(w / 2 - h * 5.0 / 14.0):int(w / 2 + h * 5.0 / 14.0)] # crop background bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background else: bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background hi, wi, channeli = bgImg.shape # 前景大小 hf, wf, channelf = fgImg.shape # 选择合适的前景大小 fg_hsize = min(0.666 * wi * hf / wf, 0.666 * hi) img = il.imgadd(fgImg, bgImg, [0.8, 0.5], round(fg_hsize), 1080) #生成文字的图像和位置 ROI_txt = img[int(hi / 6):int(1 * hi / 3), int(wi / 8):int(7 * wi / 8)] txtImage(txt, "horizontal", ROI_txt) pos = [[0.5, 0.2, 0], [0.5, 0.4, 0], [0.05, 0.95, 0]] print("template 2") return img, pos
def template8(bgPath, fgPath, txt): bgImg = cv2.imread(bgPath, -1) fgImg = cv2.imread(fgPath, -1) h, w, channel = bgImg.shape if h / w < 7 / 5: bgImg = bgImg[0:h, int(w / 2 - h * 5.0 / 14.0):int(w / 2 + h * 5.0 / 14.0)] # crop background bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background else: bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background fgImg = il.img_circle(fgImg) img = il.imgadd(fgImg, bgImg, [0.5, 0.5], int(1 * 1080 / 2), 1080) hi, wi, channeli = img.shape #第一段文字 ROI_txt = img[int(1 * hi / 9):int(8 * hi / 9), int(wi / 2):wi] txtImage([txt[0], ''], "vertical", ROI_txt) #第二段文字 ROI_txt = img[int(3 * hi / 4):hi, 0:int(wi / 2)] txtImage(['', txt[1]], "horizontal", ROI_txt) pos = [[0.99, 1 / 9, 1], [0.05, 3 / 4, -1], [0.5, 0.95, 0]] print("template 8") return img, pos
def template7(bgPath, fgPath, txt): bgImg = cv2.imread(bgPath, -1) fgImg = cv2.imread(fgPath, -1) h, w, channel = bgImg.shape if h / w < 7 / 5: bgImg = bgImg[0:h, int(w / 2 - h * 5.0 / 14.0):int(w / 2 + h * 5.0 / 14.0)] # crop background bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background else: bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background hi, wi, channeli = bgImg.shape hf, wf, channelf = fgImg.shape # 选择合适的前景大小 fg_hsize = min(0.8 * wi * hf / wf, 0.8 * hi) img = il.imgadd(fgImg, bgImg, [0.5, 0.5], int(fg_hsize), 1080) ROI_txt = img[0:int(hi / 2), int(wi / 2):int(wi)] txtImage(txt, "horizontal", ROI_txt) txtpos = [[0.95, 0.05, 1], [0.95, 0.8, 1], [0.048, 0.95, 0]] print("template 7") return img, txtpos
def template5(bgPath, fgPath, txt): bgImg = cv2.imread(bgPath, -1) fgImg = cv2.imread(fgPath, -1) h, w, channel = bgImg.shape if h / w < 7 / 5: bgImg = bgImg[0:h, int(w / 2 - h * 5.0 / 14.0):int(w / 2 + h * 5.0 / 14.0)] # crop background bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background else: bgImg = cv2.resize(bgImg, (774, 1080), interpolation=cv2.INTER_AREA) # crop background hi, wi, channeli = bgImg.shape hf, wf, channelf = fgImg.shape fg_hsize = min(0.7 * wi * hf / wf, 0.7 * hi) img = il.imgadd(fgImg, bgImg, [0.6, 0.3], round(fg_hsize), 1080) ROI_txt = img[int(hi / 2):hi, int(wi / 2):wi] txtImage(txt, "horizontal", ROI_txt) txtpos = [[0.9, 0.1, 1], [1, 0.6, 1], [0.1, 0.1, 0]] print("template 5") return img, txtpos