• 给图片添加文字


    import glob
    import os
    from PIL import Image,ImageDraw,ImageFont
    import cv2
    
    from PIL import Image, ImageFont, ImageDraw
    
    import numpy as np
    
    #读取图片并写入文字
    def wirte_text_to_pic(text,pic_path):
        img = Image.open(pic_path)
        w,h = img.size
    
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype('georgia.ttf',size=20,encoding="utf-8")
        txtW,txtH = draw.textsize(text,font=font)
        draw.text((w-txtW-5,h-txtH-2),text=text,fill=(20,200,100,166),font=font)
        savename = pic_path
        img.save(savename)
    
    #读取图片并写入文字
    def wirte_text_to_pic_two(text,pic_path):
        print("text:")
        print(text)
        print("pic_path")
        print(pic_path)
        img1 = cv2.imread(pic_path, cv2.IMREAD_COLOR)
    
        pil_image = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
        w,h = pil_image.size
    
        font = ImageFont.truetype('simhei.ttf', 50, encoding="utf-8")
    
        color = (255, 10, 255)
    
        pos = (w/8, h/4*3)
    
        text = text
    
        draw = ImageDraw.Draw(pil_image)
    
        draw.text(pos, text, font=font, fill=color)
    
        cv_img = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2BGR)
    
        # cv2.imshow(pic_path, cv_img)  #xianshi
    
        cv2.waitKey(0)
        cv2.imwrite(pic_path,cv_img)  #保存图片
    
    
    #获取所有文件
    def get_file_name(file_dir):
        li = []
        for root, dirs, files in os.walk(file_dir):
            print("files:")
            print(files)
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    li.append(os.path.join(root, file))
        print("li:")
        print(li)
        return li
    
    #读取english.txt文件
    with open("D:\PycharmProjects\cigenpic\english.txt",encoding="utf-8",mode='r') as f:
        cigen_list = f.readlines()
        print(cigen_list)
    
    
    #读取picture文件
    WSI_MASK_PATH = 'D:\PycharmProjects\cigenpic\picture'#存放图片的文件夹路径
    print(os.path.join(WSI_MASK_PATH, '*.jpg'))
    wsi_mask_paths_list = glob.glob(os.path.join(WSI_MASK_PATH, '*.jpg'))
    print(wsi_mask_paths_list)
    wsi_mask_paths_list.sort()
    print(wsi_mask_paths_list)
    
    def read_file(file_name):
        with open("D:\PycharmProjects\cigenpic\english.txt", encoding="utf-8", mode='r') as f:
            cigen_list = f.readlines()
            print("cigen_list:")
            print(cigen_list)
            cigen_list_len = len(cigen_list)
            return cigen_list
    
    
    def get_all_pic_path(WSI_MASK_PATH):
        # 读取picture文件
        WSI_MASK_PATH = 'D:\PycharmProjects\cigenpic\picture'  # 存放图片的文件夹路径
        print(os.path.join(WSI_MASK_PATH, '*.jpg'))
        wsi_mask_paths_list = glob.glob(os.path.join(WSI_MASK_PATH, '*.jpg'))
        print(wsi_mask_paths_list)
        wsi_mask_paths_list.sort()
        print(wsi_mask_paths_list)
        return wsi_mask_paths_list
    
    
    def get_new_picture(file_name,pic_dir,copy_pic_dir):
        cigen_list = read_file(file_name=file_name)
        cigen_list_len = len(cigen_list)
    
        li = []
        for root, dirs, files in os.walk(top=pic_dir):
            file_list = files
            print("file_list:")
            print(file_list)
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    li.append(os.path.join(root, file))
        print("li:")
        print(li)
        for i in range(0,len(li)):
            #rb,二进制读取文件
            with open(file=li[i],mode="rb") as f:
                data = f.read()
            wirte_pic_path = copy_pic_dir+"\\"+file_list[i]
            with open(file=wirte_pic_path,mode="wb") as f:
                f.write(data)
            if i < cigen_list_len:
                text = cigen_list[i]
                pic_path= wirte_pic_path
                wirte_text_to_pic_two(text, pic_path)
            else:
                shang = i // cigen_list_len
                print("shang:")
                print(shang)
                yushu = i % cigen_list_len
                print("yushu:")
                print(yushu)
                text = cigen_list[yushu]
                pic_path= wirte_pic_path
                wirte_text_to_pic_two(text, pic_path)
    
    
    
    
    
    
    
    
    
    file_name = r"D:\PycharmProjects\cigenpic\english.txt"
    pic_dir=r"D:\PycharmProjects\cigenpic\picture"
    copy_pic_dir=r"D:\PycharmProjects\cigenpic\engpic"
    get_new_picture(file_name,pic_dir,copy_pic_dir)
  • 相关阅读:
    关于$.ajax同步和异步的问题和提交后台的一些问题。
    maven的web项目手工发布
    eclipse中的maven配置
    springmvc转页面
    javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
    Maven项目导入后打红色X
    springmvc 拦截器,不拦截jsp文件
    RAID 磁盘阵列
    消息队列
    查看占用内存最高的进程
  • 原文地址:https://www.cnblogs.com/jingzaixin/p/16171837.html
Copyright © 2020-2023  润新知