{"id":1049239,"date":"2024-12-31T13:57:51","date_gmt":"2024-12-31T05:57:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049239.html"},"modified":"2024-12-31T13:57:54","modified_gmt":"2024-12-31T05:57:54","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%99%e8%87%aa%e5%b7%b1%e7%9a%84%e7%88%b6%e4%ba%b2%e5%ba%86%e7%94%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1049239.html","title":{"rendered":"\u5982\u4f55\u7528python\u7ed9\u81ea\u5df1\u7684\u7236\u4eb2\u5e86\u751f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b0be82d9-3bf7-4b6d-9b76-fc7aacaff08b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u7ed9\u81ea\u5df1\u7684\u7236\u4eb2\u5e86\u751f\" \/><\/p>\n<p><p> <strong>\u4e3a\u7236\u4eb2\u5e86\u751f\uff0c\u53ef\u4ee5\u7528Python\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u751f\u6210\u4e2a\u6027\u5316\u7684\u751f\u65e5\u795d\u798f\u3001\u521b\u5efa\u5e7b\u706f\u7247\u56de\u5fc6\u3001\u53d1\u9001\u81ea\u52a8\u90ae\u4ef6\u6216\u77ed\u4fe1<\/strong>\u3002\u5176\u4e2d\uff0c\u901a\u8fc7Python\u751f\u6210\u4e2a\u6027\u5316\u7684\u751f\u65e5\u795d\u798f\u56fe\u7247\u662f\u4e00\u4e2a\u6709\u8da3\u4e14\u5b9e\u7528\u7684\u4f8b\u5b50\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u6765\u521b\u5efa\u5e76\u7f16\u8f91\u56fe\u50cf\uff0c\u6dfb\u52a0\u6587\u5b57\u548c\u88c5\u9970\uff0c\u4f7f\u5f97\u751f\u65e5\u795d\u798f\u66f4\u52a0\u7279\u522b\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u7528Python\u751f\u6210\u4e2a\u6027\u5316\u7684\u751f\u65e5\u795d\u798f\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u751f\u65e5\u795d\u798f\u56fe\u7247\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e9bPython\u5e93\uff0c\u5305\u62ecPillow\u548cmatplotlib\u3002Pillow\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u800cmatplotlib\u53ef\u4ee5\u7528\u6765\u751f\u6210\u4e00\u4e9b\u56fe\u5f62\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install pillow matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u57fa\u672c\u7684\u56fe\u50cf\u753b\u5e03<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u56fe\u50cf\u753b\u5e03\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u5176\u4e0a\u7ed8\u5236\u6587\u5b57\u548c\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf<\/strong><\/h2>\n<p>width, height = 800, 600<\/p>\n<p>image = Image.new(&#39;RGB&#39;, (width, height), &#39;white&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u751f\u65e5\u795d\u798f\u6587\u5b57<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u79cd\u5b57\u4f53\u5e76\u5728\u56fe\u50cf\u4e0a\u6dfb\u52a0\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u5b57\u4f53<\/p>\n<p>font_path = &quot;\/usr\/share\/fonts\/truetype\/dejavu\/DejaVuSans-Bold.ttf&quot;  # \u6839\u636e\u4f60\u7684\u7cfb\u7edf\u9009\u62e9\u5b57\u4f53\u8def\u5f84<\/p>\n<p>font = ImageFont.truetype(font_path, 40)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u5b57<\/strong><\/h2>\n<p>message = &quot;Happy Birthday, Dad!&quot;<\/p>\n<p>text_width, text_height = draw.textsize(message, font=font)<\/p>\n<p>text_x = (width - text_width) \/\/ 2<\/p>\n<p>text_y = height \/\/ 4<\/p>\n<p>draw.text((text_x, text_y), message, font=font, fill=&quot;black&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u88c5\u9970\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u751f\u6210\u4e00\u4e9b\u88c5\u9970\u5143\u7d20\uff0c\u6bd4\u5982\u6c14\u7403\u3001\u86cb\u7cd5\u7b49\u3002<\/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>\u751f\u6210\u6c14\u7403\u56fe\u50cf<\/strong><\/h2>\n<p>def create_balloon():<\/p>\n<p>    fig, ax = plt.subplots(figsize=(2, 4))<\/p>\n<p>    circle = plt.Circle((0.5, 0.75), 0.2, color=&#39;red&#39;, fill=True)<\/p>\n<p>    ax.add_artist(circle)<\/p>\n<p>    ax.plot([0.5, 0.5], [0.1, 0.75], color=&#39;black&#39;)<\/p>\n<p>    ax.axis(&#39;off&#39;)<\/p>\n<p>    plt.savefig(&#39;balloon.png&#39;, bbox_inches=&#39;tight&#39;, pad_inches=0)<\/p>\n<p>    plt.close(fig)<\/p>\n<p>create_balloon()<\/p>\n<p>balloon_image = Image.open(&#39;balloon.png&#39;)<\/p>\n<h2><strong>\u5c06\u6c14\u7403\u6dfb\u52a0\u5230\u795d\u798f\u56fe\u50cf\u4e0a<\/strong><\/h2>\n<p>image.paste(balloon_image, (width - 200, height - 400), balloon_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u5e76\u5c55\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u5c06\u751f\u6210\u7684\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u5e76\u5c55\u793a\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>image.save(&#39;happy_birthday_dad.png&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u53d1\u9001\u81ea\u52a8\u90ae\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u795d\u798f\u66f4\u52a0\u7279\u522b\uff0c\u53ef\u4ee5\u81ea\u52a8\u5c06\u751f\u6210\u7684\u56fe\u7247\u53d1\u9001\u5230\u7236\u4eb2\u7684\u90ae\u7bb1\u3002\u4f7f\u7528smtplib\u5e93\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>from em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l.mime.multipart import MIMEMultipart<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<p>from email.mime.image import MIMEImage<\/p>\n<p>def send_email(subject, body, to_email, image_path):<\/p>\n<p>    from_email = &quot;your_email@example.com&quot;<\/p>\n<p>    password = &quot;your_password&quot;<\/p>\n<p>    # \u521b\u5efa\u90ae\u4ef6<\/p>\n<p>    msg = MIMEMultipart()<\/p>\n<p>    msg[&#39;From&#39;] = from_email<\/p>\n<p>    msg[&#39;To&#39;] = to_email<\/p>\n<p>    msg[&#39;Subject&#39;] = subject<\/p>\n<p>    # \u6dfb\u52a0\u90ae\u4ef6\u5185\u5bb9<\/p>\n<p>    msg.attach(MIMEText(body, &#39;plain&#39;))<\/p>\n<p>    # \u6dfb\u52a0\u56fe\u7247<\/p>\n<p>    with open(image_path, &#39;rb&#39;) as file:<\/p>\n<p>        img = MIMEImage(file.read())<\/p>\n<p>        img.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;happy_birthday_dad.png&#39;)<\/p>\n<p>        msg.attach(img)<\/p>\n<p>    # \u53d1\u9001\u90ae\u4ef6<\/p>\n<p>    with smtplib.SMTP(&#39;smtp.example.com&#39;, 587) as server:<\/p>\n<p>        server.starttls()<\/p>\n<p>        server.login(from_email, password)<\/p>\n<p>        server.send_message(msg)<\/p>\n<p>send_email(&quot;Happy Birthday, Dad!&quot;, &quot;Wishing you a fantastic birthday!&quot;, &quot;dad_email@example.com&quot;, &quot;happy_birthday_dad.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u7528Python\u751f\u6210\u4e00\u5f20\u4e2a\u6027\u5316\u7684\u751f\u65e5\u795d\u798f\u56fe\u7247\uff0c\u5e76\u81ea\u52a8\u53d1\u9001\u5230\u7236\u4eb2\u7684\u90ae\u7bb1\u3002\u8fd9\u4e0d\u4ec5\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u7f16\u7a0b\u7ec3\u4e60\uff0c\u8fd8\u80fd\u4e3a\u7236\u4eb2\u7684\u751f\u65e5\u589e\u6dfb\u4e00\u4efd\u7279\u522b\u7684\u795d\u798f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4e0d\u4ec5\u5c55\u793a\u4e86\u4f60\u7684\u7f16\u7a0b\u6280\u80fd\uff0c\u8fd8\u5c55\u793a\u4e86\u4f60\u7684\u521b\u610f\u548c\u5fc3\u610f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u751f\u65e5\u795d\u798f\u7a0b\u5e8f\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\uff0c\u5411\u60a8\u7684\u7236\u4eb2\u53d1\u9001\u751f\u65e5\u795d\u798f\u3002\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u795d\u798f\u5185\u5bb9\uff0c\u7136\u540e\u901a\u8fc7<code>print()<\/code>\u8f93\u51fa\u3002\u8fd8\u53ef\u4ee5\u7ed3\u5408\u65e5\u671f\u548c\u65f6\u95f4\u5e93\u6765\u786e\u4fdd\u5728\u751f\u65e5\u5f53\u5929\u81ea\u52a8\u53d1\u9001\u795d\u798f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5236\u4f5c\u751f\u65e5\u8d3a\u5361\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u521b\u5efa\u751f\u65e5\u8d3a\u5361\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PIL<\/code>\uff08Python Imaging Library\uff09\u6765\u5904\u7406\u56fe\u50cf\uff0c\u6dfb\u52a0\u6587\u5b57\u3002\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u6765\u7ed8\u5236\u56fe\u5f62\u548c\u56fe\u8868\uff0c\u7ed3\u5408\u795d\u798f\u8bed\u5236\u4f5c\u51fa\u72ec\u7279\u7684\u751f\u65e5\u8d3a\u5361\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u53d1\u9001\u751f\u65e5\u795d\u798f\u90ae\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>smtplib<\/code>\u5e93\u6765\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u3002\u901a\u8fc7\u8bbe\u7f6eSMTP\u670d\u52a1\u5668\u548c\u7aef\u53e3\uff0c\u53ef\u4ee5\u5c06\u751f\u65e5\u795d\u798f\u4f5c\u4e3a\u90ae\u4ef6\u6b63\u6587\u53d1\u9001\u7ed9\u60a8\u7684\u7236\u4eb2\u3002\u786e\u4fdd\u60a8\u4e86\u89e3\u5982\u4f55\u5904\u7406\u90ae\u4ef6\u670d\u52a1\u5668\u7684\u8eab\u4efd\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u90ae\u4ef6\u53d1\u9001\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e3a\u7236\u4eb2\u5e86\u751f\uff0c\u53ef\u4ee5\u7528Python\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u751f\u6210\u4e2a\u6027\u5316\u7684\u751f\u65e5\u795d\u798f\u3001\u521b\u5efa\u5e7b\u706f\u7247\u56de\u5fc6\u3001\u53d1\u9001\u81ea\u52a8\u90ae\u4ef6\u6216\u77ed\u4fe1\u3002\u5176\u4e2d\uff0c\u901a [&hellip;]","protected":false},"author":3,"featured_media":1049250,"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\/1049239"}],"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=1049239"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049239\/revisions"}],"predecessor-version":[{"id":1049252,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049239\/revisions\/1049252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1049250"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1049239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1049239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1049239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}