{"id":948613,"date":"2024-12-27T00:05:14","date_gmt":"2024-12-26T16:05:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948613.html"},"modified":"2024-12-27T00:05:16","modified_gmt":"2024-12-26T16:05:16","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8draw","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948613.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528draw"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083919\/444b18e4-a12f-4e3e-9404-bded9e302c43.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528draw\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4f7f\u7528draw\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PIL\u5e93\u521b\u5efa\u548c\u7ed8\u5236\u56fe\u50cf\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3001\u4f7f\u7528Turtle\u6a21\u5757\u8fdb\u884c\u7b80\u5355\u7ed8\u56fe\u3002<\/strong>\u5176\u4e2d\uff0cPIL\u5e93\u662fPython Imaging Library\u7684\u7b80\u79f0\uff0c\u4e3b\u8981\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u7ed8\u5236\uff1bMatplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff1bTurtle\u6a21\u5757\u5219\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7f16\u7a0b\u548c\u7b80\u5355\u7ed8\u56fe\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u8fdb\u884c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PIL\u5e93\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5141\u8bb8\u7528\u6237\u521b\u5efa\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u56fe\u50cf\u3002\u867d\u7136PIL\u539f\u59cb\u7248\u672c\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u4f46Pillow\u4f5c\u4e3a\u5b83\u7684\u4e00\u4e2a\u5206\u652f\u7ee7\u7eed\u7ef4\u62a4\uff0c\u5e76\u4e14\u517c\u5bb9PIL\u7684\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Pillow\u8fdb\u884c\u56fe\u50cf\u521b\u5efa\u548c\u7ed8\u5236\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528Pillow\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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<ol start=\"2\">\n<li>\u521b\u5efa\u548c\u7ed8\u5236\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165Pillow\u5e93\u4e2d\u7684Image\u548cImageDraw\u6a21\u5757\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Image\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528ImageDraw\u6a21\u5757\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u767d\u8272\u56fe\u50cf<\/strong><\/h2>\n<p>width, height = 400, 400<\/p>\n<p>image = Image.new(&quot;RGB&quot;, (width, height), &quot;white&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u7ea2\u8272\u77e9\u5f62<\/strong><\/h2>\n<p>draw.rectangle([50, 50, 150, 150], fill=&quot;red&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u692d\u5706<\/strong><\/h2>\n<p>draw.ellipse([200, 50, 300, 150], fill=&quot;blue&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u6761\u7eff\u8272\u7ebf<\/strong><\/h2>\n<p>draw.line([50, 200, 350, 200], fill=&quot;green&quot;, width=3)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&quot;example.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u9ad8\u7ea7\u7ed8\u56fe\u529f\u80fd<\/li>\n<\/ol>\n<p><p>Pillow\u4e0d\u4ec5\u652f\u6301\u7b80\u5355\u7684\u5f62\u72b6\u7ed8\u5236\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6587\u672c\u7ed8\u5236\u3001\u56fe\u50cf\u5408\u6210\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u52a0\u8f7d\u5b57\u4f53\u5e76\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageFont<\/p>\n<h2><strong>\u52a0\u8f7d\u5b57\u4f53<\/strong><\/h2>\n<p>font = ImageFont.truetype(&quot;arial.ttf&quot;, 20)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>draw.text((50, 250), &quot;Hello, Pillow!&quot;, font=font, fill=&quot;black&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001Matplotlib\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u5b83\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u4f8b\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Matplotlib\u8fdb\u884c\u57fa\u672c\u7ed8\u56fe\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Matplotlib\u5e93<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Matplotlib\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7ed8\u5236\u57fa\u672c\u56fe\u5f62<\/li>\n<\/ol>\n<p><p>\u5bfc\u5165Matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\uff0c\u7136\u540e\u53ef\u4ee5\u5f00\u59cb\u7ed8\u5236\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u6761\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y, label=&quot;Prime numbers&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Line Chart Example&quot;)<\/p>\n<p>plt.xlabel(&quot;X axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u9ad8\u7ea7\u7ed8\u56fe\u529f\u80fd<\/li>\n<\/ol>\n<p><p>Matplotlib\u4e0d\u4ec5\u652f\u6301\u7b80\u5355\u7684\u4e8c\u7ef4\u56fe\u5f62\u7ed8\u5236\uff0c\u8fd8\u652f\u6301\u4e09\u7ef4\u7ed8\u56fe\u3001\u5b50\u56fe\u3001\u52a8\u753b\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u6563\u70b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6570\u636e<\/strong><\/h2>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<h2><strong>\u521b\u5efa\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y, c=&#39;r&#39;, marker=&#39;o&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Scatter Plot Example&quot;)<\/p>\n<p>plt.xlabel(&quot;X axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001Turtle\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Turtle\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u9002\u5408\u7528\u4e8e\u6559\u80b2\u76ee\u7684\uff0c\u5e2e\u52a9\u521d\u5b66\u8005\u7406\u89e3\u7f16\u7a0b\u6982\u5ff5\u548c\u7b80\u5355\u7ed8\u56fe\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Turtle\u8fdb\u884c\u7ed8\u56fe\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Turtle\u8fdb\u884c\u57fa\u672c\u7ed8\u56fe<\/li>\n<\/ol>\n<p><p>Turtle\u6a21\u5757\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed8\u5236\u6b63\u65b9\u5f62\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efa\u753b\u5e03<\/strong><\/h2>\n<p>wn = turtle.Screen()<\/p>\n<p>wn.bgcolor(&quot;lightgreen&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6d77\u9f9f\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.color(&quot;blue&quot;)<\/p>\n<p>t.pensize(3)<\/p>\n<h2><strong>\u7ed8\u5236\u6b63\u65b9\u5f62<\/strong><\/h2>\n<p>for _ in range(4):<\/p>\n<p>    t.forward(100)<\/p>\n<p>    t.left(90)<\/p>\n<h2><strong>\u5173\u95ed\u753b\u5e03<\/strong><\/h2>\n<p>wn.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u9ad8\u7ea7\u529f\u80fd<\/li>\n<\/ol>\n<p><p>Turtle\u8fd8\u652f\u6301\u7ed8\u5236\u590d\u6742\u7684\u5f62\u72b6\u3001\u8bbe\u7f6e\u52a8\u753b\u901f\u5ea6\u3001\u5b9a\u4e49\u989c\u8272\u7b49\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u4e94\u89d2\u661f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e94\u89d2\u661f\u7ed8\u5236\u51fd\u6570<\/p>\n<p>def draw_star(size):<\/p>\n<p>    for _ in range(5):<\/p>\n<p>        t.forward(size)<\/p>\n<p>        t.right(144)<\/p>\n<h2><strong>\u7ed8\u5236\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>draw_star(100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u65b9\u5f0f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cPillow\u9002\u5408\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0cMatplotlib\u9002\u5408\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800cTurtle\u5219\u9002\u5408\u7528\u4e8e\u6559\u80b2\u548c\u7b80\u5355\u7ed8\u56fe\u3002\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u548c\u9879\u76ee\u7279\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u5b9e\u73b0\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528draw\u6a21\u5757\u8fdb\u884c\u56fe\u5f62\u7ed8\u5236\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cdraw\u6a21\u5757\u901a\u5e38\u4e0e\u56fe\u5f62\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u6bd4\u5982Tkinter\u3001Pygame\u6216Matplotlib\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u7a97\u53e3\u3001\u7ed8\u5236\u7ebf\u6761\u3001\u5f62\u72b6\u548c\u6587\u672c\u3002\u4ee5Pygame\u4e3a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pygame.draw<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u63d0\u4f9b\u7b80\u5355\u7684\u63a5\u53e3\u6765\u7ed8\u5236\u77e9\u5f62\u3001\u5706\u5f62\u548c\u591a\u8fb9\u5f62\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528draw\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6027\u80fd\u4f18\u5316\uff1f<\/strong><br \/>\u7ed8\u56fe\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u7ed8\u5236\u590d\u6742\u56fe\u5f62\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cc\u7f13\u51b2\u6280\u672f\uff0c\u51cf\u5c11\u5c4f\u5e55\u95ea\u70c1\uff0c\u786e\u4fdd\u53ea\u5728\u9700\u8981\u65f6\u66f4\u65b0\u5c4f\u5e55\u3002\u6b64\u5916\uff0c\u5408\u7406\u4f7f\u7528\u56fe\u5c42\u548c\u7cbe\u7075\uff08sprites\uff09\u53ef\u4ee5\u63d0\u5347\u7ed8\u5236\u6548\u7387\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u4e0edraw\u529f\u80fd\u7ed3\u5408\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7ed8\u56fe\uff1f<\/strong><br \/>\u9664\u4e86Pygame\uff0cPython\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u5e93\u53ef\u4ee5\u4e0edraw\u529f\u80fd\u76f8\u7ed3\u5408\uff0c\u5982Matplotlib\u3001Turtle\u548cTkinter\u3002Matplotlib\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800cTurtle\u5219\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u5f62\u7ed8\u5236\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u8ba9\u4f60\u7684\u7ed8\u56fe\u9879\u76ee\u66f4\u52a0\u9ad8\u6548\u548c\u6613\u4e8e\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4f7f\u7528draw\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PIL\u5e93\u521b\u5efa\u548c\u7ed8\u5236\u56fe\u50cf\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3001\u4f7f\u7528T [&hellip;]","protected":false},"author":3,"featured_media":948614,"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\/948613"}],"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=948613"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948613\/revisions"}],"predecessor-version":[{"id":948616,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948613\/revisions\/948616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948614"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}