{"id":931925,"date":"2024-12-26T17:47:54","date_gmt":"2024-12-26T09:47:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931925.html"},"modified":"2024-12-26T17:47:57","modified_gmt":"2024-12-26T09:47:57","slug":"python%e5%a6%82%e4%bd%95%e8%af%86%e5%88%abfont","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931925.html","title":{"rendered":"python\u5982\u4f55\u8bc6\u522bfont"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070003\/e61ba346-4b5e-4765-b9fb-21872a8a8333.webp\" alt=\"python\u5982\u4f55\u8bc6\u522bfont\" \/><\/p>\n<p><p> <strong>Python\u8bc6\u522b\u5b57\u4f53\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u3001OCR\u6280\u672f\u4ee5\u53ca\u5b57\u4f53\u6587\u4ef6\u89e3\u6790\u7b49\u65b9\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u5982OpenCV\u548cPIL\uff08Pillow\uff09\u8fdb\u884c\u9884\u5904\u7406\u548c\u7279\u5f81\u63d0\u53d6\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\uff0cOCR\u6280\u672f\u5982Tesseract\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6587\u672c\u548c\u5b57\u4f53\uff0c\u800c\u89e3\u6790\u5b57\u4f53\u6587\u4ef6\u5219\u9700\u8981\u4f7f\u7528\u4e13\u95e8\u7684\u5b57\u4f53\u5904\u7406\u5e93\u5982FontTools\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u8bc6\u522b\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u8fdb\u884c\u9884\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8bc6\u522b\u5b57\u4f53\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002\u56fe\u50cf\u5904\u7406\u5e93\u5982OpenCV\u548cPIL\uff08Pillow\uff09\u5728\u8fd9\u65b9\u9762\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u56fe\u50cf\u7070\u5ea6\u5316\u548c\u4e8c\u503c\u5316<\/strong><br \/>\u7070\u5ea6\u5316\u662f\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff0c\u4ee5\u7b80\u5316\u5904\u7406\u8fc7\u7a0b\u3002\u4e8c\u503c\u5316\u5219\u662f\u5c06\u7070\u5ea6\u56fe\u50cf\u8f6c\u6362\u4e3a\u4e8c\u503c\u56fe\u50cf\uff0c\u589e\u5f3a\u6587\u672c\u7684\u5bf9\u6bd4\u5ea6\uff0c\u4f7f\u5f97\u540e\u7eed\u7684\u6587\u672c\u8bc6\u522b\u66f4\u52a0\u51c6\u786e\u3002\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>cv2.cvtColor()<\/code>\u548c<code>cv2.threshold()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u566a\u58f0\u53bb\u9664\u548c\u8fb9\u7f18\u68c0\u6d4b<\/strong><br \/>\u56fe\u50cf\u4e2d\u53ef\u80fd\u5b58\u5728\u566a\u58f0\uff0c\u8fd9\u4f1a\u5f71\u54cd\u5b57\u4f53\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002\u4f7f\u7528OpenCV\u7684<code>cv2.GaussianBlur()<\/code>\u51fd\u6570\u53ef\u4ee5\u53bb\u9664\u566a\u58f0\u3002\u8fb9\u7f18\u68c0\u6d4b\u5219\u53ef\u4ee5\u5e2e\u52a9\u63d0\u53d6\u6587\u672c\u7684\u8fb9\u7f18\u8f6e\u5ed3\uff0c\u5e38\u7528\u7684\u7b97\u6cd5\u6709Canny\u8fb9\u7f18\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528OCR\u6280\u672f\u8fdb\u884c\u8bc6\u522b<\/p>\n<\/p>\n<p><p>OCR\uff08Optical Character Recognition\uff09\u6280\u672f\u53ef\u4ee5\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u672c\uff0c\u5e76\u4e14\u67d0\u4e9bOCR\u5de5\u5177\u53ef\u4ee5\u8bc6\u522b\u5b57\u4f53\u6837\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Tesseract OCR\u7684\u4f7f\u7528<\/strong><br \/>Tesseract\u662f\u4e00\u4e2a\u5f00\u6e90\u7684OCR\u5f15\u64ce\uff0c\u53ef\u4ee5\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u672c\u3002\u901a\u8fc7\u5b89\u88c5pytesseract\u5e93\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8c03\u7528Tesseract\u8fdb\u884c\u6587\u672c\u8bc6\u522b\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Tesseract OCR\uff0c\u7136\u540e\u4f7f\u7528<code>pytesseract.image_to_string()<\/code>\u51fd\u6570\u5bf9\u9884\u5904\u7406\u540e\u7684\u56fe\u50cf\u8fdb\u884c\u8bc6\u522b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u4f53\u6837\u5f0f\u8bc6\u522b\u7684\u53ef\u80fd\u6027<\/strong><br \/>\u867d\u7136Tesseract\u4e3b\u8981\u7528\u4e8e\u6587\u672c\u8bc6\u522b\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u4e5f\u53ef\u4ee5\u8bc6\u522b\u5b57\u4f53\u6837\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7\u8bad\u7ec3\u81ea\u5b9a\u4e49\u7684\u8bed\u8a00\u5305\uff0c\u5e2e\u52a9Tesseract\u8bc6\u522b\u7279\u5b9a\u7684\u5b57\u4f53\u6837\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u89e3\u6790\u5b57\u4f53\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u76f4\u63a5\u89e3\u6790\u5b57\u4f53\u6587\u4ef6\u4ee5\u8bc6\u522b\u5b57\u4f53\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528FontTools\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6\u5b57\u4f53\u6587\u4ef6\u4fe1\u606f<\/strong><br \/>FontTools\u662f\u4e00\u4e2a\u7528\u4e8e\u64cd\u4f5c\u5b57\u4f53\u6587\u4ef6\u7684Python\u5e93\uff0c\u53ef\u4ee5\u8bfb\u53d6\u548c\u4fee\u6539\u5b57\u4f53\u6587\u4ef6\u4e2d\u7684\u4fe1\u606f\u3002\u901a\u8fc7FontTools\uff0c\u53ef\u4ee5\u89e3\u6790\u5b57\u4f53\u6587\u4ef6\uff08\u5982TTF\u3001OTF\u7b49\uff09\uff0c\u83b7\u53d6\u5b57\u4f53\u7684\u540d\u79f0\u3001\u5b57\u7b26\u6620\u5c04\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6bd4\u8f83\u5b57\u4f53\u7279\u5f81<\/strong><br \/>\u5728\u8bc6\u522b\u5b57\u4f53\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u5b57\u4f53\u7684\u7279\u5f81\uff08\u5982\u5b57\u7b26\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u7b14\u753b\u6837\u5f0f\u7b49\uff09\u6765\u786e\u5b9a\u5b57\u4f53\u7684\u7c7b\u578b\u3002FontTools\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u53d6\u8fd9\u4e9b\u7279\u5f81\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u63d0\u9ad8\u51c6\u786e\u6027<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u9ad8\u5b57\u4f53\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed3\u5408\u56fe\u50cf\u5904\u7406\u548cOCR\u6280\u672f<\/strong><br \/>\u901a\u8fc7\u56fe\u50cf\u5904\u7406\u8fdb\u884c\u9884\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8OCR\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002\u5c06\u56fe\u50cf\u5904\u7406\u548cOCR\u6280\u672f\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u672c\u548c\u5b57\u4f53\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8fdb\u884c\u8bad\u7ec3<\/strong><br \/>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\uff0c\u8bad\u7ec3\u6a21\u578b\u6765\u8bc6\u522b\u7279\u5b9a\u7684\u5b57\u4f53\u3002\u901a\u8fc7\u6536\u96c6\u548c\u6807\u6ce8\u5927\u91cf\u7684\u6837\u672c\u6570\u636e\uff0c\u8bad\u7ec3\u4e00\u4e2a\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u7cbe\u5ea6\u7684\u5b57\u4f53\u8bc6\u522b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5177\u4f53\u5e94\u7528\u4e2d\uff0c\u8bc6\u522b\u5b57\u4f53\u9700\u8981\u7ed3\u5408\u5b9e\u9645\u9700\u6c42\u548c\u6280\u672f\u6761\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><br \/>\u5b57\u4f53\u8bc6\u522b\u6280\u672f\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6587\u6863\u6570\u5b57\u5316\u3001\u56fe\u50cf\u5904\u7406\u3001\u8bbe\u8ba1\u6392\u7248\u7b49\u9886\u57df\u3002\u5728\u8fd9\u4e9b\u5e94\u7528\u4e2d\uff0c\u51c6\u786e\u8bc6\u522b\u5b57\u4f53\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u81ea\u52a8\u5316\u6c34\u5e73\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><br \/>\u8bc6\u522b\u5b57\u4f53\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u56fe\u50cf\u7684\u6e05\u6670\u5ea6\u548c\u5bf9\u6bd4\u5ea6\uff0c\u8fd9\u4f1a\u76f4\u63a5\u5f71\u54cd\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u5b57\u4f53\u6837\u5f0f\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u989d\u5916\u7684\u8bad\u7ec3\u548c\u8c03\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u8bc6\u522b\u5b57\u4f53\uff0c\u5305\u62ec\u56fe\u50cf\u5904\u7406\u3001OCR\u6280\u672f\u4ee5\u53ca\u89e3\u6790\u5b57\u4f53\u6587\u4ef6\u7b49\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u51c6\u786e\u7684\u5b57\u4f53\u8bc6\u522b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u548c\u8bc6\u522b\u5b57\u4f53\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PIL<\/code>\uff08Pillow\uff09\u5e93\u6765\u52a0\u8f7d\u548c\u8bc6\u522b\u5b57\u4f53\u6587\u4ef6\u3002\u901a\u8fc7<code>ImageFont<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u7684\u8def\u5f84\u5e76\u521b\u5efa\u4e00\u4e2a\u5b57\u4f53\u5bf9\u8c61\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import ImageFont\n\nfont_path = &quot;path\/to\/your\/font.ttf&quot;\nfont_size = 12\nfont = ImageFont.truetype(font_path, font_size)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5e2e\u52a9\u4f60\u52a0\u8f7d\u6307\u5b9a\u8def\u5f84\u7684\u5b57\u4f53\u6587\u4ef6\uff0c\u5e76\u53ef\u4ee5\u5728\u56fe\u50cf\u4e2d\u4f7f\u7528\u3002<\/p>\n<p><strong>Python\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u8bc6\u522b\u5b57\u4f53\u7684\u6837\u5f0f\u548c\u7279\u5f81\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>fonttools<\/code>\u5e93\u53ef\u4ee5\u7528\u6765\u5206\u6790\u5b57\u4f53\u6587\u4ef6\u7684\u7279\u5f81\u3002\u8fd9\u4e2a\u5e93\u53ef\u4ee5\u63d0\u53d6\u5b57\u4f53\u7684\u5143\u6570\u636e\uff0c\u6bd4\u5982\u5b57\u4f53\u540d\u79f0\u3001\u6837\u5f0f\u3001\u5b57\u91cd\u7b49\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre><code class=\"language-python\">from fontTools.ttLib import TTFont\n\nfont = TTFont(&quot;path\/to\/your\/font.ttf&quot;)\nprint(font[&quot;name&quot;].names)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u6253\u5370\u51fa\u5b57\u4f53\u7684\u5404\u79cd\u540d\u79f0\u4fe1\u606f\uff0c\u4ece\u4e2d\u4f60\u53ef\u4ee5\u627e\u5230\u6240\u9700\u7684\u6837\u5f0f\u548c\u7279\u5f81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u56fe\u50cf\u4e2d\u4f7f\u7528Python\u8bc6\u522b\u7684\u5b57\u4f53\u8fdb\u884c\u6587\u672c\u6e32\u67d3\uff1f<\/strong><br \/>\u5728\u56fe\u50cf\u4e2d\u6e32\u67d3\u6587\u672c\u65f6\uff0c\u53ef\u4ee5\u5229\u7528<code>Pillow<\/code>\u5e93\u5c06\u8bc6\u522b\u7684\u5b57\u4f53\u5e94\u7528\u4e8e\u6587\u672c\u3002\u901a\u8fc7<code>ImageDraw<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u6587\u672c\uff0c\u5e76\u4f7f\u7528\u4e4b\u524d\u52a0\u8f7d\u7684\u5b57\u4f53\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont\n\n# \u521b\u5efa\u56fe\u50cf\nimage = Image.new(&#39;RGB&#39;, (200, 100), color=&#39;white&#39;)\ndraw = ImageDraw.Draw(image)\n\n# \u7ed8\u5236\u6587\u672c\ndraw.text((10, 25), &quot;Hello, World!&quot;, font=font, fill=&#39;black&#39;)\n\n# \u4fdd\u5b58\u56fe\u50cf\nimage.save(&#39;output.png&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u521b\u5efa\u7684\u767d\u8272\u56fe\u50cf\u4e0a\u4ee5\u52a0\u8f7d\u7684\u5b57\u4f53\u7ed8\u5236\u6587\u672c\uff0c\u5e76\u4fdd\u5b58\u4e3a\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bc6\u522b\u5b57\u4f53\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u3001OCR\u6280\u672f\u4ee5\u53ca\u5b57\u4f53\u6587\u4ef6\u89e3\u6790\u7b49\u65b9\u5f0f\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u56fe\u50cf\u5904\u7406 [&hellip;]","protected":false},"author":3,"featured_media":931929,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931925"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=931925"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931925\/revisions"}],"predecessor-version":[{"id":931931,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931925\/revisions\/931931"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/931929"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=931925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=931925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=931925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}