{"id":1131171,"date":"2025-01-08T20:46:19","date_gmt":"2025-01-08T12:46:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131171.html"},"modified":"2025-01-08T20:46:23","modified_gmt":"2025-01-08T12:46:23","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%85%ad%e8%be%b9%e5%bd%a2%e4%b8%ad%e5%86%99%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131171.html","title":{"rendered":"python\u5982\u4f55\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101022\/ab2a46c9-7499-4f4a-a183-629133cba373.webp\" alt=\"python\u5982\u4f55\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57\" \/><\/p>\n<p><p> <strong>Python\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57\u7684\u5b9e\u73b0\u65b9\u6cd5<\/strong>\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528PIL\u5e93\u7ed8\u5236\u56fe\u50cf\u3001\u5229\u7528Matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62\u3001\u7ed3\u5408Tkinter\u5e93\u8fdb\u884cGUI\u7f16\u7a0b<\/strong>\u7b49\u3002<strong>PIL\u5e93\u7ed8\u5236\u56fe\u50cf<\/strong>\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8be6\u7ec6\u4ecb\u7ecd\u5176\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\u5e93\u7ed8\u5236\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u4fee\u6539\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5PIL\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5PIL\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\uff0c\u8fd9\u662fPIL\u7684\u4e00\u4e2a\u53cb\u597d\u7684\u5206\u652f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u516d\u8fb9\u5f62\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528PIL\u5e93\u521b\u5efa\u4e00\u4e2a\u516d\u8fb9\u5f62\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u6587\u5b57\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ee3\u7801\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u50cf<\/strong><\/h2>\n<p>image_size = (400, 400)<\/p>\n<p>image = Image.new(&quot;RGB&quot;, image_size, &quot;white&quot;)<\/p>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u5b9a\u4e49\u516d\u8fb9\u5f62\u7684\u9876\u70b9<\/strong><\/h2>\n<p>hexagon = [(200, 50), (300, 100), (300, 200), (200, 250), (100, 200), (100, 100)]<\/p>\n<h2><strong>\u7ed8\u5236\u516d\u8fb9\u5f62<\/strong><\/h2>\n<p>draw.polygon(hexagon, outline=&quot;black&quot;, fill=&quot;lightblue&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u5b57\u4f53<\/strong><\/h2>\n<p>font = ImageFont.truetype(&quot;arial.ttf&quot;, 24)<\/p>\n<h2><strong>\u8ba1\u7b97\u6587\u672c\u4f4d\u7f6e<\/strong><\/h2>\n<p>text = &quot;Hello&quot;<\/p>\n<p>text_width, text_height = draw.textsize(text, font=font)<\/p>\n<p>text_x = (image_size[0] - text_width) \/\/ 2<\/p>\n<p>text_y = (image_size[1] - text_height) \/\/ 2<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>draw.text((text_x, text_y), text, fill=&quot;black&quot;, font=font)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&quot;hexagon_text.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a400&#215;400\u7684\u767d\u8272\u80cc\u666f\u56fe\u50cf\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u516d\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807\uff0c\u5e76\u4f7f\u7528<code>draw.polygon<\/code>\u65b9\u6cd5\u7ed8\u5236\u516d\u8fb9\u5f62\u3002\u63a5\u7740\uff0c\u901a\u8fc7<code>ImageFont.truetype<\/code>\u65b9\u6cd5\u5b9a\u4e49\u5b57\u4f53\uff0c\u5e76\u8ba1\u7b97\u6587\u672c\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u4ee5\u786e\u4fdd\u6587\u672c\u5728\u516d\u8fb9\u5f62\u7684\u4e2d\u5fc3\u4f4d\u7f6e\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>draw.text<\/code>\u65b9\u6cd5\u5728\u516d\u8fb9\u5f62\u4e2d\u7ed8\u5236\u6587\u672c\uff0c\u5e76\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528Matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Matplotlib\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u516d\u8fb9\u5f62\u56fe\u5f62\u5e76\u5199\u5165\u6587\u5b57<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u5e93\u5b9e\u73b0\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.patches as patches<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u8f74<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u5b9a\u4e49\u516d\u8fb9\u5f62\u7684\u9876\u70b9<\/strong><\/h2>\n<p>hexagon = patches.RegularPolygon((0.5, 0.5), numVertices=6, radius=0.4, orientation=np.radians(30), edgecolor=&#39;black&#39;, facecolor=&#39;lightblue&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u516d\u8fb9\u5f62\u5230\u8f74<\/strong><\/h2>\n<p>ax.add_patch(hexagon)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u672c<\/strong><\/h2>\n<p>plt.text(0.5, 0.5, &#39;Hello&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;black&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8f74\u7684\u8303\u56f4\u548c\u9690\u85cf\u8f74<\/strong><\/h2>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<p>ax.axis(&#39;off&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u548c\u8f74\uff0c\u7136\u540e\u901a\u8fc7<code>RegularPolygon<\/code>\u65b9\u6cd5\u5b9a\u4e49\u4e86\u516d\u8fb9\u5f62\u7684\u9876\u70b9\uff0c\u5e76\u6dfb\u52a0\u5230\u8f74\u4e0a\u3002\u63a5\u7740\uff0c\u901a\u8fc7<code>plt.text<\/code>\u65b9\u6cd5\u5728\u516d\u8fb9\u5f62\u7684\u4e2d\u5fc3\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\uff0c\u5e76\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>plt.show<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408Tkinter\u5e93\u8fdb\u884cGUI\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Tkinter\u5e93\u7ed8\u5236\u56fe\u5f62\u5e76\u5199\u5165\u6587\u5b57<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Tkinter\u5e93\u5b9e\u73b0\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Hexagon Text&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u753b\u5e03<\/strong><\/h2>\n<p>canvas = tk.Canvas(root, width=400, height=400, bg=&quot;white&quot;)<\/p>\n<p>canvas.pack()<\/p>\n<h2><strong>\u5b9a\u4e49\u516d\u8fb9\u5f62\u7684\u9876\u70b9<\/strong><\/h2>\n<p>hexagon = [(200, 50), (300, 100), (300, 200), (200, 250), (100, 200), (100, 100)]<\/p>\n<h2><strong>\u7ed8\u5236\u516d\u8fb9\u5f62<\/strong><\/h2>\n<p>canvas.create_polygon(hexagon, outline=&quot;black&quot;, fill=&quot;lightblue&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u672c<\/strong><\/h2>\n<p>canvas.create_text(200, 150, text=&quot;Hello&quot;, font=(&quot;Arial&quot;, 24), fill=&quot;black&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u7a97\u53e3<\/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>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u901a\u8fc7<code>Canvas<\/code>\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u753b\u5e03\u3002\u7136\u540e\uff0c\u5b9a\u4e49\u4e86\u516d\u8fb9\u5f62\u7684\u9876\u70b9\uff0c\u5e76\u4f7f\u7528<code>create_polygon<\/code>\u65b9\u6cd5\u7ed8\u5236\u516d\u8fb9\u5f62\u3002\u63a5\u7740\uff0c\u901a\u8fc7<code>create_text<\/code>\u65b9\u6cd5\u5728\u516d\u8fb9\u5f62\u7684\u4e2d\u5fc3\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\uff0c\u5e76\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>mainloop<\/code>\u65b9\u6cd5\u8fd0\u884c\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u516d\u8fb9\u5f62\u4e2d\u7ed8\u5236\u6587\u672c\u3002PIL\u5e93\u9002\u5408\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0cMatplotlib\u5e93\u9002\u5408\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\uff0cTkinter\u5e93\u9002\u5408\u7528\u4e8eGUI\u7f16\u7a0b\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0Python\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57\u9700\u8981\u7528\u5230\u4ec0\u4e48Python\u5e93\uff1f<\/strong><br \/>\u8981\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57\uff0c\u63a8\u8350\u4f7f\u7528\u4e00\u4e9b\u56fe\u5f62\u5904\u7406\u5e93\uff0c\u5982Matplotlib\u3001Pygame\u6216Turtle\u3002\u8fd9\u4e9b\u5e93\u90fd\u63d0\u4f9b\u4e86\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c\u7684\u529f\u80fd\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u5728\u516d\u8fb9\u5f62\u5185\u90e8\u6dfb\u52a0\u6587\u5b57\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u516d\u8fb9\u5f62\u4e2d\u8c03\u6574\u6587\u5b57\u7684\u4f4d\u7f6e\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u5b57\u7684\u5750\u6807\u4f4d\u7f6e\u6765\u8c03\u6574\u6587\u5b57\u5728\u516d\u8fb9\u5f62\u4e2d\u7684\u4f4d\u7f6e\u3002\u5927\u591a\u6570\u56fe\u5f62\u5e93\u5141\u8bb8\u60a8\u6307\u5b9a\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u989c\u8272\u7b49\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>text<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u6587\u5b57\u7684\u5177\u4f53\u4f4d\u7f6e\u548c\u6837\u5f0f\u53c2\u6570\uff0c\u4ee5\u786e\u4fdd\u6587\u5b57\u5728\u516d\u8fb9\u5f62\u4e2d\u5c45\u4e2d\u6216\u6309\u7167\u60a8\u60f3\u8981\u7684\u65b9\u5f0f\u6392\u5217\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u516d\u8fb9\u5f62\u7684\u6bcf\u4e2a\u8fb9\u4e0a\u5199\u5b57\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u516d\u8fb9\u5f62\u6bcf\u6761\u8fb9\u7684\u4e2d\u70b9\u6765\u786e\u5b9a\u6587\u5b57\u7684\u4f4d\u7f6e\u3002\u901a\u8fc7\u5728\u6bcf\u6761\u8fb9\u7684\u4e2d\u70b9\u4e0a\u6dfb\u52a0\u6587\u672c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u516d\u8fb9\u5f62\u7684\u6bcf\u6761\u8fb9\u90fd\u6709\u6587\u5b57\u7684\u6548\u679c\u3002\u8fd9\u6837\u7684\u6548\u679c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c\u5750\u6807\u8ba1\u7b97\u6765\u5b9e\u73b0\uff0c\u786e\u4fdd\u6587\u5b57\u4e0e\u8fb9\u7684\u5bf9\u79f0\u548c\u7f8e\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u516d\u8fb9\u5f62\u4e2d\u5199\u5b57\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528PIL\u5e93\u7ed8\u5236\u56fe\u50cf\u3001\u5229\u7528Matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1131178,"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\/1131171"}],"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=1131171"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131171\/revisions"}],"predecessor-version":[{"id":1131182,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131171\/revisions\/1131182"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131178"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}