{"id":1149750,"date":"2025-01-13T16:53:24","date_gmt":"2025-01-13T08:53:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149750.html"},"modified":"2025-01-13T16:53:27","modified_gmt":"2025-01-13T08:53:27","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%be%93%e5%85%a5%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149750.html","title":{"rendered":"\u5982\u4f55\u7528python\u8f93\u5165\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180409\/e0e5cdff-9630-498a-82e2-87eb43765e32.webp\" alt=\"\u5982\u4f55\u7528python\u8f93\u5165\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u7528Python\u8f93\u5165\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528OpenCV\u3001Pillow\u5e93\u3001matplotlib\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>OpenCV<\/strong> \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u56fe\u50cf\u548c\u89c6\u9891\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u51fd\u6570\uff1b<strong>Pillow<\/strong> \u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u63a5\u53e3\uff1b\u800c <strong>matplotlib<\/strong> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u4e5f\u80fd\u591f\u7528\u4e8e\u56fe\u50cf\u7684\u52a0\u8f7d\u548c\u663e\u793a\u3002<strong>OpenCV\u66f4\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u7684\u573a\u666f\uff0c\u800cPillow\u5219\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u7f16\u8f91\u548c\u5904\u7406\u4efb\u52a1<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u6765\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p>pip install Pillow<\/p>\n<p>pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><h4>1\u3001\u52a0\u8f7d\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u52a0\u8f7d\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u56fe\u50cf\u662f\u5426\u52a0\u8f7d\u6210\u529f<\/strong><\/h2>\n<p>if image is None:<\/p>\n<p>    print(&quot;Error: Could not load image.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Image loaded successfully.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u663e\u793a\u56fe\u50cf\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u8c03\u7528<code>cv2.imshow()<\/code>\u51fd\u6570\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u50cf<\/p>\n<p>cv2.imshow(&#39;Loaded Image&#39;, image)<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u952e\u4e8b\u4ef6\uff0c\u6309\u4efb\u610f\u952e\u5173\u95ed\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u6240\u6709OpenCV\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pillow\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><h4>1\u3001\u52a0\u8f7d\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u52a0\u8f7d\u56fe\u50cf\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\uff0c\u4e0b\u9762\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf\u57fa\u672c\u4fe1\u606f<\/strong><\/h2>\n<p>print(image.format, image.size, image.mode)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684<code>show()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u50cf<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528matplotlib\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><h4>1\u3001\u52a0\u8f7d\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u52a0\u8f7d\u56fe\u50cf\u53ef\u4ee5\u901a\u8fc7<code>plt.imread()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>image = plt.imread(&#39;path\/to\/your\/image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf\u57fa\u672c\u4fe1\u606f<\/strong><\/h2>\n<p>print(image.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u663e\u793a\u56fe\u50cf\u53ef\u4ee5\u901a\u8fc7<code>plt.imshow()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u50cf<\/p>\n<p>plt.imshow(image)<\/p>\n<h2><strong>\u53bb\u9664\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf\u7a97\u53e3<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u56fe\u50cf\u5904\u7406\u548c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u53ef\u4ee5\u901a\u8fc7<code>cv2.resize()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/p>\n<p>resized_image = cv2.resize(image, (300, 300))<\/p>\n<h2><strong>\u663e\u793a\u8c03\u6574\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Resized Image&#39;, resized_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8f6c\u6362\u56fe\u50cf\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u8f6c\u6362\u56fe\u50cf\u989c\u8272\u53ef\u4ee5\u901a\u8fc7<code>cv2.cvtColor()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u56fe\u50cf\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684OpenCV\u3001Pillow\u548cmatplotlib\u5e93\u6765\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u7279\u70b9\uff0c<strong>OpenCV\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0cPillow\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u7f16\u8f91\u548c\u5904\u7406\u4efb\u52a1\uff0c\u800cmatplotlib\u5219\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u548c\u7b80\u5355\u7684\u56fe\u50cf\u663e\u793a\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8fdb\u4e00\u6b65\u5b66\u4e60<\/h3>\n<\/p>\n<p><h4>1\u3001OpenCV\u7684\u9ad8\u7ea7\u5e94\u7528<\/h4>\n<\/p>\n<p><p>OpenCV\u4e0d\u4ec5\u80fd\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\uff0c\u8fd8\u80fd\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u5982\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u53d8\u6362\u3001\u7279\u5f81\u5339\u914d\u7b49\u3002\u5b66\u4e60OpenCV\u7684\u9ad8\u7ea7\u5e94\u7528\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u8ba1\u7b97\u673a\u89c6\u89c9\u9886\u57df\u7684\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Pillow\u7684\u56fe\u50cf\u7f16\u8f91\u529f\u80fd<\/h4>\n<\/p>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u7f16\u8f91\u529f\u80fd\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u6ee4\u955c\u7b49\u3002\u6df1\u5165\u5b66\u4e60Pillow\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u8fdb\u884c\u56fe\u50cf\u7f16\u8f91\u548c\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>3\u3001matplotlib\u7684\u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>matplotlib\u4e0d\u4ec5\u80fd\u663e\u793a\u56fe\u50cf\uff0c\u8fd8\u80fd\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u5728\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u7684\u540c\u65f6\uff0c\u7ed3\u5408\u6570\u636e\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5206\u6790\u548c\u7406\u89e3\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1Python\u56fe\u50cf\u5904\u7406\u7684\u6280\u80fd\uff0c\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982PIL\uff08Pillow\uff09\u6216OpenCV\u6765\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\u3002\u4f7f\u7528Pillow\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\u52a0\u8f7d\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>show()<\/code>\u65b9\u6cd5\u663e\u793a\u5b83\u3002\u5bf9\u4e8eOpenCV\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.imread()<\/code>\u6765\u8bfb\u53d6\u56fe\u7247\uff0c\u4f7f\u7528<code>cv2.imshow()<\/code>\u6765\u663e\u793a\u3002\u786e\u4fdd\u5728\u663e\u793a\u56fe\u7247\u540e\u4f7f\u7528<code>cv2.waitKey(0)<\/code>\u6765\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406\u56fe\u7247\uff1f<\/strong><br \/>\u5904\u7406\u56fe\u7247\u7684\u5e93\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecPillow\u3001OpenCV\u3001scikit-image\u548cmatplotlib\u3002Pillow\u9002\u5408\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u5982\u6253\u5f00\u3001\u4fdd\u5b58\u548c\u4fee\u6539\u56fe\u50cf\uff1bOpenCV\u5219\u66f4\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff1bscikit-image\u4e13\u6ce8\u4e8e\u56fe\u50cf\u5904\u7406\u7b97\u6cd5\uff0c\u800cmatplotlib\u5219\u5e38\u7528\u4e8e\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u7247\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pillow\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>Image.save()<\/code>\u65b9\u6cd5\u5c06\u5904\u7406\u540e\u7684\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730\u3002\u5bf9\u4e8eOpenCV\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite()<\/code>\u51fd\u6570\u6765\u4fdd\u5b58\u56fe\u50cf\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u6307\u5b9a\u6587\u4ef6\u7684\u8def\u5f84\u548c\u683c\u5f0f\uff0c\u4f8b\u5982<code>.jpg<\/code>\u6216<code>.png<\/code>\uff0c\u4ee5\u786e\u4fdd\u56fe\u7247\u6b63\u786e\u4fdd\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u8f93\u5165\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528OpenCV\u3001Pillow\u5e93\u3001matplotlib\u7b49\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":1149756,"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\/1149750"}],"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=1149750"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149750\/revisions"}],"predecessor-version":[{"id":1149760,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149750\/revisions\/1149760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149756"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}