{"id":1001201,"date":"2024-12-27T09:58:03","date_gmt":"2024-12-27T01:58:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001201.html"},"modified":"2024-12-27T09:58:06","modified_gmt":"2024-12-27T01:58:06","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e7%99%bd%e8%89%b2%e8%83%8c%e6%99%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001201.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u767d\u8272\u80cc\u666f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075501\/8984aac5-7d38-4401-aff1-411a77650360.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001PIL\u5e93\u3001Tkinter\u5e93\u3002<\/strong> \u5728Python\u4e2d\uff0c\u80cc\u666f\u989c\u8272\u7684\u8bbe\u7f6e\u901a\u5e38\u4e0e\u56fe\u5f62\u6216\u56fe\u50cf\u5904\u7406\u76f8\u5173\u3002\u5177\u4f53\u6765\u8bf4\uff0cmatplotlib\u5e93\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\u548c\u56fe\u5f62\uff0cPIL\u5e93\u7528\u4e8e\u5904\u7406\u56fe\u50cf\uff0c\u800cTkinter\u5e93\u7528\u4e8e\u521b\u5efaGUI\u754c\u9762\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5e93\u8bbe\u7f6e\u767d\u8272\u80cc\u666f<\/p>\n<\/p>\n<p><p>matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u9002\u5408\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u5c24\u4e3a\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u751f\u6210\u62a5\u544a\u6216\u5c55\u793a\u56fe\u8868\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>figure<\/code>\u51fd\u6570\u8bbe\u7f6e\u80cc\u666f<\/li>\n<\/ol>\n<p><p>\u5728matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>figure<\/code>\u51fd\u6570\u7684<code>facecolor<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u56fe\u5f62\u7684\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>fig = plt.figure(facecolor=&#39;white&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>ax = fig.add_subplot(111)<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>ax.plot([1, 2, 3], [4, 5, 6])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>figure(facecolor=&#39;white&#39;)<\/code>\u8bbe\u7f6e\u4e86\u6574\u4e2a\u56fe\u5f62\u5bf9\u8c61\u7684\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>set_facecolor<\/code>\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u5728\u521b\u5efa\u56fe\u5f62\u65f6\u6307\u5b9a\u80cc\u666f\u989c\u8272\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>Axes<\/code>\u5bf9\u8c61\u7684<code>set_facecolor<\/code>\u65b9\u6cd5\u66f4\u6539\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b50\u56fe\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>ax.set_facecolor(&#39;white&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>ax.plot([1, 2, 3], [4, 5, 6])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>ax.set_facecolor(&#39;white&#39;)<\/code>\u5c06\u5b50\u56fe\u7684\u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u767d\u8272\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PIL\u5e93\u8bbe\u7f6e\u767d\u8272\u80cc\u666f<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3002\u5728\u4f7f\u7528PIL\u521b\u5efa\u65b0\u56fe\u50cf\u6216\u4fee\u6539\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u767d\u8272\u80cc\u666f\u7684\u65b0\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u4e2d\u7684<code>Image.new<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u767d\u8272\u80cc\u666f\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf\uff0c\u5c3a\u5bf8\u4e3a100x100<\/strong><\/h2>\n<p>white_background = Image.new(&quot;RGB&quot;, (100, 100), &quot;white&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>white_background.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Image.new(&quot;RGB&quot;, (100, 100), &quot;white&quot;)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2aRGB\u6a21\u5f0f\u7684100&#215;100\u50cf\u7d20\u7684\u767d\u8272\u80cc\u666f\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u66f4\u6539\u73b0\u6709\u56fe\u50cf\u7684\u80cc\u666f<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u6539\u73b0\u6709\u56fe\u50cf\u7684\u80cc\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u7684\u5408\u6210\u6280\u672f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u56fe\u50cf<\/strong><\/h2>\n<p>img = Image.open(&#39;example.png&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf\uff0c\u5c3a\u5bf8\u4e0e\u539f\u56fe\u4e00\u81f4<\/strong><\/h2>\n<p>white_background = Image.new(&quot;RGB&quot;, img.size, &quot;white&quot;)<\/p>\n<h2><strong>\u5408\u5e76\u56fe\u50cf<\/strong><\/h2>\n<p>img_with_white_bg = Image.alpha_composite(white_background.convert(&quot;RGBA&quot;), img.convert(&quot;RGBA&quot;))<\/p>\n<h2><strong>\u663e\u793a\u5408\u6210\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img_with_white_bg.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Image.alpha_composite<\/code>\u7528\u4e8e\u5c06\u5df2\u6709\u56fe\u50cf\u4e0e\u767d\u8272\u80cc\u666f\u5408\u6210\uff0c\u4ece\u800c\u5b9e\u73b0\u80cc\u666f\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001TKINTER\u5e93\u8bbe\u7f6e\u767d\u8272\u80cc\u666f<\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5e38\u7528\u4e8e\u5f00\u53d1\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7a97\u53e3\u5c0f\u90e8\u4ef6\u7684<code>bg<\/code>\u53c2\u6570\u6765\u66f4\u6539\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8bbe\u7f6e\u7a97\u53e3\u80cc\u666f\u989c\u8272<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5728\u521b\u5efaTkinter\u7a97\u53e3\u65f6\u6307\u5b9a\u5176\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>root.configure(bg=&#39;white&#39;)<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>root.configure(bg=&#39;white&#39;)<\/code>\u5c06Tkinter\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u767d\u8272\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8bbe\u7f6e\u5c0f\u90e8\u4ef6\u80cc\u666f\u989c\u8272<\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u8bbe\u7f6e\u6574\u4e2a\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\uff0c\u4e5f\u53ef\u4ee5\u4e3a\u5355\u72ec\u7684\u5c0f\u90e8\u4ef6\u8bbe\u7f6e\u80cc\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6807\u7b7e\uff0c\u5e76\u8bbe\u7f6e\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;Hello, World!&quot;, bg=&#39;white&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u5230\u7a97\u53e3<\/strong><\/h2>\n<p>label.pack()<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Label<\/code>\u5c0f\u90e8\u4ef6\u901a\u8fc7<code>bg=&#39;white&#39;<\/code>\u8bbe\u7f6e\u4e86\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u4e3b\u8981\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<strong>\u5bf9\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0cmatplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff1b\u5bf9\u4e8e\u56fe\u50cf\u5904\u7406\uff0cPIL\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff1b\u800c\u5bf9\u4e8eGUI\u5f00\u53d1\uff0cTkinter\u5219\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u9009\u62e9\u3002<\/strong> \u4e86\u89e3\u8fd9\u4e9b\u5de5\u5177\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u63a7\u5236\u80cc\u666f\u989c\u8272\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7f8e\u89c2\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u7684\u56fe\u5f62\u754c\u9762\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u80cc\u666f\u989c\u8272\u5c5e\u6027\u4e3a\u767d\u8272\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nroot.configure(bg=&#39;white&#39;)\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u7a97\u53e3\u3002<\/p>\n<p><strong>\u5728Python\u7ed8\u56fe\u65f6\u5982\u4f55\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u56fe\u5f62\u7684facecolor\u53c2\u6570\u6765\u8c03\u6574\u80cc\u666f\u989c\u8272\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nplt.figure(facecolor=&#39;white&#39;)\nplt.plot([1, 2, 3], [4, 5, 6])\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u8868\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4fdd\u5b58\u5e26\u6709\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u4f7f\u7528Pillow\u5e93\uff08PIL\uff09\u65f6\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf\u5e76\u4fdd\u5b58\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\nimg = Image.new(&#39;RGB&#39;, (200, 200), color=&#39;white&#39;)\nimg.save(&#39;white_background_image.png&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u521b\u5efa\u5e76\u4fdd\u5b58\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u767d\u8272\u80cc\u666f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001PIL\u5e93\u3001Tkinter\u5e93\u3002 \u5728Pytho [&hellip;]","protected":false},"author":3,"featured_media":1001214,"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\/1001201"}],"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=1001201"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001201\/revisions"}],"predecessor-version":[{"id":1001220,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001201\/revisions\/1001220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001214"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}