{"id":1001253,"date":"2024-12-27T09:58:27","date_gmt":"2024-12-27T01:58:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001253.html"},"modified":"2024-12-27T09:58:29","modified_gmt":"2024-12-27T01:58:29","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%b0%83%e7%94%a8%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001253.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u8c03\u7528\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075513\/6e1fe1da-7b7b-4f03-a085-a5253d17eb93.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u8c03\u7528\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u8c03\u7528\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u901a\u8fc7PIL\uff08Pillow\uff09\u5e93\u3001OpenCV\u5e93\u3001matplotlib\u5e93\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002Pillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u9002\u5408\u7b80\u5355\u7684\u56fe\u7247\u5904\u7406\uff1bOpenCV\u9002\u5408\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff1bmatplotlib\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u8c03\u7528\u548c\u5904\u7406\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Pillow\u5e93<\/p>\n<\/p>\n<p><p>Pillow\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\u4e4b\u4e00\uff0c\u5b83\u662fPIL\uff08Python Imaging Library\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Pillow\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\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<ol start=\"2\">\n<li>\u8c03\u7528\u548c\u663e\u793a\u56fe\u7247<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Pillow\u5e93\u8c03\u7528\u548c\u663e\u793a\u56fe\u7247\u975e\u5e38\u7b80\u5355\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Pillow\u5e93\u4e2d\u7684Image\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6253\u5f00\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5904\u7406\u56fe\u7247<\/li>\n<\/ol>\n<p><p>Pillow\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u548c\u88c1\u526a\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/p>\n<p>resized_image = image.resize((100, 100))<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u7247<\/strong><\/h2>\n<p>rotated_image = image.rotate(45)<\/p>\n<h2><strong>\u88c1\u526a\u56fe\u7247<\/strong><\/h2>\n<p>cropped_image = image.crop((10, 10, 200, 200))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u56fe\u7247\u8fdb\u884c\u57fa\u672c\u7684\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528OpenCV\u5e93<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u56fe\u50cf\u548c\u89c6\u9891\u7684\u5904\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5OpenCV\u5e93<\/li>\n<\/ol>\n<p><p>\u540c\u6837\uff0c\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<ol start=\"2\">\n<li>\u8c03\u7528\u548c\u663e\u793a\u56fe\u7247<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528OpenCV\u8c03\u7528\u548c\u663e\u793a\u56fe\u7247\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u7247<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/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><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>cv2.imread()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u56fe\u7247\uff0c<code>cv2.imshow()<\/code>\u65b9\u6cd5\u7528\u4e8e\u663e\u793a\u56fe\u7247\uff0c<code>cv2.waitKey(0)<\/code>\u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u7528\u6237\u6309\u952e\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5904\u7406\u56fe\u7247<\/li>\n<\/ol>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u8f6c\u6362\u989c\u8272\u7a7a\u95f4\u3001\u6ee4\u6ce2\u548c\u8fb9\u7f18\u68c0\u6d4b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528\u9ad8\u65af\u6ee4\u6ce2<\/strong><\/h2>\n<p>blurred_image = cv2.GaussianBlur(image, (5, 5), 0)<\/p>\n<h2><strong>\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(image, 100, 200)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7OpenCV\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528matplotlib\u5e93<\/p>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u5f62\u7684\u7efc\u5408\u5e93\uff0c\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5matplotlib\u5e93<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8c03\u7528\u548c\u663e\u793a\u56fe\u7247<\/li>\n<\/ol>\n<p><p>matplotlib\u5e93\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u56fe\u7247\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\u7247<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>mpimg.imread()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u56fe\u7247\uff0c<code>plt.imshow()<\/code>\u65b9\u6cd5\u7528\u4e8e\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>matplotlib\u5e38\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u6570\u636e\u53ef\u89c6\u5316\u548c\u56fe\u50cf\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u56fe\u50cf<\/strong><\/h2>\n<p>random_image = np.random.rand(100, 100, 3)<\/p>\n<h2><strong>\u663e\u793a\u968f\u673a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(random_image)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0enumpy\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u521b\u5efa\u548c\u663e\u793a\u968f\u673a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5982\u679c\u53ea\u9700\u8981\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u4f8b\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u6216\u88c1\u526a\uff0cPillow\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0c\u4f8b\u5982\u5bf9\u8c61\u68c0\u6d4b\u548c\u8bc6\u522b\uff0cOpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u5e76\u5c55\u793a\u56fe\u50cf\uff0cmatplotlib\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff0c\u901a\u8fc7\u4f7f\u7528Pillow\u3001OpenCV\u548cmatplotlib\u5e93\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u8c03\u7528\u548c\u5904\u7406\u56fe\u7247\u7684\u529f\u80fd\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u66f4\u597d\u5730\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\u3002\u6700\u5e38\u7528\u7684\u5e93\u5305\u62ecPIL\uff08Pillow\uff09\u548cOpenCV\u3002\u4f7f\u7528Pillow\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport matplotlib.pyplot as plt\n\n# \u52a0\u8f7d\u56fe\u7247\nimage = Image.open(&#39;path_to_your_image.jpg&#39;)\n# \u663e\u793a\u56fe\u7247\nplt.imshow(image)\nplt.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74\nplt.show()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u8fd0\u884c\u4ee3\u7801\u4e4b\u524d\u5df2\u5b89\u88c5\u76f8\u5173\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install Pillow matplotlib<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u56fe\u7247\u3002Pillow\u662f\u6700\u5e38\u89c1\u7684\u5e93\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u56fe\u50cf\u7684\u6253\u5f00\u3001\u7f16\u8f91\u548c\u4fdd\u5b58\u3002OpenCV\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u6b64\u5916\uff0cscikit-image\u548cimageio\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u56fe\u7247\u7684\u5143\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u8981\u8bfb\u53d6\u56fe\u7247\u7684\u5143\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00\u56fe\u7247\nimage = Image.open(&#39;path_to_your_image.jpg&#39;)\n# \u83b7\u53d6\u56fe\u7247\u7684\u5143\u6570\u636e\nmetadata = image.info\nprint(metadata)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u83b7\u53d6\u56fe\u7247\u7684\u683c\u5f0f\u3001\u5c3a\u5bf8\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u4fe1\u606f\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u65f6\u5df2\u5b89\u88c5Pillow\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u8c03\u7528\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u901a\u8fc7PIL\uff08Pillow\uff09\u5e93\u3001OpenCV\u5e93\u3001matplot [&hellip;]","protected":false},"author":3,"featured_media":1001258,"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\/1001253"}],"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=1001253"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001253\/revisions"}],"predecessor-version":[{"id":1001261,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001253\/revisions\/1001261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001258"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}