{"id":1000324,"date":"2024-12-27T09:49:04","date_gmt":"2024-12-27T01:49:04","guid":{"rendered":""},"modified":"2024-12-27T09:49:25","modified_gmt":"2024-12-27T01:49:25","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%b1%8f%e5%b9%95%e5%8f%96%e8%af%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000324.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074952\/d3b0b334-e203-45c4-bc58-c1da7db83eb0.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\uff0c\u53ef\u4ee5\u4f7f\u7528OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u6280\u672f\u3001\u622a\u5c4f\u6280\u672f\u3001\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u7b49\u5de5\u5177\u6765\u5b8c\u6210\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u622a\u56fe\u3001Tesseract\u8fdb\u884cOCR\u8bc6\u522b\u3001\u4ee5\u53ca\u4f7f\u7528PyAutoGUI\u81ea\u52a8\u5316\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u901a\u8fc7\u7ed3\u5408PIL\u548cTesseract\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u622a\u56fe\uff0c\u7136\u540e\u901a\u8fc7Tesseract\u8fdb\u884cOCR\u8bc6\u522b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001PIL\u5e93\u4e0e\u5c4f\u5e55\u622a\u56fe<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3002\u4f7f\u7528PIL\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u5904\u7406\u56fe\u50cf\uff0c\u5305\u62ec\u622a\u56fe\u529f\u80fd\u3002\u4e3a\u4e86\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\uff0c\u9996\u5148\u9700\u8981\u8fdb\u884c\u5c4f\u5e55\u7684\u622a\u56fe\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528PIL\u8fdb\u884c\u622a\u56fe<\/li>\n<\/ol>\n<p><p>PIL\u5e93\u4e2d\u7684<code>ImageGrab<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u622a\u53d6\u5c4f\u5e55\u56fe\u50cf\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86PIL\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u5c4f\u5e55\u622a\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageGrab<\/p>\n<p>def capture_screen():<\/p>\n<p>    # \u83b7\u53d6\u6574\u4e2a\u5c4f\u5e55\u7684\u622a\u56fe<\/p>\n<p>    screenshot = ImageGrab.grab()<\/p>\n<p>    # \u5c06\u622a\u56fe\u4fdd\u5b58\u4e3a\u6587\u4ef6<\/p>\n<p>    screenshot.save(&quot;screenshot.png&quot;)<\/p>\n<p>    return screenshot<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u7528\u4e8e\u83b7\u53d6\u6574\u4e2a\u5c4f\u5e55\u7684\u622a\u56fe\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aPNG\u6587\u4ef6\u3002\u8fd9\u662f\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001Tesseract OCR\u8bc6\u522b<\/p>\n<\/p>\n<p><p>Tesseract\u662f\u4e00\u4e2a\u5f00\u6e90\u7684OCR\u5f15\u64ce\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u5b57\u3002\u4e3a\u4e86\u5728Python\u4e2d\u4f7f\u7528Tesseract\uff0c\u9700\u8981\u5b89\u88c5Tesseract\u8f6f\u4ef6\u5e76\u7ed3\u5408<code>pytesseract<\/code>\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Tesseract<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u5e76\u5b89\u88c5Tesseract\u8f6f\u4ef6\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5b89\u88c5pytesseract<\/li>\n<\/ol>\n<p><p><code>pytesseract<\/code>\u662fPython\u7684\u4e00\u4e2a\u5e93\uff0c\u7528\u4e8e\u4e0eTesseract\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytesseract<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8fdb\u884cOCR\u8bc6\u522b<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528pytesseract\u5bf9\u622a\u56fe\u8fdb\u884cOCR\u8bc6\u522b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pytesseract<\/p>\n<p>from PIL import Image<\/p>\n<p>def ocr_image(image_path):<\/p>\n<p>    # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>    image = Image.open(image_path)<\/p>\n<p>    # \u4f7f\u7528tesseract\u8fdb\u884cOCR\u8bc6\u522b<\/p>\n<p>    text = pytesseract.image_to_string(image)<\/p>\n<p>    return text<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u5bf9\u5148\u524d\u622a\u53d6\u7684\u5c4f\u5e55\u8fdb\u884cOCR\u8bc6\u522b<\/strong><\/h2>\n<p>text = ocr_image(&quot;screenshot.png&quot;)<\/p>\n<p>print(&quot;\u8bc6\u522b\u51fa\u7684\u6587\u672c\uff1a&quot;, text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u6253\u5f00\u4e86\u4fdd\u5b58\u7684\u622a\u56fe\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Tesseract\u8fdb\u884cOCR\u8bc6\u522b\uff0c\u6700\u540e\u8f93\u51fa\u8bc6\u522b\u51fa\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001PyAutoGUI\u81ea\u52a8\u5316\u5de5\u5177<\/p>\n<\/p>\n<p><p>PyAutoGUI\u662f\u4e00\u4e2a\u7528\u4e8eGUI\u81ea\u52a8\u5316\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u5b9e\u73b0\u5bf9\u9f20\u6807\u548c\u952e\u76d8\u7684\u63a7\u5236\u3002\u5728\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\u7684\u8fc7\u7a0b\u4e2d\uff0cPyAutoGUI\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4f4d\u9f20\u6807\u4f4d\u7f6e\u3001\u6a21\u62df\u9f20\u6807\u70b9\u51fb\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PyAutoGUI<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PyAutoGUI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528PyAutoGUI\u8fdb\u884c\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528PyAutoGUI\u83b7\u53d6\u9f20\u6807\u4f4d\u7f6e\u5e76\u8fdb\u884c\u622a\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>def get_mouse_position():<\/p>\n<p>    # \u83b7\u53d6\u5f53\u524d\u9f20\u6807\u7684\u4f4d\u7f6e<\/p>\n<p>    x, y = pyautogui.position()<\/p>\n<p>    return x, y<\/p>\n<p>def capture_region(x1, y1, x2, y2):<\/p>\n<p>    # \u622a\u53d6\u6307\u5b9a\u533a\u57df\u7684\u622a\u56fe<\/p>\n<p>    region = pyautogui.screenshot(region=(x1, y1, x2-x1, y2-y1))<\/p>\n<p>    region.save(&quot;region.png&quot;)<\/p>\n<p>    return region<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u83b7\u53d6\u9f20\u6807\u4f4d\u7f6e\u5e76\u622a\u53d6\u9644\u8fd1\u533a\u57df<\/strong><\/h2>\n<p>x, y = get_mouse_position()<\/p>\n<p>capture_region(x-50, y-50, x+50, y+50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u83b7\u53d6\u5f53\u524d\u9f20\u6807\u7684\u4f4d\u7f6e\uff0c\u5e76\u622a\u53d6\u9f20\u6807\u9644\u8fd1\u7684\u4e00\u4e2a\u5c0f\u533a\u57df\u3002\u8fd9\u53ef\u4ee5\u7528\u4e8e\u5728\u9700\u8981\u65f6\u52a8\u6001\u83b7\u53d6\u5c4f\u5e55\u4e0a\u7684\u6587\u5b57\u533a\u57df\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u5e76\u8bc6\u522b\u6587\u672c\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u5e93\u5bf9\u6587\u672c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NLTK\u3001spaCy\u7b49\u5e93\u8fdb\u884c\u6587\u672c\u5206\u6790\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u5173\u952e\u5b57\u63d0\u53d6\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5NLTK<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5NLTK\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nltk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5bf9\u8bc6\u522b\u51fa\u7684\u6587\u672c\u8fdb\u884c\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528NLTK\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u548c\u8bcd\u6027\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>def process_text(text):<\/p>\n<p>    # \u5206\u8bcd<\/p>\n<p>    tokens = nltk.word_tokenize(text)<\/p>\n<p>    # \u8bcd\u6027\u6807\u6ce8<\/p>\n<p>    tagged = nltk.pos_tag(tokens)<\/p>\n<p>    return tagged<\/p>\n<h2><strong>\u5bf9\u8bc6\u522b\u51fa\u7684\u6587\u672c\u8fdb\u884c\u5904\u7406<\/strong><\/h2>\n<p>tagged_text = process_text(text)<\/p>\n<p>print(&quot;\u5904\u7406\u540e\u7684\u6587\u672c\uff1a&quot;, tagged_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528NLTK\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8bc6\u522b\u51fa\u7684\u6587\u672c\u8fdb\u884c\u66f4\u6df1\u5165\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408PIL\u3001Tesseract\u3001PyAutoGUI\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\u529f\u80fd\u3002\u9996\u5148\uff0c\u4f7f\u7528PIL\u8fdb\u884c\u5c4f\u5e55\u622a\u56fe\uff0c\u7136\u540e\u901a\u8fc7Tesseract\u8fdb\u884cOCR\u8bc6\u522b\uff0c\u63a5\u7740\u4f7f\u7528PyAutoGUI\u5b9e\u73b0\u81ea\u52a8\u5316\u64cd\u4f5c\uff0c\u6700\u540e\u901a\u8fc7\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u5bf9\u8bc6\u522b\u51fa\u7684\u6587\u672c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u548c\u5904\u7406\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u5168\u9762\u7684\u5c4f\u5e55\u53d6\u8bcd\u5de5\u5177\uff0c\u7528\u4e8e\u5404\u79cd\u6587\u672c\u8bc6\u522b\u548c\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u5c4f\u5e55\u4e0a\u7684\u6587\u672c\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u6280\u672f\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5e38\u7528\u7684OCR\u5e93\u5305\u62ecTesseract\u548cPytesseract\u3002\u7528\u6237\u9700\u8981\u5148\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u5e76\u901a\u8fc7\u622a\u56fe\u5de5\u5177\u83b7\u53d6\u5c4f\u5e55\u56fe\u50cf\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528OCR\u5e93\u5206\u6790\u56fe\u50cf\u5e76\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728Windows\u548cMac\u4e0a\u5982\u4f55\u5b9e\u73b0\u5c4f\u5e55\u622a\u56fe\uff1f<\/strong><br \/>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u201c\u622a\u5c4f\u5de5\u5177\u201d\u6216\u5feb\u6377\u952e\u201cWin + Shift + S\u201d\u8fdb\u884c\u622a\u56fe\u3002\u5bf9\u4e8eMac\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528\u201cCommand + Shift + 4\u201d\u8fdb\u884c\u81ea\u5b9a\u4e49\u533a\u57df\u622a\u56fe\u3002\u622a\u56fe\u5b8c\u6210\u540e\uff0c\u5c06\u56fe\u50cf\u4fdd\u5b58\u5e76\u5728Python\u7a0b\u5e8f\u4e2d\u8bfb\u53d6\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8OCR\u63d0\u53d6\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u5347OCR\u7684\u8bc6\u522b\u7387\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u9009\u62e9\u6e05\u6670\u5ea6\u9ad8\u7684\u56fe\u7247\u3001\u4f7f\u7528\u9ed1\u767d\u5bf9\u6bd4\u660e\u663e\u7684\u6587\u672c\uff0c\u6216\u8005\u5bf9\u56fe\u50cf\u8fdb\u884c\u9884\u5904\u7406\uff0c\u6bd4\u5982\u8c03\u6574\u5bf9\u6bd4\u5ea6\u548c\u4eae\u5ea6\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5408\u9002\u7684\u8bed\u8a00\u5305\u548c\u4f18\u5316OCR\u53c2\u6570\u4e5f\u80fd\u663e\u8457\u63d0\u9ad8\u8bc6\u522b\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5b9e\u73b0\u5c4f\u5e55\u53d6\u8bcd\uff0c\u53ef\u4ee5\u4f7f\u7528OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u6280\u672f\u3001\u622a\u5c4f\u6280\u672f\u3001\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u7b49\u5de5\u5177\u6765\u5b8c\u6210\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1000367,"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\/1000324"}],"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=1000324"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000324\/revisions"}],"predecessor-version":[{"id":1000370,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000324\/revisions\/1000370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000367"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}