{"id":990025,"date":"2024-12-27T08:16:28","date_gmt":"2024-12-27T00:16:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/990025.html"},"modified":"2024-12-27T08:16:30","modified_gmt":"2024-12-27T00:16:30","slug":"python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e8%be%93%e5%87%ba%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/990025.html","title":{"rendered":"python\u5982\u4f55\u4fdd\u5b58\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\/25064937\/d2cf9e22-59aa-48b1-a2b3-f5f8cb8c90cc.webp\" alt=\"python\u5982\u4f55\u4fdd\u5b58\u8f93\u51fa\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4fdd\u5b58\u8f93\u51fa\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u5e93\u3001PIL\u5e93\u6216OpenCV\u5e93\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u9996\u5148\uff0cmatplotlib\u5e93\u662fPython\u4e2d\u7528\u4e8e\u7ed8\u56fe\u5e93\u7684\u4e00\u4e2a\u91cd\u8981\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176\u63d0\u4f9b\u7684savefig\u51fd\u6570\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u5404\u79cd\u683c\u5f0f\uff1b\u5176\u6b21\uff0cPIL\uff08Pillow\uff09\u5e93\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u652f\u6301\u5404\u79cd\u56fe\u50cf\u6587\u4ef6\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176save\u65b9\u6cd5\u4fdd\u5b58\u56fe\u50cf\uff1b\u6700\u540e\uff0cOpenCV\u5e93\u662f\u4e00\u4e2a\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0c\u4e5f\u652f\u6301\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528matplotlib\u5e93\u4fdd\u5b58\u56fe\u50cf\u7684\u65b9\u6cd5\u6700\u4e3a\u5e38\u89c1\u548c\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5e93\u4fdd\u5b58\u56fe\u7247<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u65b9\u4fbf\u5730\u521b\u5efa\u9759\u6001\u3001\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u5728Matplotlib\u4e2d\uff0c\u4fdd\u5b58\u56fe\u50cf\u662f\u4e00\u4ef6\u975e\u5e38\u7b80\u5355\u7684\u4e8b\u60c5\uff0c\u4e3b\u8981\u901a\u8fc7<code>savefig<\/code>\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u56fe\u50cf\u5e76\u4fdd\u5b58<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u50cf\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>savefig<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;output_image.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>savefig<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\uff08\u5982PNG, PDF, SVG\u7b49\uff09\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6edpi\u53c2\u6570\u6765\u8c03\u6574\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u4fdd\u5b58\u9009\u9879<\/strong><\/p>\n<\/p>\n<p><p><code>savefig<\/code>\u51fd\u6570\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u6765\u5b9a\u5236\u56fe\u50cf\u7684\u4fdd\u5b58\u3002\u4f8b\u5982\uff0c<code>bbox_inches=&#39;tight&#39;<\/code>\u53ef\u4ee5\u5728\u4fdd\u5b58\u65f6\u81ea\u52a8\u8c03\u6574\u8fb9\u754c\uff0c\u4ee5\u786e\u4fdd\u56fe\u50cf\u4e0d\u88ab\u622a\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;output_image.png&#39;, dpi=300, bbox_inches=&#39;tight&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u56fe\u50cf\u4fdd\u5b58\u7684\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001PIL\uff08Pillow\uff09\u5e93\u4fdd\u5b58\u56fe\u7247<\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3002\u901a\u8fc7Pillow\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u5404\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165Pillow\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\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><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6216\u6253\u5f00\u56fe\u50cf\u5e76\u4fdd\u5b58<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u56fe\u50cf\uff0c\u4e5f\u53ef\u4ee5\u6253\u5f00\u73b0\u6709\u7684\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u8fdb\u884c\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684RGB\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (200, 100), color = (73, 109, 137))<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e9b\u56fe\u6848<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<p>draw.text((10, 10), &quot;Hello World&quot;, fill=(255, 255, 0))<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u56fe\u50cf\u683c\u5f0f\uff0c\u5982JPEG, PNG, BMP\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001OpenCV\u5e93\u4fdd\u5b58\u56fe\u7247<\/p>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>imwrite<\/code>\u51fd\u6570\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165OpenCV\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5OpenCV\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u6216\u521b\u5efa\u56fe\u50cf\u5e76\u4fdd\u5b58<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5e93\uff0c\u53ef\u4ee5\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c\u8fdb\u884c\u5904\u7406\u540e\u4fdd\u5b58\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u56fe\u50cf\u5e76\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((100, 200, 3), dtype=np.uint8)<\/p>\n<h2><strong>\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u4e00\u4e9b\u56fe\u6848<\/strong><\/h2>\n<p>cv2.putText(image, &#39;Hello World&#39;, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output_image.png&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>OpenCV\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u4fdd\u5b58\uff0c\u5982PNG, JPEG, BMP\u7b49\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4fdd\u5b58\u8f93\u51fa\u56fe\u7247\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f7f\u7528\u7684\u5e93\u548c\u56fe\u50cf\u5904\u7406\u7684\u9700\u6c42\u3002<strong>Matplotlib\u5e93\u9002\u7528\u4e8e\u9700\u8981\u7ed8\u5236\u56fe\u8868\u548c\u6570\u636e\u53ef\u89c6\u5316\u7684\u573a\u666f\uff0cPillow\u5e93\u9002\u7528\u4e8e\u56fe\u50cf\u7684\u57fa\u672c\u5904\u7406\u548c\u8f6c\u6362\uff0c\u800cOpenCV\u5e93\u5219\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/strong>\u901a\u8fc7\u5408\u7406\u5730\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6ee1\u8db3\u5404\u79cd\u56fe\u50cf\u4fdd\u5b58\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u751f\u6210\u7684\u56fe\u50cf\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u4fdd\u5b58\u8f93\u51fa\u56fe\u50cf\uff0c\u5982PIL\uff08Pillow\uff09\u3001Matplotlib\u6216OpenCV\u3002\u5bf9\u4e8ePIL\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Image.save()<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6307\u5b9a\u683c\u5f0f\u3002Matplotlib\u5219\u53ef\u4ee5\u4f7f\u7528<code>plt.savefig()<\/code>\u51fd\u6570\u6765\u4fdd\u5b58\u7ed8\u5236\u7684\u56fe\u50cf\u3002\u786e\u4fdd\u5728\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\u6307\u5b9a\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u53ca\u6240\u9700\u7684\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528Matplotlib\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>Matplotlib\u652f\u6301\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u5982PNG\u3001JPG\u3001SVG\u7b49\u3002\u5728\u4f7f\u7528<code>plt.savefig()<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u540d\u540e\u7f00\u6307\u5b9a\u6240\u9700\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.savefig(&#39;output.png&#39;)<\/code>\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u3002\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\uff0cPNG\u9002\u5408\u900f\u660e\u80cc\u666f\uff0c\u800cJPG\u9002\u5408\u7167\u7247\u7c7b\u56fe\u50cf\u3002<\/p>\n<p><strong>\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u5982\u4f55\u63a7\u5236\u56fe\u50cf\u7684\u5206\u8fa8\u7387\uff1f<\/strong><br \/>\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>dpi<\/code>\u53c2\u6570\u6765\u63a7\u5236\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u3002\u4f8b\u5982\uff0c\u5728Matplotlib\u4e2d\uff0c\u4f7f\u7528<code>plt.savefig(&#39;output.png&#39;, dpi=300)<\/code>\u53ef\u4ee5\u5c06\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u8bbe\u7f6e\u4e3a300 DPI\uff0c\u8fd9\u5728\u6253\u5370\u6216\u9ad8\u8d28\u91cf\u663e\u793a\u65f6\u5c24\u4e3a\u91cd\u8981\u3002\u786e\u4fdd\u6839\u636e\u7528\u9014\u9009\u62e9\u9002\u5f53\u7684\u5206\u8fa8\u7387\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4fdd\u5b58\u8f93\u51fa\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u5e93\u3001PIL\u5e93\u6216OpenCV\u5e93\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u9996\u5148\uff0cm [&hellip;]","protected":false},"author":3,"featured_media":990031,"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\/990025"}],"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=990025"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990025\/revisions"}],"predecessor-version":[{"id":990034,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990025\/revisions\/990034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/990031"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=990025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=990025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=990025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}