{"id":936094,"date":"2024-12-26T19:10:15","date_gmt":"2024-12-26T11:10:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936094.html"},"modified":"2024-12-26T19:10:18","modified_gmt":"2024-12-26T11:10:18","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/936094.html","title":{"rendered":"python\u5982\u4f55\u753b\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072522\/d63930d0-824b-499d-971b-adc4a2cd0e65.webp\" alt=\"python\u5982\u4f55\u753b\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528Matplotlib\u3001Pillow\u3001OpenCV\u7b49\u5e93\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u56fe\u5f62\u7ed8\u5236\uff0cMatplotlib\u662f\u6700\u5e38\u7528\u7684\u9009\u62e9\uff0c\u800cPillow\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u56fe\u50cf\u7f16\u8f91\u548c\u5904\u7406\u3002OpenCV\u5219\u9002\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u56fe\u50cf\u5904\u7406\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u5e93\u5728Python\u4e2d\u8fdb\u884c\u56fe\u7247\u7ed8\u5236\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u7ed8\u5236\u56fe\u7247<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u7247\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Matplotlib<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\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\u7b80\u5355\u56fe\u5f62<\/li>\n<\/ol>\n<p><p>Matplotlib\u63d0\u4f9b\u4e86\u5f88\u591a\u7b80\u5355\u7684\u7ed8\u56fe\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u7ed8\u5236\u7b80\u5355\u7ebf\u6761\u548c\u5f62\u72b6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u753b\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf<\/strong><\/h2>\n<p>ax.plot([0, 1], [0, 1], label=&#39;Line&#39;)<\/p>\n<h2><strong>\u753b\u4e00\u4e2a\u77e9\u5f62<\/strong><\/h2>\n<p>rect = plt.Rectangle((0.2, 0.2), 0.2, 0.4, fill=False)<\/p>\n<p>ax.add_patch(rect)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>ax.set_title(&#39;Simple Plot&#39;)<\/p>\n<p>ax.set_xlabel(&#39;x-axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;y-axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\n<p>ax.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>\u7ed8\u5236\u56fe\u7247<\/li>\n<\/ol>\n<p><p>Matplotlib\u4e0d\u4ec5\u53ef\u4ee5\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\uff0c\u8fd8\u53ef\u4ee5\u663e\u793a\u548c\u4fee\u6539\u56fe\u7247\uff1a<\/p>\n<\/p>\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\u7247<\/strong><\/h2>\n<p>img = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>plt.imshow(img)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001PILLOW\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u5bf9\u56fe\u50cf\u8fdb\u884c\u521b\u5efa\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pillow\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>\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>Pillow\u53ef\u4ee5\u8f7b\u677e\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u7247<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u56fe\u50cf\u7ed8\u5236\u548c\u7f16\u8f91<\/li>\n<\/ol>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u56fe\u5f62\u548c\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img = Image.new(&#39;RGB&#39;, (200, 200), color = &#39;white&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(img)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>font = ImageFont.load_default()<\/p>\n<p>draw.text((10, 10), &quot;Hello, World!&quot;, fill=&quot;black&quot;, font=font)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>draw.line((10, 30, 190, 30), fill=&quot;black&quot;, width=2)<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62<\/strong><\/h2>\n<p>draw.rectangle((10, 50, 190, 100), outline=&quot;black&quot;, width=2)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u7247<\/strong><\/h2>\n<p>img.save(&#39;example_draw.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001OPENCV\u7528\u4e8e\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5OpenCV<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528OpenCV\u53ef\u4ee5\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u7247<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, img)<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u952e<\/strong><\/h2>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u56fe\u50cf\u7ed8\u5236\u548c\u7f16\u8f91<\/li>\n<\/ol>\n<p><p>OpenCV\u8fd8\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u7ed8\u5236\u548c\u7f16\u8f91\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf<\/strong><\/h2>\n<p>img = 255 * np.ones((200, 200, 3), dtype=np.uint8)<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>cv2.line(img, (10, 10), (190, 10), (0, 0, 0), 2)<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62<\/strong><\/h2>\n<p>cv2.rectangle(img, (10, 20), (190, 80), (0, 0, 0), 2)<\/p>\n<h2><strong>\u7ed8\u5236\u5706\u5f62<\/strong><\/h2>\n<p>cv2.circle(img, (100, 150), 30, (0, 0, 0), 2)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>cv2.putText(img, &#39;OpenCV&#39;, (10, 190), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Drawing&#39;, img)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u548c\u5904\u7406\u56fe\u50cf\u6709\u591a\u79cd\u5de5\u5177\u53ef\u4f9b\u9009\u62e9\u3002<strong>Matplotlib<\/strong>\u9002\u5408\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\u548c\u5c55\u793a\u56fe\u7247\uff1b<strong>Pillow<\/strong>\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u56fe\u50cf\u7f16\u8f91\u529f\u80fd\uff1b\u800c<strong>OpenCV<\/strong>\u5219\u662f\u8fdb\u884c\u590d\u6742\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u7684\u7406\u60f3\u9009\u62e9\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u56fe\u50cf\u5904\u7406\u7684\u6548\u7387\u548c\u6548\u679c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u8fdb\u884c\u56fe\u7247\u7ed8\u5236\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u56fe\u7247\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u56fe\u7247\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecMatplotlib\u3001Pillow\u548cOpenCV\u3002Matplotlib\u4e3b\u8981\u7528\u4e8e\u7ed8\u52362D\u56fe\u5f62\uff0c\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\uff1bPillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u548c\u56fe\u50cf\u64cd\u4f5c\uff1bOpenCV\u5219\u66f4\u9002\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u56fe\u50cf\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u4f7f\u7528Matplotlib\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\u7684\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u5b89\u88c5Matplotlib\u5e93\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09\uff0c\u7136\u540e\u5bfc\u5165\u5e93\u3002\u5728\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u7ed8\u5236\u7ebf\u56fe\uff0c\u6216\u8005\u4f7f\u7528<code>plt.imshow()<\/code>\u663e\u793a\u56fe\u50cf\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u6765\u5c55\u793a\u7ed8\u5236\u7684\u56fe\u5f62\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u548c\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u6765\u5904\u7406\u548c\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\u3002\u901a\u8fc7\u5bfc\u5165Pillow\u5e93\uff0c\u53ef\u4ee5\u6253\u5f00\u73b0\u6709\u56fe\u50cf\u3001\u8fdb\u884c\u7f16\u8f91\uff08\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u8c03\u6574\u5927\u5c0f\u7b49\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>save()<\/code>\u65b9\u6cd5\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\uff08\u5982JPEG\u3001PNG\u7b49\uff09\u3002\u8fd9\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5b58\u50a8\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed8\u5236\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528Matplotlib\u3001Pillow\u3001OpenCV\u7b49\u5e93\u3002\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":936098,"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\/936094"}],"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=936094"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936094\/revisions"}],"predecessor-version":[{"id":936099,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936094\/revisions\/936099"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/936098"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=936094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=936094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=936094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}