{"id":1096269,"date":"2025-01-08T14:59:34","date_gmt":"2025-01-08T06:59:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096269.html"},"modified":"2025-01-08T14:59:36","modified_gmt":"2025-01-08T06:59:36","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%9b%be%e5%83%8f%e4%bd%bf%e7%94%a8base64-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096269.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u56fe\u50cf\u4f7f\u7528base64"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211547\/29e25ad4-779a-4e77-9b3f-a8b9d869b2a7.webp\" alt=\"python\u5982\u4f55\u5bf9\u56fe\u50cf\u4f7f\u7528base64\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bf9\u56fe\u50cf\u4f7f\u7528base64\u7f16\u7801\u548c\u89e3\u7801\u7684\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528<code>base64<\/code>\u5e93\u5bf9\u56fe\u50cf\u8fdb\u884c\u7f16\u7801\u3001\u89e3\u7801\uff0c\u4f7f\u7528<code>PIL<\/code>\u5e93\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u8be6\u7ec6\u5730\u63cf\u8ff0\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5982\u4f55\u5b89\u88c5\u76f8\u5173\u5e93\u5f00\u59cb\uff0c\u63a5\u7740\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5bf9\u56fe\u50cf\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5c55\u793a\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86<code>PIL<\/code>\u5e93\uff08Python Imaging Library\uff0c\u901a\u5e38\u4f7f\u7528<code>Pillow<\/code>\uff09\u548c<code>base64<\/code>\u5e93\u3002<code>base64<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u65e0\u9700\u5b89\u88c5\u989d\u5916\u7684\u5305\u3002\u800c<code>Pillow<\/code>\u5e93\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u56fe\u50cf\u8bfb\u53d6\u4e0e\u5904\u7406<\/h2>\n<\/p>\n<p><p><strong>Pillow<\/strong>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5b83\u6765\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u56fe\u50cf\u7684base64\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3abase64\u683c\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u5c06\u56fe\u50cf\u8bfb\u53d6\u4e3a\u5b57\u8282\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u7f16\u7801\u3002\u4ee5\u4e0b\u662f\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3abase64\u7f16\u7801\u5b57\u7b26\u4e32\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import base64<\/p>\n<p>from io import BytesIO<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6d41<\/strong><\/h2>\n<p>buffered = BytesIO()<\/p>\n<p>image.save(buffered, format=&quot;JPEG&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>img_byte = buffered.getvalue()<\/p>\n<h2><strong>\u5c06\u5b57\u8282\u6570\u636e\u8fdb\u884cbase64\u7f16\u7801<\/strong><\/h2>\n<p>img_base64 = base64.b64encode(img_byte)<\/p>\n<p>img_base64_str = img_base64.decode(&#39;utf-8&#39;)<\/p>\n<p>print(img_base64_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>BytesIO<\/code>\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u5b57\u8282\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>base64.b64encode<\/code>\u65b9\u6cd5\u8fdb\u884c\u7f16\u7801\uff0c\u6700\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u56fe\u50cf\u7684base64\u89e3\u7801<\/h2>\n<\/p>\n<p><p>\u5c06base64\u7f16\u7801\u7684\u56fe\u50cf\u5b57\u7b26\u4e32\u89e3\u7801\u5e76\u8f6c\u6362\u56de\u56fe\u50cf\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import base64<\/p>\n<p>from io import BytesIO<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>base64\u7f16\u7801\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>img_base64_str = &#39;...&#39;  # \u8fd9\u91cc\u5e94\u8be5\u662f\u4f60\u7684base64\u5b57\u7b26\u4e32<\/p>\n<h2><strong>\u5c06base64\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>img_data = base64.b64decode(img_base64_str)<\/p>\n<h2><strong>\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(BytesIO(img_data))<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>base64.b64decode<\/code>\u5c06base64\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u5b57\u8282\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>BytesIO<\/code>\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u56fe\u50cf\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u9645\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cbase64\u7f16\u7801\u548c\u89e3\u7801\u56fe\u50cf\u5728\u7f51\u7edc\u4f20\u8f93\u548c\u6570\u636e\u5b58\u50a8\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u56fe\u50cf\u8bfb\u53d6\u3001\u7f16\u7801\u3001\u89e3\u7801\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import base64<\/p>\n<p>from io import BytesIO<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3abase64\u7f16\u7801\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>buffered = BytesIO()<\/p>\n<p>image.save(buffered, format=&quot;JPEG&quot;)<\/p>\n<p>img_byte = buffered.getvalue()<\/p>\n<p>img_base64 = base64.b64encode(img_byte)<\/p>\n<p>img_base64_str = img_base64.decode(&#39;utf-8&#39;)<\/p>\n<p>print(&quot;Encoded base64 string:&quot;)<\/p>\n<p>print(img_base64_str)<\/p>\n<h2><strong>\u5c06base64\u5b57\u7b26\u4e32\u89e3\u7801\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>img_data = base64.b64decode(img_base64_str)<\/p>\n<p>decoded_image = Image.open(BytesIO(img_data))<\/p>\n<p>decoded_image.save(&#39;decoded_example.jpg&#39;)<\/p>\n<p>print(&quot;Decoded image saved as &#39;decoded_example.jpg&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3abase64\u7f16\u7801\u5b57\u7b26\u4e32\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5c06\u5176\u89e3\u7801\u56de\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p><strong>Python<\/strong>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\uff0c\u4f7f\u5f97\u5904\u7406\u56fe\u50cf\u548c\u8fdb\u884cbase64\u7f16\u7801\u89e3\u7801\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7<strong>Pillow<\/strong>\u5e93\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u50cf\uff0c\u7ed3\u5408<strong>base64<\/strong>\u5e93\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u5b9e\u9645\u5e94\u7528\uff0c\u5982\u7f51\u7edc\u4f20\u8f93\u3001\u6570\u636e\u5b58\u50a8\u548c\u56fe\u50cf\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u5728\u66f4\u591a\u590d\u6742\u7684\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4e3a\u60a8\u7684\u5e94\u7528\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aBase64\u683c\u5f0f\uff1f<\/strong><br \/>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aBase64\u683c\u5f0f\u7684\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5185\u7f6e\u5e93\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>base64<\/code>\u6a21\u5757\u3002\u63a5\u7740\uff0c\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>base64.b64encode()<\/code>\u51fd\u6570\u5c06\u5b57\u8282\u6d41\u7f16\u7801\u4e3aBase64\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import base64\n\nwith open(&quot;image.jpg&quot;, &quot;rb&quot;) as image_file:\n    encoded_string = base64.b64encode(image_file.read()).decode(&#39;utf-8&#39;)\nprint(encoded_string)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4eceBase64\u683c\u5f0f\u6062\u590d\u56fe\u50cf\uff1f<\/strong><br \/>\u8981\u5c06Base64\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u56fe\u50cf\uff0c\u60a8\u9700\u8981\u4f7f\u7528<code>base64.b64decode()<\/code>\u51fd\u6570\u3002\u8bfb\u53d6Base64\u5b57\u7b26\u4e32\u540e\uff0c\u5c06\u5176\u89e3\u7801\u4e3a\u5b57\u8282\u6d41\uff0c\u5e76\u4f7f\u7528Python\u7684\u6587\u4ef6\u5199\u5165\u529f\u80fd\u5c06\u5b57\u8282\u6d41\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import base64\n\n# \u5047\u8bbeencoded_string\u662f\u60a8\u4e4b\u524d\u83b7\u5f97\u7684Base64\u5b57\u7b26\u4e32\nwith open(&quot;output_image.jpg&quot;, &quot;wb&quot;) as image_file:\n    image_file.write(base64.b64decode(encoded_string))\n<\/code><\/pre>\n<p><strong>Base64\u7f16\u7801\u7684\u56fe\u50cf\u6709\u4ec0\u4e48\u4f18\u52bf\u548c\u52a3\u52bf\uff1f<\/strong><br \/>Base64\u7f16\u7801\u7684\u56fe\u50cf\u5728\u4f20\u8f93\u548c\u5b58\u50a8\u4e2d\u5177\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\uff0c\u6bd4\u5982\u53ef\u4ee5\u5d4c\u5165HTML\u6216JSON\u4e2d\uff0c\u4fbf\u4e8e\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u3002\u7136\u800c\uff0c\u4f7f\u7528Base64\u7f16\u7801\u4f1a\u5bfc\u81f4\u6587\u4ef6\u4f53\u79ef\u81a8\u80c0\uff0c\u901a\u5e38\u589e\u52a0\u7ea633%\u7684\u6570\u636e\u91cf\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u548c\u5b58\u50a8\u6548\u7387\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u4f7f\u7528Base64\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6743\u8861\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bf9\u56fe\u50cf\u4f7f\u7528base64\u7f16\u7801\u548c\u89e3\u7801\u7684\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528base64\u5e93\u5bf9\u56fe\u50cf\u8fdb\u884c\u7f16\u7801\u3001\u89e3\u7801\uff0c\u4f7f\u7528PIL [&hellip;]","protected":false},"author":3,"featured_media":1096274,"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\/1096269"}],"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=1096269"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096269\/revisions"}],"predecessor-version":[{"id":1096278,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096269\/revisions\/1096278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096274"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}