{"id":1058465,"date":"2024-12-31T15:16:41","date_gmt":"2024-12-31T07:16:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058465.html"},"modified":"2024-12-31T15:16:42","modified_gmt":"2024-12-31T07:16:42","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e7%89%87%e4%bf%9d%e5%ad%98%e5%9c%a8html","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058465.html","title":{"rendered":"python\u5982\u4f55\u5c06\u56fe\u7247\u4fdd\u5b58\u5728html"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b667e352-c792-4709-9376-7b73c0abc414.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u56fe\u7247\u4fdd\u5b58\u5728html\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f\u3001\u5c06\u56fe\u7247\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u4e0a\u5e76\u901a\u8fc7URL\u5f15\u7528\u3001\u5c06\u56fe\u7247\u5d4c\u5165HTML\u6807\u7b7e\u4e2d<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f\u5e76\u5d4c\u5165HTML\u6807\u7b7e\u4e2d\uff0c\u6765\u5c06\u56fe\u7247\u4fdd\u5b58\u5728HTML\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u4e0d\u9700\u8981\u4f9d\u8d56\u5916\u90e8\u8d44\u6e90\uff0c\u6240\u6709\u6570\u636e\u90fd\u5305\u542b\u5728\u4e00\u4e2aHTML\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f\u5e76\u5d4c\u5165HTML\u6807\u7b7e\u4e2d\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684Python\u5e93<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u6216\u5176\u5206\u652fPillow\u6765\u5904\u7406\u56fe\u50cf\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u6253\u5f00\u56fe\u7247\u5e76\u5c06\u5176\u7f16\u7801\u4e3aBase64\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import base64<\/p>\n<p>from io import BytesIO<\/p>\n<p>def encode_image_to_base64(image_path):<\/p>\n<p>    # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>    with Image.open(image_path) as img:<\/p>\n<p>        # \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6d41<\/p>\n<p>        buffered = BytesIO()<\/p>\n<p>        # \u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u5b57\u8282\u6d41\u4e2d\uff0c\u683c\u5f0f\u4e3aPNG<\/p>\n<p>        img.save(buffered, format=&quot;PNG&quot;)<\/p>\n<p>        # \u83b7\u53d6\u5b57\u8282\u6d41\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u7f16\u7801\u4e3aBase64<\/p>\n<p>        img_str = base64.b64encode(buffered.getvalue()).decode(&quot;utf-8&quot;)<\/p>\n<p>    return img_str<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u751f\u6210\u5305\u542bBase64\u7f16\u7801\u56fe\u50cf\u7684HTML\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5c06Base64\u7f16\u7801\u7684\u56fe\u50cf\u5d4c\u5165\u5230HTML\u6807\u7b7e\u4e2d\uff0c\u5e76\u751f\u6210\u4e00\u4e2aHTML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_html_with_image(base64_image_str, output_html_path):<\/p>\n<p>    # \u5b9a\u4e49HTML\u5185\u5bb9<\/p>\n<p>    html_content = f&quot;&quot;&quot;<\/p>\n<p>    &lt;!DOCTYPE html&gt;<\/p>\n<p>    &lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>    &lt;head&gt;<\/p>\n<p>        &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<\/p>\n<p>        &lt;title&gt;Image in HTML&lt;\/title&gt;<\/p>\n<p>    &lt;\/head&gt;<\/p>\n<p>    &lt;body&gt;<\/p>\n<p>        &lt;h1&gt;Embedded Image&lt;\/h1&gt;<\/p>\n<p>        &lt;img src=&quot;data:image\/png;base64,{base64_image_str}&quot; alt=&quot;Embedded Image&quot;&gt;<\/p>\n<p>    &lt;\/body&gt;<\/p>\n<p>    &lt;\/html&gt;<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    # \u5c06HTML\u5185\u5bb9\u5199\u5165\u6587\u4ef6<\/p>\n<p>    with open(output_html_path, &quot;w&quot;) as html_file:<\/p>\n<p>        html_file.write(html_content)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    # \u56fe\u7247\u8def\u5f84<\/p>\n<p>    image_path = &quot;path_to_your_image.png&quot;<\/p>\n<p>    # \u8f93\u51faHTML\u6587\u4ef6\u8def\u5f84<\/p>\n<p>    output_html_path = &quot;output.html&quot;<\/p>\n<p>    # \u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f<\/p>\n<p>    base64_image_str = encode_image_to_base64(image_path)<\/p>\n<p>    # \u751f\u6210\u5305\u542bBase64\u7f16\u7801\u56fe\u50cf\u7684HTML\u6587\u4ef6<\/p>\n<p>    generate_html_with_image(base64_image_str, output_html_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u89e3\u91caPython\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u8fd9\u6bb5Python\u4ee3\u7801\u9996\u5148\u5c06\u56fe\u50cf\u6587\u4ef6\u7f16\u7801\u4e3aBase64\u683c\u5f0f\uff0c\u7136\u540e\u751f\u6210\u4e00\u4e2a\u5305\u542b\u8be5\u56fe\u50cf\u7684HTML\u6587\u4ef6\u3002\u5728HTML\u6587\u4ef6\u4e2d\uff0c\u56fe\u50cf\u901a\u8fc7<code>&lt;img&gt;<\/code>\u6807\u7b7e\u5d4c\u5165\uff0c<code>src<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a<code>data:image\/png;base64,{base64_image_str}<\/code>\uff0c\u8fd9\u8868\u793a\u56fe\u50cf\u6570\u636e\u662fBase64\u7f16\u7801\u7684PNG\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u56fe\u7247\u4fdd\u5b58\u5728HTML\u4e2d\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c06\u56fe\u7247\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u4e0a\u5e76\u901a\u8fc7URL\u5f15\u7528<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5c06\u56fe\u7247\u4e0a\u4f20\u5230\u670d\u52a1\u5668\uff0c\u7136\u540e\u5728HTML\u6587\u4ef6\u4e2d\u4f7f\u7528<code>&lt;img&gt;<\/code>\u6807\u7b7e\u5f15\u7528\u56fe\u7247\u7684URL\u5730\u5740\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u56fe\u7247\u8f83\u5927\u65f6\uff0c\u53ef\u4ee5\u51cf\u5c11HTML\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528CSS\u80cc\u666f\u56fe\u7247<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u901a\u8fc7CSS\u5c06\u56fe\u7247\u8bbe\u7f6e\u4e3a\u80cc\u666f\u56fe\u7247\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-html\">&lt;style&gt;<\/p>\n<p>    .background-image {<\/p>\n<p>        background-image: url(&#39;path_to_your_image.png&#39;);<\/p>\n<p>        width: 100px;<\/p>\n<p>        height: 100px;<\/p>\n<p>    }<\/p>\n<p>&lt;\/style&gt;<\/p>\n<p>&lt;div class=&quot;background-image&quot;&gt;&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u56fe\u7247\u4f5c\u4e3a\u80cc\u666f\u56fe\u50cf\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528JavaScript\u52a8\u6001\u52a0\u8f7d\u56fe\u7247<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u901a\u8fc7JavaScript\u5728\u9875\u9762\u52a0\u8f7d\u540e\u52a8\u6001\u52a0\u8f7d\u56fe\u7247\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-html\">&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Load Image with JavaScript&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Image loaded by JavaScript&lt;\/h1&gt;<\/p>\n<p>    &lt;div id=&quot;image-container&quot;&gt;&lt;\/div&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        const img = document.createElement(&#39;img&#39;);<\/p>\n<p>        img.src = &#39;path_to_your_image.png&#39;;<\/p>\n<p>        document.getElementById(&#39;image-container&#39;).appendChild(img);<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u6216\u66f4\u6539\u56fe\u7247\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u56fe\u7247\u4fdd\u5b58\u5728HTML\u6587\u4ef6\u4e2d\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u901a\u8fc7\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f\u5e76\u5d4c\u5165HTML\u6807\u7b7e\u4e2d\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u4e0d\u4f9d\u8d56\u5916\u90e8\u8d44\u6e90\uff0c\u6240\u6709\u6570\u636e\u90fd\u5305\u542b\u5728\u4e00\u4e2aHTML\u6587\u4ef6\u4e2d\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u5176\u4ed6\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u901a\u8fc7URL\u5f15\u7528\u56fe\u7247\u3001\u4f7f\u7528CSS\u80cc\u666f\u56fe\u7247\u4ee5\u53ca\u4f7f\u7528JavaScript\u52a8\u6001\u52a0\u8f7d\u56fe\u7247\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06\u56fe\u7247\u5d4c\u5165\u5230HTML\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728HTML\u4e2d\u5d4c\u5165Python\u751f\u6210\u7684\u56fe\u7247\uff1f<\/strong><br \/>\u5728HTML\u4e2d\u5d4c\u5165Python\u751f\u6210\u7684\u56fe\u7247\u901a\u5e38\u6709\u51e0\u79cd\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff08\u5982PIL\u6216OpenCV\uff09\u751f\u6210\u56fe\u7247\uff0c\u7136\u540e\u5c06\u5176\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff0c\u63a5\u7740\u5728HTML\u4e2d\u4f7f\u7528<code>&lt;img&gt;<\/code>\u6807\u7b7e\u5f15\u7528\u8be5\u6587\u4ef6\u7684\u8def\u5f84\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5c06\u56fe\u7247\u8f6c\u6362\u4e3aBase64\u7f16\u7801\uff0c\u4ee5\u76f4\u63a5\u5728HTML\u4e2d\u5d4c\u5165\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u751f\u6210\u56fe\u7247\u540e\uff0c\u5982\u4f55\u786e\u4fdd\u5728HTML\u4e2d\u6b63\u786e\u663e\u793a\uff1f<\/strong><br \/>\u786e\u4fdd\u5728HTML\u4e2d\u6b63\u786e\u663e\u793aPython\u751f\u6210\u7684\u56fe\u7247\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u56fe\u7247\u7684\u8def\u5f84\u6b63\u786e\u65e0\u8bef\uff0cHTML\u6587\u4ef6\u548c\u56fe\u7247\u6587\u4ef6\u5e94\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u6216\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002\u5176\u6b21\uff0c\u68c0\u67e5\u56fe\u7247\u683c\u5f0f\u662f\u5426\u88ab\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u5e38\u89c1\u7684\u683c\u5f0f\u6709JPEG\u3001PNG\u548cGIF\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\u68c0\u67e5\u662f\u5426\u5b58\u5728\u52a0\u8f7d\u9519\u8bef\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python Flask\u6846\u67b6\u76f4\u63a5\u5728\u7f51\u9875\u4e2d\u5c55\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u662f\u7684\uff0cFlask\u6846\u67b6\u63d0\u4f9b\u4e86\u4fbf\u5229\u7684\u65b9\u5f0f\u6765\u5c55\u793a\u56fe\u7247\u3002\u4f60\u53ef\u4ee5\u5c06\u56fe\u7247\u4fdd\u5b58\u5728Flask\u7684\u9759\u6001\u6587\u4ef6\u5939\u4e2d\uff0c\u5e76\u901a\u8fc7\u8def\u7531\u5c06\u5176\u5448\u73b0\u3002\u4f7f\u7528<code>url_for<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5411\u9759\u6001\u6587\u4ef6\u7684URL\uff0c\u4ece\u800c\u5728HTML\u4e2d\u4f7f\u7528<code>&lt;img&gt;<\/code>\u6807\u7b7e\u5f15\u7528\u8be5URL\uff0c\u786e\u4fdd\u56fe\u7247\u80fd\u591f\u88ab\u6b63\u786e\u52a0\u8f7d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBase64\u683c\u5f0f\u3001\u5c06\u56fe\u7247\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u4e0a\u5e76\u901a\u8fc7URL\u5f15\u7528\u3001\u5c06\u56fe\u7247\u5d4c\u5165HTM [&hellip;]","protected":false},"author":3,"featured_media":1058469,"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\/1058465"}],"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=1058465"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058465\/revisions"}],"predecessor-version":[{"id":1058471,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058465\/revisions\/1058471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058469"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}