├── .gitattributes ├── .gitignore ├── README.md ├── image.py └── log.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows thumbnail cache files 2 | Thumbs.db 3 | ehthumbs.db 4 | ehthumbs_vista.db 5 | 6 | # Folder config file 7 | Desktop.ini 8 | 9 | # Recycle Bin used on file shares 10 | $RECYCLE.BIN/ 11 | 12 | # Windows Installer files 13 | *.cab 14 | *.msi 15 | *.msm 16 | *.msp 17 | 18 | # Windows shortcuts 19 | *.lnk 20 | 21 | # ========================= 22 | # Operating System Files 23 | # ========================= 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python处理图片 2 | 图片合并,剪裁,模板匹配,文字识别等功能 3 | -------------------------------------------------------------------------------- /image.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import aircv as ac 4 | from PIL import Image, ImageDraw, ImageFont 5 | 6 | 7 | def write(): 8 | """ 9 | 在图片上写文字 10 | :return: 11 | """ 12 | txt = "你这个死宅说话" 13 | txt2 = " 还挺搞笑的" 14 | font_img = Image.open("1.jpg") 15 | draw = ImageDraw.Draw(font_img) 16 | # 指定文字字体 17 | ttfront = ImageFont.truetype('./simhei.ttf', 20) 18 | draw.text((40, 60), txt, fill=(0, 0, 0), font=ttfront) 19 | draw.text((60, 130), txt2, fill=(0, 0, 0), font=ttfront) 20 | font_img.save("./out.jpg") 21 | 22 | 23 | def imgCut(imgsrc, box, newimagename="cutting.jpg"): 24 | """ 25 | 图片裁剪,从图片上取得部分图片的内容(长方形区域) 26 | :param imgsrc: 27 | :param box: 28 | :param newimagename: 29 | :return: 30 | """ 31 | im = Image.open(imgsrc) 32 | # box = (10,10,100,100) 33 | region = im.crop(box) 34 | region.save(newimagename) 35 | 36 | 37 | def paste(): 38 | """ 39 | 图片的拼合,2个图片拼合到成一个图片 40 | :return: 41 | """ 42 | img = Image.open("1.jpg") 43 | jgz = Image.open("cutting.jpg") 44 | img.paste(jgz, (196, 139)) 45 | img.save("./out.jpg") 46 | 47 | 48 | def matchImg(imgsrc, imgobj, confidence=0.9): 49 | """ 50 | 查找图片在原始图片上的坐标点 51 | 参考资料:https://www.cnblogs.com/meitian/p/7417582.html 52 | :param imgsrc:原始图像 53 | :param imgobj:待查找的图片 54 | :param confidence: 55 | :return: 56 | """ 57 | imsrc = ac.imread(imgsrc) 58 | imobj = ac.imread(imgobj) 59 | 60 | match_result = ac.find_template(imsrc, imobj, 61 | confidence) 62 | # sample: {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)} 63 | if match_result is not None: 64 | # 添加图片的高和宽 65 | match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽 66 | 67 | return match_result 68 | 69 | 70 | def find_all_template(imgsrc, imgobj, confidence=0.9): 71 | """ 72 | 查找图片在原始图片上的坐标点 73 | 参考资料:https://www.cnblogs.com/meitian/p/7417582.html 74 | :param imgsrc:原始图像 75 | :param imgobj:待查找的图片 76 | :param confidence: 77 | :return: 78 | """ 79 | imsrc = ac.imread(imgsrc) 80 | imobj = ac.imread(imgobj) 81 | 82 | match_result = ac.find_all_template(imsrc, imobj, 83 | confidence) 84 | # sample:{'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)} 85 | return match_result 86 | 87 | 88 | def baidu_ai(): 89 | """ 90 | 利用百度云识别图片中的文字 91 | :return: 92 | """ 93 | from aip import AipOcr 94 | 95 | APP_ID = '' # 百度云后台获取 96 | API_KEY = '' # 百度云后台获取 97 | SECRET_KEY = '' # 百度云后台获取 98 | 99 | client = AipOcr(APP_ID, API_KEY, SECRET_KEY) 100 | image = get_file_content('out.jpg') 101 | 102 | """ 调用通用文字识别, 图片参数为本地图片 """ 103 | return client.basicGeneral(image) 104 | 105 | 106 | def get_file_content(filePath): 107 | """ 108 | 读取图片 109 | :param filePath: 110 | :return: 111 | """ 112 | with open(filePath, 'rb') as fp: 113 | return fp.read() 114 | 115 | 116 | if __name__ == '__main__': 117 | result = find_all_template('0.jpg', '1.jpg') 118 | print(result) 119 | -------------------------------------------------------------------------------- /log.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import logging 3 | #'%a, %d %b %Y %H:%M:%S' #Mon, 15 Oct 2018 16:10:25 4 | #"%Y/%m/%d %H:%M:%S %p" #2018/10/15 16:27:45 PM 5 | logging.basicConfig(level=logging.DEBUG, 6 | format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 7 | datefmt="%Y/%m/%d %H:%M:%S %p", 8 | filename='log.log', 9 | filemode='a') 10 | 11 | #logging.debug('This is debug message') 12 | #logging.info('This is info message') 13 | #logging.warning('This is warning message') 14 | 15 | def debug(msg): 16 | logging.debug(msg) 17 | 18 | def info(msg): 19 | logging.info(msg) 20 | 21 | def warning(msg): 22 | logging.warning(msg) 23 | 24 | if __name__ == '__main__': 25 | warning('%Y %m %d %H:%M:%S') --------------------------------------------------------------------------------