{"id":959893,"date":"2024-12-27T03:44:50","date_gmt":"2024-12-26T19:44:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959893.html"},"modified":"2024-12-27T03:44:51","modified_gmt":"2024-12-26T19:44:51","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e5%85%b3%e9%97%ad%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959893.html","title":{"rendered":"python\u5982\u4f55\u81ea\u52a8\u5173\u95ed\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102558\/22888db8-af18-449f-a290-ee5e7f33cba0.webp\" alt=\"python\u5982\u4f55\u81ea\u52a8\u5173\u95ed\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u81ea\u52a8\u5173\u95ed\u56fe\u7247\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u663e\u5f0f\u5173\u95ed\u3001\u4f7f\u7528GUI\u5e93\u7684\u529f\u80fd\u3002<\/strong>\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f<strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong>\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u7684\u5b89\u5168\u7ba1\u7406\u548c\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u663e\u793a\u548c\u5173\u95ed\u56fe\u7247\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u901a\u5e38\u6d89\u53ca\u5230\u4e0d\u540c\u7684\u5e93\uff0c\u6bd4\u5982PIL\uff08Python Imaging Library\uff09\u3001OpenCV\u3001Matplotlib\u7b49\u3002\u6bcf\u4e2a\u5e93\u6709\u4e0d\u540c\u7684<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u65b9\u5f0f\uff0c\u4f46\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u7b80\u5316\u548c\u7edf\u4e00\u8d44\u6e90\u7684\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cdPython\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u4f60\u4e3a\u4e00\u4e9b\u4ee3\u7801\u5757\u8bbe\u7f6e\u8d77\u59cb\u548c\u7ed3\u675f\u64cd\u4f5c\u3002\u5728\u5904\u7406\u6587\u4ef6\u6216\u56fe\u50cf\u65f6\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6210\u540e\u8d44\u6e90\u88ab\u81ea\u52a8\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528PIL\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>PIL\u5e93\u662fPython\u4e2d\u5904\u7406\u56fe\u50cf\u7684\u5e38\u7528\u5e93\u3002\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u4f7f\u7528\u5b8c\u56fe\u7247\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>with Image.open(&#39;example.jpg&#39;) as img:<\/p>\n<p>    img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Image.open<\/code>\u6253\u5f00\u4e86\u4e00\u5f20\u56fe\u7247\uff0c\u5e76\u5728<code>with<\/code>\u5757\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u663e\u5f0f\u5173\u95ed<\/h3>\n<\/p>\n<p><p>\u663e\u5f0f\u5173\u95ed\u662f\u6307\u5728\u7a0b\u5e8f\u4e2d\u660e\u786e\u8c03\u7528\u5173\u95ed\u51fd\u6570\u6765\u91ca\u653e\u8d44\u6e90\u3002\u867d\u7136\u663e\u5f0f\u5173\u95ed\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u88ab\u91ca\u653e\uff0c\u4f46\u5bb9\u6613\u56e0\u4e3a\u5f02\u5e38\u6216\u903b\u8f91\u9519\u8bef\u800c\u5fd8\u8bb0\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528PIL\u663e\u5f0f\u5173\u95ed<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>img.show()<\/p>\n<p>img.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6709\u6548\uff0c\u4f46\u5982\u679c\u7a0b\u5e8f\u4e2d\u9014\u51fa\u73b0\u5f02\u5e38\uff0c<code>img.close()<\/code>\u53ef\u80fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528GUI\u5e93\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u56fe\u50cf\u5e93\u63d0\u4f9b\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u663e\u793a\u548c\u5173\u95ed\u56fe\u50cf\u7a97\u53e3\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u4f1a\u5904\u7406\u7a97\u53e3\u5173\u95ed\u540e\u7684\u8d44\u6e90\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\u7684\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u663e\u793a\u56fe\u7247\u3002\u5b83\u4f1a\u81ea\u52a8\u5904\u7406\u7a97\u53e3\u5173\u95ed\u540e\u7684\u8d44\u6e90\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<p>img = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<p>imgplot = plt.imshow(img)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Matplotlib\u4e2d\uff0c\u5173\u95ed\u7a97\u53e3\u540e\u8d44\u6e90\u4f1a\u81ea\u52a8\u91ca\u653e\uff0c\u7528\u6237\u65e0\u9700\u663e\u5f0f\u8c03\u7528\u5173\u95ed\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528OpenCV<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e38\u7528\u4e8e\u5b9e\u65f6\u56fe\u50cf\u5904\u7406\u3002\u901a\u8fc7OpenCV\u53ef\u4ee5\u663e\u793a\u56fe\u50cf\u5e76\u901a\u8fc7\u6309\u952e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<p>cv2.imshow(&#39;Image&#39;, img)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)  # \u6309\u4efb\u610f\u952e\u5173\u95ed\u7a97\u53e3<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86<code>destroyAllWindows()<\/code>\u51fd\u6570\u6765\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u7a97\u53e3\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u56fe\u50cf\u65f6\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u6ce8\u610f\u4e0d\u540c\u5e93\u7684\u517c\u5bb9\u6027\u548c\u6027\u80fd\u3002\u5bf9\u4e8e\u5927\u6279\u91cf\u56fe\u50cf\u5904\u7406\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u8d44\u6e90\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8981\u6ce8\u610f\u56fe\u50cf\u6587\u4ef6\u7684\u8def\u5f84\u548c\u683c\u5f0f\u652f\u6301\u3002\u4e0d\u540c\u5e93\u53ef\u80fd\u652f\u6301\u4e0d\u540c\u7684\u56fe\u50cf\u683c\u5f0f\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u683c\u5f0f\u8f6c\u6362\u548c\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5e93\uff0c\u53ef\u4ee5\u786e\u4fddPython\u7a0b\u5e8f\u5728\u5904\u7406\u56fe\u50cf\u65f6\u9ad8\u6548\u5730\u7ba1\u7406\u8d44\u6e90\uff0c\u5e76\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u7b49\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u81ea\u52a8\u5173\u95ed\u6253\u5f00\u7684\u56fe\u7247\u7a97\u53e3\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u6765\u663e\u793a\u56fe\u7247\uff0c\u5e76\u5728\u663e\u793a\u540e\u8bbe\u7f6e\u81ea\u52a8\u5173\u95ed\u7a97\u53e3\u7684\u529f\u80fd\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>cv2.waitKey()<\/code>\u51fd\u6570\u914d\u5408<code>cv2.destroyAllWindows()<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7a97\u53e3\u5728\u6307\u5b9a\u65f6\u95f4\u540e\u81ea\u52a8\u5173\u95ed\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u663e\u793a\u56fe\u7247\u540e\u8c03\u7528<code>cv2.waitKey(3000)<\/code>\u6765\u8ba9\u7a97\u53e3\u57283\u79d2\u540e\u5173\u95ed\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528OpenCV\u7684\u60c5\u51b5\u4e0b\u81ea\u52a8\u5173\u95ed\u56fe\u7247\u7a97\u53e3\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u9664\u4e86OpenCV\uff0cPIL\uff08Python Imaging Library\uff09\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u663e\u793a\u56fe\u7247\u3002\u867d\u7136PIL\u672c\u8eab\u4e0d\u652f\u6301\u76f4\u63a5\u5173\u95ed\u7a97\u53e3\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408Tkinter\u7b49GUI\u5e93\u8fdb\u884c\u5b9e\u73b0\u3002\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\u6765\u663e\u793a\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>after()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u5b9a\u65f6\u5173\u95ed\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u56fe\u7247\u663e\u793a\u65f6\u7684\u7528\u6237\u4ea4\u4e92\uff1f<\/strong><br \/>\u5728\u4f7f\u7528OpenCV\u663e\u793a\u56fe\u7247\u65f6\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u952e\u8fdb\u884c\u4ea4\u4e92\uff0c\u4f8b\u5982\u6309\u4e0b\u7279\u5b9a\u952e\u6765\u5173\u95ed\u7a97\u53e3\u3002\u4e3a\u4e86\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5728\u7a97\u53e3\u4e2d\u63d0\u793a\u7528\u6237\u6309\u4e0b\u7279\u5b9a\u952e\u8fdb\u884c\u64cd\u4f5c\u3002\u5982\u679c\u5e0c\u671b\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7528\u6237\u4ea4\u4e92\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Tkinter\u6216PyQt\u7b49\u6846\u67b6\uff0c\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u66f4\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u81ea\u52a8\u5173\u95ed\u56fe\u7247\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u663e\u5f0f\u5173\u95ed\u3001\u4f7f\u7528GUI\u5e93\u7684\u529f\u80fd\u3002\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e0a\u4e0b\u6587 [&hellip;]","protected":false},"author":3,"featured_media":959899,"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\/959893"}],"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=959893"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959893\/revisions"}],"predecessor-version":[{"id":959901,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959893\/revisions\/959901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959899"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}