{"id":958459,"date":"2024-12-27T03:28:11","date_gmt":"2024-12-26T19:28:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958459.html"},"modified":"2024-12-27T03:28:14","modified_gmt":"2024-12-26T19:28:14","slug":"python%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8img%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/958459.html","title":{"rendered":"python\u5982\u4f55\u5b58\u50a8img\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101533\/e2b362a2-07d2-4330-a915-227678c2f2a1.webp\" alt=\"python\u5982\u4f55\u5b58\u50a8img\u683c\u5f0f\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u5728Python\u4e2d\u5b58\u50a8img\u683c\u5f0f\u7684\u65b9\u6cd5\u6709<strong>\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93<\/strong>\u7b49\u3002<strong>\u4f7f\u7528PIL\u5e93<\/strong>\u662f\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3aPIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u5404\u79cd\u56fe\u50cf\u683c\u5f0f\u3002\u901a\u8fc7PIL\u5e93\u7684Image\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u52a0\u8f7d\u56fe\u50cf\u5e76\u901a\u8fc7<code>save()<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3aimg\u683c\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u64cd\u4f5c\u7b80\u5355\u4e14\u652f\u6301\u591a\u79cd\u683c\u5f0f\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b58\u50a8img\u683c\u5f0f\u7684\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIL\u5e93<\/p>\n<\/p>\n<p><p>PIL\u5e93\u662fPython\u4e2d\u5904\u7406\u56fe\u50cf\u7684\u7ecf\u5178\u5e93\uff0c\u867d\u7136PIL\u5df2\u7ecf\u4e0d\u518d\u7ef4\u62a4\uff0c\u4f46\u5b83\u7684\u4e00\u4e2a\u5206\u652fPillow\u4ecd\u5728\u79ef\u6781\u5f00\u53d1\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u517c\u5bb9\u6027\u548c\u529f\u80fd\u3002Pillow\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7528\u6765\u6253\u5f00\u3001\u5904\u7406\u548c\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u5e93<\/p>\n<p>\u8981\u4f7f\u7528Pillow\u5e93\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u5bfc\u5165Pillow\u4e2d\u7684Image\u6a21\u5757\u6765\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>Pillow\u5e93\u4e2d\uff0c\u4f7f\u7528Image\u6a21\u5757\u53ef\u4ee5\u6253\u5f00\u548c\u4fdd\u5b58\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4ee5img\u683c\u5f0f\u4fdd\u5b58<\/strong><\/h2>\n<p>img.save(&#39;example.img&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e86\u4e00\u4e2aJPEG\u683c\u5f0f\u7684\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u4ee5img\u683c\u5f0f\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528OpenCV\u5e93<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9886\u57df\u3002\u5b83\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5OpenCV\u5e93<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5OpenCV\u5e93\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf\u540c\u6837\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aimg\u683c\u5f0f<\/strong><\/h2>\n<p>cv2.imwrite(&#39;example.img&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>cv2.imwrite()<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3aimg\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Matplotlib\u5e93<\/p>\n<\/p>\n<p><p>Matplotlib\u4e3b\u8981\u7528\u4e8e\u7ed8\u56fe\u5e93\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u7684\u7b80\u5355\u4efb\u52a1\u3002\u867d\u7136\u4e0d\u662f\u4e13\u95e8\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528\u5176\u529f\u80fd\u6765\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Matplotlib\u5e93<\/p>\n<p>\u5b89\u88c5Matplotlib\u5e93\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Matplotlib\u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Matplotlib\u4fdd\u5b58\u56fe\u50cf\uff1a<\/li>\n<\/p>\n<\/ol>\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<\/strong><\/h2>\n<p>img = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aimg\u683c\u5f0f<\/strong><\/h2>\n<p>plt.imsave(&#39;example.img&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>mpimg.imread()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>plt.imsave()<\/code>\u51fd\u6570\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406img\u683c\u5f0f\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406img\u683c\u5f0f\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u517c\u5bb9\u6027\u95ee\u9898\uff1a\u786e\u4fdd\u4f60\u6240\u4f7f\u7528\u7684\u5e93\u80fd\u591f\u652f\u6301img\u683c\u5f0f\u3002\u5e76\u4e0d\u662f\u6240\u6709\u7684\u5e93\u90fd\u652f\u6301\u76f4\u63a5\u8bfb\u53d6\u548c\u4fdd\u5b58img\u683c\u5f0f\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u524d\u9700\u8981\u67e5\u9605\u6587\u6863\u786e\u8ba4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u56fe\u50cf\u8d28\u91cf\uff1a\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u8d28\u91cf\u7684\u9009\u62e9\u3002\u67d0\u4e9b\u5e93\u5141\u8bb8\u5728\u4fdd\u5b58\u65f6\u8bbe\u7f6e\u8d28\u91cf\u53c2\u6570\uff0c\u4ee5\u4fbf\u5728\u6587\u4ef6\u5927\u5c0f\u548c\u56fe\u50cf\u8d28\u91cf\u4e4b\u95f4\u8fdb\u884c\u5e73\u8861\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u683c\u5f0f\u8f6c\u6362\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u5176\u4ed6\u683c\u5f0f\u7684\u56fe\u50cf\u8f6c\u6362\u4e3aimg\u683c\u5f0f\u3002\u8fd9\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u6b65\u9aa4\uff0c\u4f8b\u5982\u4f7f\u7528\u4e2d\u95f4\u683c\u5f0f\u8fdb\u884c\u8fc7\u6e21\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u56fe\u50cf\u5b58\u50a8\u4e3aimg\u683c\u5f0f\u3002\u65e0\u8bba\u662f\u4f7f\u7528Pillow\u3001OpenCV\u8fd8\u662fMatplotlib\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u3002<strong>Pillow\u5e93\u56e0\u5176\u7b80\u5355\u6613\u7528\u6027\u548c\u5e7f\u6cdb\u7684\u683c\u5f0f\u652f\u6301<\/strong>\uff0c\u901a\u5e38\u662f\u9996\u9009\u3002\u638c\u63e1\u8fd9\u4e9b\u5e93\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u56fe\u50cf\u5904\u7406\u548c\u5b58\u50a8\u4e2d\u6e38\u5203\u6709\u4f59\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aimg\u683c\u5f0f\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u50cf\u4e3aimg\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u5e76\u4fdd\u5b58\u56fe\u50cf\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u52a0\u8f7d\u56fe\u50cf\nimage = Image.open(&#39;example.jpg&#39;)\n\n# \u4fdd\u5b58\u4e3aimg\u683c\u5f0f\nimage.save(&#39;output.img&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u4ece\u5176\u4ed6\u683c\u5f0f\u8f6c\u6362\u4e3aimg\u683c\u5f0f\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u683c\u5f0f\u8f6c\u6362\u3002\u901a\u8fc7Pillow\u5e93\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6\u4efb\u4f55\u652f\u6301\u7684\u56fe\u50cf\u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aimg\u683c\u5f0f\u3002\u53ea\u9700\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u6587\u4ef6\u6269\u5c55\u540d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">image.save(&#39;converted_image.img&#39;)\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cimg\u683c\u5f0f\u7684\u517c\u5bb9\u6027\u53ef\u80fd\u56e0\u5177\u4f53\u9700\u6c42\u800c\u5f02\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u9996\u5148\uff0c\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u5982\u679c\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u4fdd\u5b58\u56fe\u50cf\u3002\u5176\u6b21\uff0c\u68c0\u67e5\u56fe\u50cf\u683c\u5f0f\u662f\u5426\u652f\u6301\u3002\u67d0\u4e9b\u683c\u5f0f\u53ef\u80fd\u5728\u8f6c\u6362\u65f6\u4f1a\u4e22\u5931\u7279\u5b9a\u7684\u4fe1\u606f\u6216\u8d28\u91cf\u3002\u6700\u540e\uff0c\u786e\u4fdd\u60a8\u6709\u8db3\u591f\u7684\u6743\u9650\u6765\u5199\u5165\u76ee\u6807\u76ee\u5f55\uff0c\u4ee5\u907f\u514d\u6743\u9650\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\u5b58\u50a8img\u683c\u5f0f\u7684\u65b9\u6cd5\u6709\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":958460,"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\/958459"}],"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=958459"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958459\/revisions"}],"predecessor-version":[{"id":958461,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958459\/revisions\/958461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958460"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}