{"id":953734,"date":"2024-12-27T01:47:09","date_gmt":"2024-12-26T17:47:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953734.html"},"modified":"2024-12-27T01:47:10","modified_gmt":"2024-12-26T17:47:10","slug":"%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%9b%be%e7%89%87%e5%88%b0python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953734.html","title":{"rendered":"\u5982\u4f55\u5bfc\u5165\u56fe\u7247\u5230python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094139\/30238ede-0901-41e3-96ae-a6155524381d.webp\" alt=\"\u5982\u4f55\u5bfc\u5165\u56fe\u7247\u5230python\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5bfc\u5165\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09\u3001OpenCV\u3001Matplotlib\u3001Scikit-image\u7b49<\/strong>\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u4f8b\u5982\uff0cPillow\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\uff0cOpenCV\u9002\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\uff0c\u800cMatplotlib\u5219\u65b9\u4fbf\u7528\u4e8e\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5bfc\u5165\u56fe\u7247\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u6d3e\u751f\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u7684\u5404\u79cd\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pillow<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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><h4>2. \u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5bfc\u5165\u56fe\u7247\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\u5bfc\u5165\u56fe\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Pillow\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5feb\u901f\u7684\u56fe\u50cf\u64cd\u4f5c\u3002<\/li>\n<li>\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u5982JPEG\u3001PNG\u3001BMP\u7b49\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u76f8\u6bd4OpenCV\uff0c\u5904\u7406\u901f\u5ea6\u8f83\u6162\u3002<\/li>\n<li>\u4e0d\u9002\u5408\u5904\u7406\u9ad8\u6027\u80fd\u8981\u6c42\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5OpenCV<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5OpenCV\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5bfc\u5165\u56fe\u7247\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\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><h4>3. OpenCV\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/li>\n<li>\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u80fd\u529b\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>API\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u65b0\u624b\u53ef\u80fd\u9700\u8981\u4e00\u5b9a\u7684\u5b66\u4e60\u66f2\u7ebf\u3002<\/li>\n<li>\u9ed8\u8ba4\u8bfb\u53d6\u7684\u56fe\u50cf\u662fBGR\u683c\u5f0f\uff0c\u9700\u8981\u8f6c\u6362\u4e3aRGB\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528Matplotlib<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Matplotlib\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5bfc\u5165\u56fe\u7247\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/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<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Matplotlib\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u975e\u5e38\u9002\u5408\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u3002<\/li>\n<li>\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<li>\u8bfb\u53d6\u901f\u5ea6\u8f83\u6162\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528Scikit-image<\/h3>\n<\/p>\n<p><p>Scikit-image\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u57fa\u4e8eScipy\u6784\u5efa\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Scikit-image<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Scikit-image\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scikit-image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Scikit-image\u5bfc\u5165\u56fe\u7247\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = io.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>io.imshow(image)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Scikit-image\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u63d0\u4f9b\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/li>\n<li>\u6613\u4e8e\u96c6\u6210\u5230\u79d1\u5b66\u8ba1\u7b97\u5de5\u4f5c\u6d41\u4e2d\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6bd4OpenCV\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u573a\u666f\uff0c\u4e0d\u9002\u5408\u5b9e\u65f6\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5bfc\u5165\u56fe\u7247\u5230Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002\u5982\u679c\u4f60\u9700\u8981\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\uff0cPillow\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5982\u679c\u4f60\u4ece\u4e8b\u8ba1\u7b97\u673a\u89c6\u89c9\u5f00\u53d1\uff0cOpenCV\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5982\u679c\u4f60\u9700\u8981\u79d1\u5b66\u8ba1\u7b97\u4e2d\u7684\u56fe\u50cf\u5904\u7406\uff0cScikit-image\u662f\u7406\u60f3\u9009\u62e9\uff1b\u800cMatplotlib\u5219\u9002\u5408\u7528\u6765\u505a\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u5c55\u793a\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u548c\u6027\u80fd\u8981\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u56fe\u7247\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u5bfc\u5165\u548c\u5904\u7406\u56fe\u7247\u6587\u4ef6\u3002\u5e38\u7528\u7684\u5e93\u6709PIL\uff08Pillow\uff09\u3001OpenCV\u548cMatplotlib\u3002\u4ee5Pillow\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u56fe\u7247\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimage = Image.open(&#39;your_image.jpg&#39;)\nimage.show()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u8fd0\u884c\u4ee3\u7801\u4e4b\u524d\u5df2\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install Pillow<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5904\u7406\u5bfc\u5165\u7684\u56fe\u7247\u65f6\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\uff1f<\/strong><br \/>\u5bfc\u5165\u56fe\u7247\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u591a\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u526a\u88c1\u3001\u8fc7\u6ee4\u7b49\u3002\u4f7f\u7528Pillow\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6765\u8c03\u6574\u56fe\u7247\u5927\u5c0f\uff1a<\/p>\n<pre><code class=\"language-python\">resized_image = image.resize((width, height))\nresized_image.show()\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0cOpenCV\u4e5f\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u56fe\u7247\u5904\u7406\u529f\u80fd\uff0c\u6bd4\u5982\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u5e73\u6ed1\u7b49\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Jupyter Notebook\u4e2d\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nimg = mpimg.imread(&#39;your_image.jpg&#39;)\nplt.imshow(img)\nplt.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Notebook\u4e2d\u67e5\u770b\u548c\u5206\u6790\u56fe\u7247\u6570\u636e\uff0c\u9002\u5408\u8fdb\u884c\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u76f8\u5173\u7684\u56fe\u50cf\u5904\u7406\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09\u3001OpenCV\u3001Mat [&hellip;]","protected":false},"author":3,"featured_media":953741,"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\/953734"}],"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=953734"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953734\/revisions"}],"predecessor-version":[{"id":953743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953734\/revisions\/953743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953741"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}