{"id":1004418,"date":"2024-12-27T10:26:13","date_gmt":"2024-12-27T02:26:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004418.html"},"modified":"2024-12-27T10:26:20","modified_gmt":"2024-12-27T02:26:20","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%bd%8d%e5%b1%8f%e5%b9%95%e5%8c%ba%e5%9f%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004418.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081357\/51ce87cb-e883-4d1b-b2c6-2f0e69160f12.webp\" alt=\"python\u5982\u4f55\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6216\u56fe\u50cf\u5904\u7406\u5e93\u6765\u5b9e\u73b0\uff0c\u5982PyAutoGUI\u3001OpenCV\u3001Pillow\u7b49\u3002PyAutoGUI\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u5316\u5c4f\u5e55\u4ea4\u4e92\u3001\u83b7\u53d6\u5c4f\u5e55\u5c3a\u5bf8\u548c\u5750\u6807\u3001\u622a\u53d6\u5c4f\u5e55\u7279\u5b9a\u533a\u57df\u7b49\uff0cOpenCV\u5219\u66f4\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u800cPillow\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u64cd\u4f5c\u548c\u5904\u7406\u3002<\/strong>\u5728\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df\u65f6\uff0c\u9996\u5148\u9700\u8981\u660e\u786e\u6240\u4f7f\u7528\u7684\u5e93\u548c\u5176\u7279\u5b9a\u529f\u80fd\uff0c\u7136\u540e\u6839\u636e\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u5b9e\u73b0\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PYAUTOGUI\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df<\/p>\n<\/p>\n<p><p>PyAutoGUI\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u5141\u8bb8\u6211\u4eec\u901a\u8fc7Python\u63a7\u5236\u9f20\u6807\u548c\u952e\u76d8\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u622a\u5c4f\u548c\u56fe\u50cf\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528PyAutoGUI\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pyautogui.size()<\/code>\u51fd\u6570\u83b7\u53d6\u5c4f\u5e55\u7684\u5206\u8fa8\u7387\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>screenWidth, screenHeight = pyautogui.size()<\/p>\n<p>print(f&quot;Screen width: {screenWidth}, Screen height: {screenHeight}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u5b9a\u4f4d\u5c4f\u5e55\u4e0a\u7684\u7279\u5b9a\u533a\u57df<\/p>\n<\/p>\n<p><p>PyAutoGUI\u7684<code>locateOnScreen()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4f4d\u5c4f\u5e55\u4e0a\u7684\u7279\u5b9a\u56fe\u50cf\u6216\u533a\u57df\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u5c4f\u5e55\u4e0a\u67e5\u627e\u4e0e\u63d0\u4f9b\u7684\u56fe\u50cf\u5339\u914d\u7684\u533a\u57df\uff0c\u5e76\u8fd4\u56de\u5176\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>region = pyautogui.locateOnScreen(&#39;example.png&#39;)<\/p>\n<p>if region:<\/p>\n<p>    print(f&quot;Region found at: {region}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Region not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u529f\u80fd\u5bf9\u4e8e\u81ea\u52a8\u5316\u4efb\u52a1\uff08\u5982\u6e38\u620f\u3001\u8f6f\u4ef6\u64cd\u4f5c\uff09\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>3\u3001\u622a\u53d6\u5c4f\u5e55\u533a\u57df<\/p>\n<\/p>\n<p><p>PyAutoGUI\u8fd8\u53ef\u4ee5\u622a\u53d6\u5c4f\u5e55\u7684\u7279\u5b9a\u533a\u57df\u3002\u4f7f\u7528<code>pyautogui.screenshot()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u622a\u53d6\u6574\u4e2a\u5c4f\u5e55\u6216\u6307\u5b9a\u533a\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>screenshot = pyautogui.screenshot(region=(0, 0, 300, 400))<\/p>\n<p>screenshot.save(&#39;screenshot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>region<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u622a\u53d6\u7684\u533a\u57df\uff0c\u5176\u683c\u5f0f\u4e3a<code>(x, y, width, height)<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528OPENCV\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u901a\u8fc7<code>cv2<\/code>\u6a21\u5757\u53ef\u4ee5\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>image = cv2.imread(&#39;example.png&#39;)<\/p>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6a21\u677f\u5339\u914d\u5b9a\u4f4d<\/p>\n<\/p>\n<p><p>OpenCV\u7684\u6a21\u677f\u5339\u914d\u529f\u80fd\u53ef\u4ee5\u7528\u4e8e\u5728\u5927\u56fe\u4e2d\u627e\u5230\u5c0f\u56fe\u7684\u4f4d\u7f6e\u3002<code>cv2.matchTemplate()<\/code>\u51fd\u6570\u7528\u4e8e\u6267\u884c\u6a21\u677f\u5339\u914d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>large_image = cv2.imread(&#39;large_image.png&#39;)<\/p>\n<p>template = cv2.imread(&#39;template.png&#39;)<\/p>\n<p>result = cv2.matchTemplate(large_image, template, cv2.TM_CCOEFF_NORMED)<\/p>\n<p>min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62\u6846\u663e\u793a\u5339\u914d\u7ed3\u679c<\/strong><\/h2>\n<p>top_left = max_loc<\/p>\n<p>bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0])<\/p>\n<p>cv2.rectangle(large_image, top_left, bottom_right, (0, 0, 255), 2)<\/p>\n<p>cv2.imshow(&#39;Matched Image&#39;, large_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6a21\u677f\u5339\u914d\u53ef\u4ee5\u7528\u4e8e\u8bc6\u522b\u5c4f\u5e55\u4e0a\u7279\u5b9a\u7684\u56fe\u50cf\u6216\u533a\u57df\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PILLOW\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df<\/p>\n<\/p>\n<p><p>Pillow\u662f\u4e00\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u56fe\u50cf\u6253\u5f00\u3001\u7f16\u8f91\u548c\u4fdd\u5b58\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>image = Image.open(&#39;example.png&#39;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u83b7\u53d6\u56fe\u50cf\u533a\u57df<\/p>\n<\/p>\n<p><p>Pillow\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u56fe\u50cf\u7684\u7279\u5b9a\u533a\u57df\u3002\u901a\u8fc7<code>crop()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u622a\u53d6\u56fe\u50cf\u7684\u67d0\u4e2a\u533a\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>image = Image.open(&#39;example.png&#39;)<\/p>\n<p>cropped_image = image.crop((0, 0, 100, 100))<\/p>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>crop()<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\u4e3a\u8981\u622a\u53d6\u533a\u57df\u7684\u5750\u6807\uff0c\u683c\u5f0f\u4e3a<code>(left, upper, right, lower)<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df\u662f\u8bb8\u591a\u81ea\u52a8\u5316\u548c\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u7684\u57fa\u7840<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u975e\u5e38\u91cd\u8981\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>PyAutoGUI<\/strong>\u9002\u5408\u5feb\u901f\u5f00\u53d1\u81ea\u52a8\u5316\u811a\u672c\uff0c\u5c24\u5176\u662f\u9700\u8981\u6a21\u62df\u9f20\u6807\u952e\u76d8\u64cd\u4f5c\u65f6\u3002<\/li>\n<li><strong>OpenCV<\/strong>\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>Pillow<\/strong>\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\u548c\u5904\u7406\uff0c\u5982\u88c1\u526a\u3001\u8c03\u6574\u5927\u5c0f\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u65f6\uff0c\u786e\u4fdd\u62e5\u6709\u8db3\u591f\u7684\u6743\u9650\u8bbf\u95ee\u5c4f\u5e55\u548c\u7a97\u53e3\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u56fe\u50cf\u672a\u627e\u5230\u3001\u5750\u6807\u8d85\u51fa\u8303\u56f4\u7b49\u3002\u901a\u8fc7\u5408\u7406\u7684\u5e93\u9009\u62e9\u548c\u4ee3\u7801\u4f18\u5316\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u5c4f\u5e55\u533a\u57df\u7684\u5b9a\u4f4d\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u5c4f\u5e55\u5206\u8fa8\u7387\u4ee5\u4fbf\u5b9a\u4f4d\u7279\u5b9a\u533a\u57df\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u5c4f\u5e55\u5206\u8fa8\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyautogui<\/code>\u5e93\u4e2d\u7684<code>size()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5c4f\u5e55\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7684\u5143\u7ec4\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>pyautogui.size()<\/code>\u53ef\u4ee5\u5f97\u5230 <code>(1920, 1080)<\/code>\uff0c\u8868\u793a\u5c4f\u5e55\u7684\u5206\u8fa8\u7387\u3002\u901a\u8fc7\u8fd9\u4e9b\u4fe1\u606f\uff0c\u4f60\u53ef\u4ee5\u8ba1\u7b97\u51fa\u9700\u8981\u5b9a\u4f4d\u7684\u533a\u57df\u7684\u5750\u6807\u3002<\/p>\n<p><strong>2. \u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u6765\u5b9a\u4f4d\u5c4f\u5e55\u4e0a\u7684\u7279\u5b9a\u533a\u57df\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>opencv<\/code>\u5e93\u7ed3\u5408<code>pyautogui<\/code>\u6765\u8fdb\u884c\u56fe\u50cf\u8bc6\u522b\u3002\u9996\u5148\uff0c\u622a\u53d6\u5c4f\u5e55\u6216\u51c6\u5907\u5f85\u8bc6\u522b\u7684\u56fe\u50cf\uff0c\u7136\u540e\u4f7f\u7528<code>cv2.matchTemplate()<\/code>\u51fd\u6570\u8fdb\u884c\u6a21\u677f\u5339\u914d\u3002\u5339\u914d\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u5f97\u5230\u7279\u5b9a\u533a\u57df\u7684\u5750\u6807\uff0c\u8fdb\u800c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>3. Python\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5b9a\u4f4d\u548c\u64cd\u4f5c\u5c4f\u5e55\u533a\u57df\uff1f<\/strong><br \/>\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5b9e\u73b0\u5c4f\u5e55\u533a\u57df\u7684\u5b9a\u4f4d\u548c\u64cd\u4f5c\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>pyautogui<\/code>\u548c<code>opencv<\/code>\u3002<code>pyautogui<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u9f20\u6807\u70b9\u51fb\u3001\u952e\u76d8\u8f93\u5165\u7b49\u64cd\u4f5c\uff0c\u800c<code>opencv<\/code>\u5219\u64c5\u957f\u56fe\u50cf\u5904\u7406\u548c\u8bc6\u522b\u3002\u7ed3\u5408\u8fd9\u4e24\u4e2a\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5c4f\u5e55\u533a\u57df\u5b9a\u4f4d\u4e0e\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4f4d\u5c4f\u5e55\u533a\u57df\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6216\u56fe\u50cf\u5904\u7406\u5e93\u6765\u5b9e\u73b0\uff0c\u5982PyAutoGUI\u3001Op [&hellip;]","protected":false},"author":3,"featured_media":1004434,"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\/1004418"}],"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=1004418"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004418\/revisions"}],"predecessor-version":[{"id":1004438,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004418\/revisions\/1004438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004434"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}