{"id":1013897,"date":"2024-12-27T11:51:47","date_gmt":"2024-12-27T03:51:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013897.html"},"modified":"2024-12-27T11:51:49","modified_gmt":"2024-12-27T03:51:49","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1013897.html","title":{"rendered":"\u7528python\u5982\u4f55\u8f93\u51fa\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094153\/65ae5675-503e-4f0f-837b-96df6d1d45d4.webp\" alt=\"\u7528python\u5982\u4f55\u8f93\u51fa\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8f93\u51fa\u56fe\u7247\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528OpenCV\u5e93<\/strong>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528PIL\u5e93\uff08Python Imaging Library\uff09\u6216\u5176\u5206\u652fPillow\u3002Pillow\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u5e76\u63d0\u4f9b\u4e30\u5bcc\u7684\u56fe\u50cf\u64cd\u4f5c\u529f\u80fd\u3002\u8981\u4f7f\u7528Pillow\u8f93\u51fa\u56fe\u7247\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u52a0\u8f7d\u3001\u5904\u7406\u548c\u4fdd\u5b58\u56fe\u50cf\u6765\u5b9e\u73b0\u3002\u8fd9\u4e00\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93<\/p>\n<\/p>\n<p><p>Pillow\u662fPython\u7684\u4e00\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u56fe\u50cf\u6253\u5f00\u3001\u5904\u7406\u548c\u4fdd\u5b58\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528Pillow\u8f93\u51fa\u56fe\u7247\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5\u548c\u5bfc\u5165Pillow<\/p>\n<\/p>\n<ul>\n<li>\u8981\u4f7f\u7528Pillow\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a\n<pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165Pillow\u4e2d\u7684Image\u6a21\u5757\uff1a\n<pre><code class=\"language-python\">from PIL import Image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528Pillow\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6253\u5f00\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5728\u9ed8\u8ba4\u7684\u56fe\u50cf\u67e5\u770b\u5668\u4e2d\u663e\u793a\uff1a\n<pre><code class=\"language-python\">image = Image.open(&#39;example.jpg&#39;)  # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>image.show()  # \u663e\u793a\u56fe\u50cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\uff1a\n<pre><code class=\"language-python\">image.save(&#39;output.png&#39;)  # \u4fdd\u5b58\u56fe\u50cf\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u56fe\u50cf\u5904\u7406<\/p>\n<\/p>\n<ul>\n<li>Pillow\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u56fe\u50cf\u7684\u65cb\u8f6c\u3001\u88c1\u526a\u3001\u8c03\u6574\u5927\u5c0f\u7b49\uff1a\n<pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>rotated_image = image.rotate(45)<\/p>\n<p>rotated_image.show()<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = image.resize((100, 100))<\/p>\n<p>resized_image.show()<\/p>\n<h2><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/h2>\n<p>cropped_image = image.crop((10, 10, 200, 200))<\/p>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Matplotlib\u5e93<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406\u548c\u663e\u793a\u56fe\u50cf\u3002\u4e0ePillow\u4e0d\u540c\uff0cMatplotlib\u4e3b\u8981\u7528\u4e8e\u5c55\u793a\u548c\u7ed8\u5236\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5Matplotlib\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\uff1a\n<pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165pyplot\u6a21\u5757\uff1a\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528Matplotlib\uff0c\u53ef\u4ee5\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u663e\u793a\u5728\u7a97\u53e3\u4e2d\uff1a\n<pre><code class=\"language-python\">img = mpimg.imread(&#39;example.jpg&#39;)  # \u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>plt.imshow(img)  # \u663e\u793a\u56fe\u50cf<\/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<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<ul>\n<li>Matplotlib\u53ef\u4ee5\u5c06\u663e\u793a\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1a\n<pre><code class=\"language-python\">plt.imsave(&#39;output.png&#39;, img)  # \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u56fe\u50cf\u5904\u7406<\/p>\n<\/p>\n<ul>\n<li>\u867d\u7136Matplotlib\u4e0d\u5982Pillow\u90a3\u6837\u4e13\u6ce8\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\uff0c\u4f8b\u5982\u8c03\u6574\u56fe\u50cf\u989c\u8272\uff1a\n<pre><code class=\"language-python\"># \u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>plt.imshow(img, cmap=&#39;gray&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528OpenCV\u5e93<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u4e30\u5bcc\u7684\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5\u548c\u5bfc\u5165OpenCV<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5OpenCV\uff1a\n<pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165cv2\u6a21\u5757\uff1a\n<pre><code class=\"language-python\">import cv2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528OpenCV\u8bfb\u53d6\u56fe\u50cf\u5e76\u5728\u7a97\u53e3\u4e2d\u663e\u793a\uff1a\n<pre><code class=\"language-python\">img = cv2.imread(&#39;example.jpg&#39;)  # \u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>cv2.imshow(&#39;Image&#39;, img)  # \u663e\u793a\u56fe\u50cf<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)  # \u7b49\u5f85\u6309\u952e<\/p>\n<p>cv2.destroyAllWindows()  # \u5173\u95ed\u7a97\u53e3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<ul>\n<li>OpenCV\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1a\n<pre><code class=\"language-python\">cv2.imwrite(&#39;output.png&#39;, img)  # \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u56fe\u50cf\u5904\u7406<\/p>\n<\/p>\n<ul>\n<li>OpenCV\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u56fe\u50cf\u7684\u7f29\u653e\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\uff1a\n<pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<\/p>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<h2><strong>\u7f29\u653e\u56fe\u50cf<\/strong><\/h2>\n<p>resized_image = cv2.resize(img, (100, 100))<\/p>\n<p>cv2.imshow(&#39;Resized Image&#39;, resized_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002\u4f8b\u5982\uff0cPillow\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0cMatplotlib\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800cOpenCV\u5219\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u4efb\u52a1\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u4e30\u5bcc\u7684\u56fe\u50cf\u8f93\u51fa\u548c\u5904\u7406\u529f\u80fd\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 \/>\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982PIL\uff08Pillow\uff09\u6216OpenCV\u6765\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\u3002\u4f7f\u7528PIL\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>Image.open()<\/code>\u65b9\u6cd5\u6253\u5f00\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>Image.show()<\/code>\u6765\u663e\u793a\u3002\u4f7f\u7528OpenCV\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>cv2.imread()<\/code>\u8bfb\u53d6\u56fe\u7247\uff0c\u5e76\u901a\u8fc7<code>cv2.imshow()<\/code>\u6765\u5c55\u793a\u3002\u786e\u4fdd\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pillow<\/code>\u6216<code>pip install opencv-python<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5904\u7406\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u4e2a\u6d41\u884c\u7684\u5e93\u53ef\u4ee5\u5904\u7406\u56fe\u7247\uff0c\u6bd4\u5982Pillow\u3001OpenCV\u3001Matplotlib\u548cImageio\u7b49\u3002Pillow\u662f\u4e00\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff1bOpenCV\u5219\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u529f\u80fd\uff1bMatplotlib\u4e0d\u4ec5\u53ef\u4ee5\u7ed8\u56fe\uff0c\u8fd8\u80fd\u663e\u793a\u56fe\u50cf\uff1b\u800cImageio\u5219\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u80fd\u591f\u66f4\u6709\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u51fa\u7684\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pillow\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730\u3002\u901a\u8fc7<code>Image.save()<\/code>\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\u3002\u4f8b\u5982\uff0c<code>image.save(&#39;output.jpg&#39;)<\/code>\u5c06\u4f1a\u628a\u56fe\u7247\u4fdd\u5b58\u4e3aJPEG\u683c\u5f0f\u3002\u5982\u679c\u4f7f\u7528OpenCV\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite()<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u7247\uff0c\u786e\u4fdd\u6307\u5b9a\u597d\u6587\u4ef6\u8def\u5f84\u548c\u683c\u5f0f\u3002\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u56fe\u7247\u7684\u8d28\u91cf\u548c\u517c\u5bb9\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8f93\u51fa\u56fe\u7247\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0 [&hellip;]","protected":false},"author":3,"featured_media":1013903,"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\/1013897"}],"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=1013897"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013897\/revisions"}],"predecessor-version":[{"id":1013906,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013897\/revisions\/1013906"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1013903"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1013897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1013897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1013897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}