{"id":1063487,"date":"2024-12-31T16:01:04","date_gmt":"2024-12-31T08:01:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063487.html"},"modified":"2024-12-31T16:01:06","modified_gmt":"2024-12-31T08:01:06","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%af%bc%e5%85%a5%e4%b8%80%e5%bc%a0%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1063487.html","title":{"rendered":"\u5982\u4f55\u7528python\u5bfc\u5165\u4e00\u5f20\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/65fe0976-a941-4a10-ad76-3777306ebcd6.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u5bfc\u5165\u4e00\u5f20\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5bfc\u5165\u56fe\u7247\u5230Python\u7684\u6838\u5fc3\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528matplotlib\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c<strong>PIL\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PIL\u5e93\u5bfc\u5165\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;path_to_your_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\u5e93\u5bfc\u5165\u56fe\u7247<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\u4e4b\u4e00\u3002PIL\u5e93\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u56fe\u50cf\u3002\u867d\u7136PIL\u5e93\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u4f46\u5b83\u7684\u5206\u652fPillow\u4ecd\u5728\u79ef\u6781\u7ef4\u62a4\u548c\u5f00\u53d1\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528PIL\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u5b89\u88c5\u548c\u4f7f\u7528Pillow\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pillow\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u6b63\u786e\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\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. \u4f7f\u7528Pillow\u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165Pillow\u5e93\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bfc\u5165\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;path_to_your_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86PIL\u5e93\u4e2d\u7684Image\u6a21\u5757\u3002\u7136\u540e\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u56fe\u7247\u6587\u4ef6\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>img.show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV\u5e93\u5bfc\u5165\u56fe\u7247<\/h3>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002OpenCV\u63d0\u4f9b\u4e86\u591a\u79cd\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u540c\u65f6\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u5bfc\u5165\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5OpenCV\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\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><h4>2. \u4f7f\u7528OpenCV\u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165OpenCV\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u56fe\u7247\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>img = cv2.imread(&#39;path_to_your_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\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)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86OpenCV\u5e93\u3002\u7136\u540e\u4f7f\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u56fe\u7247\u6587\u4ef6\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>cv2.imshow()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u7247\uff0c\u5e76\u4f20\u9012\u7a97\u53e3\u540d\u79f0\u548c\u56fe\u7247\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>cv2.waitKey()<\/code>\u51fd\u6570\u7b49\u5f85\u7528\u6237\u6309\u952e\u64cd\u4f5c\uff0c\u5e76\u4f7f\u7528<code>cv2.destroyAllWindows()<\/code>\u51fd\u6570\u5173\u95ed\u6240\u6709\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528matplotlib\u5e93\u5bfc\u5165\u56fe\u7247<\/h3>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u7684Python\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u867d\u7136matplotlib\u4e3b\u8981\u7528\u4e8e\u7ed8\u56fe\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\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<p><h4>2. \u4f7f\u7528matplotlib\u5bfc\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165matplotlib\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\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>img = mpimg.imread(&#39;path_to_your_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>plt.imshow(img)<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\u548cimage\u6a21\u5757\u3002\u7136\u540e\u4f7f\u7528<code>mpimg.imread()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u56fe\u7247\u6587\u4ef6\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>plt.imshow()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u7247\uff0c\u5e76\u4f20\u9012\u56fe\u7247\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>plt.axis(&#39;off&#39;)<\/code>\u5173\u95ed\u5750\u6807\u8f74\uff0c\u5e76\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u56fe\u7247\u5230Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002PIL\u5e93\uff08Pillow\uff09\u662f\u6700\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u64cd\u4f5c\u529f\u80fd\uff1bOpenCV\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff1bmatplotlib\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5bfc\u5165\u56fe\u7247\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5bfc\u5165\u56fe\u7247\u7684\u57fa\u672c\u6b65\u9aa4\u90fd\u662f\u76f8\u4f3c\u7684\uff1a\u9996\u5148\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u7136\u540e\u4f7f\u7528\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\u8bfb\u53d6\u56fe\u7247\u6587\u4ef6\uff0c\u6700\u540e\u663e\u793a\u56fe\u7247\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528Python\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528PIL\u5e93\u5bfc\u5165\u56fe\u7247\uff1f<\/strong><br \/>\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bfc\u5165\u548c\u5904\u7406\u56fe\u50cf\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Pillow\u5e93\uff08PIL\u7684\u4e00\u4e2a\u5206\u652f\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u5bfc\u5165\u56fe\u7247\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u5bfc\u5165\u56fe\u50cf\nimage = Image.open(&#39;your_image.jpg&#39;)\nimage.show()  # \u663e\u793a\u56fe\u50cf\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u5bfc\u5165\u5e76\u67e5\u770b\u56fe\u50cf\u3002<\/p>\n<p><strong>\u5bfc\u5165\u56fe\u7247\u540e\uff0c\u5982\u4f55\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u56fe\u7247\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u5404\u79cd\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\uff0c\u4f8b\u5982\u8c03\u6574\u5927\u5c0f\u3001\u88c1\u526a\u548c\u65cb\u8f6c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a  <\/p>\n<ul>\n<li>\u8c03\u6574\u5927\u5c0f\uff1a<code>resized_image = image.resize((width, height))<\/code>  <\/li>\n<li>\u88c1\u526a\uff1a<code>cropped_image = image.crop((left, upper, right, lower))<\/code>  <\/li>\n<li>\u65cb\u8f6c\uff1a<code>rotated_image = image.rotate(angle)<\/code><br \/>\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u56fe\u50cf\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u663e\u793a\u591a\u4e2a\u56fe\u7247\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5bfc\u5165\u548c\u663e\u793a\u591a\u4e2a\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5047\u8bbe\u4f60\u6709\u591a\u4e2a\u56fe\u50cf\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfrom PIL import Image\n\n# \u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\nfolder_path = &#39;path_to_your_images&#39;\nfor filename in os.listdir(folder_path):\n    if filename.endswith(&#39;.jpg&#39;) or filename.endswith(&#39;.png&#39;):\n        image_path = os.path.join(folder_path, filename)\n        image = Image.open(image_path)\n        image.show()  # \u663e\u793a\u56fe\u50cf\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5904\u7406\u548c\u663e\u793a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5bfc\u5165\u56fe\u7247\u5230Python\u7684\u6838\u5fc3\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528matplotlib\u5e93\u3002 \u5176\u4e2d\uff0cPI [&hellip;]","protected":false},"author":3,"featured_media":1063496,"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\/1063487"}],"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=1063487"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063487\/revisions"}],"predecessor-version":[{"id":1063497,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063487\/revisions\/1063497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1063496"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1063487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1063487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1063487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}