{"id":1074924,"date":"2025-01-08T11:39:08","date_gmt":"2025-01-08T03:39:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074924.html"},"modified":"2025-01-08T11:39:10","modified_gmt":"2025-01-08T03:39:10","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e4%b8%80%e5%b9%85%e5%9b%be%e7%bc%96%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074924.html","title":{"rendered":"python\u5982\u4f55\u628a\u4e00\u5e45\u56fe\u7f16\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104443\/c9bb9b20-044c-4b22-b977-e8df4a73f414.webp\" alt=\"python\u5982\u4f55\u628a\u4e00\u5e45\u56fe\u7f16\u7801\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u4e00\u5e45\u56fe\u50cf\u7f16\u7801\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u5b57\u8282\u6570\u636e\uff0c\u6bd4\u5982PNG\u3001JPEG\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pillow\u5e93\u3001OpenCV\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cPillow\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u8bfb\u53d6\u548c\u4fdd\u5b58\u3002\u901a\u8fc7\u8be5\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u56fe\u50cf\u7f16\u7801\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u5bf9\u56fe\u50cf\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u56fe\u50cf\u7f16\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Pillow\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\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 pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u548c\u7f16\u7801\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u7684<code>Image<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u7f16\u7801\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u7f16\u7801\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import io<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;example.png&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u8282\u6d41\u5bf9\u8c61<\/strong><\/h2>\n<p>byte_arr = io.BytesIO()<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u7f16\u7801\u4e3aJPEG\u683c\u5f0f<\/strong><\/h2>\n<p>image.save(byte_arr, format=&#39;JPEG&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>encoded_image = byte_arr.getvalue()<\/p>\n<p>print(encoded_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u8bfb\u53d6\u4e86\u4e00\u5e45\u56fe\u50cf\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u8282\u6d41\u5bf9\u8c61\uff0c\u5e76\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aJPEG\u683c\u5f0f\uff0c\u6700\u540e\u83b7\u53d6\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV\u5e93\u8fdb\u884c\u56fe\u50cf\u7f16\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5OpenCV\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u548c\u7f16\u7801\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>OpenCV\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u7684\u8bfb\u53d6\u548c\u7f16\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u5c06\u56fe\u50cf\u7f16\u7801\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u7f16\u7801\u4e3aJPEG\u683c\u5f0f<\/strong><\/h2>\n<p>retval, buffer = cv2.imencode(&#39;.jpg&#39;, image)<\/p>\n<h2><strong>\u83b7\u53d6\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>encoded_image = buffer.tobytes()<\/p>\n<p>print(encoded_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u8bfb\u53d6\u4e86\u4e00\u5e45\u56fe\u50cf\uff0c\u7136\u540e\u4f7f\u7528<code>imencode<\/code>\u51fd\u6570\u5c06\u56fe\u50cf\u7f16\u7801\u4e3aJPEG\u683c\u5f0f\uff0c\u5e76\u83b7\u53d6\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528base64\u7f16\u7801\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5c06\u56fe\u50cf\u7f16\u7801\u4e3abase64\u683c\u5f0f\uff0c\u4ee5\u4fbf\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u3002\u53ef\u4ee5\u7ed3\u5408Pillow\u5e93\u6216OpenCV\u5e93\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Pillow\u5e93\u8fdb\u884cbase64\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pillow\u5e93\u5c06\u56fe\u50cf\u7f16\u7801\u4e3abase64\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import io<\/p>\n<p>import base64<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;example.png&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u8282\u6d41\u5bf9\u8c61<\/strong><\/h2>\n<p>byte_arr = io.BytesIO()<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u7f16\u7801\u4e3aJPEG\u683c\u5f0f<\/strong><\/h2>\n<p>image.save(byte_arr, format=&#39;JPEG&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>encoded_image = byte_arr.getvalue()<\/p>\n<h2><strong>\u5c06\u5b57\u8282\u6570\u636e\u7f16\u7801\u4e3abase64\u683c\u5f0f<\/strong><\/h2>\n<p>base64_encoded_image = base64.b64encode(encoded_image)<\/p>\n<p>print(base64_encoded_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528OpenCV\u5e93\u8fdb\u884cbase64\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528OpenCV\u5e93\u5c06\u56fe\u50cf\u7f16\u7801\u4e3abase64\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import base64<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u7f16\u7801\u4e3aJPEG\u683c\u5f0f<\/strong><\/h2>\n<p>retval, buffer = cv2.imencode(&#39;.jpg&#39;, image)<\/p>\n<h2><strong>\u83b7\u53d6\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>encoded_image = buffer.tobytes()<\/p>\n<h2><strong>\u5c06\u5b57\u8282\u6570\u636e\u7f16\u7801\u4e3abase64\u683c\u5f0f<\/strong><\/h2>\n<p>base64_encoded_image = base64.b64encode(encoded_image)<\/p>\n<p>print(base64_encoded_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u56fe\u50cf\u7f16\u7801\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u7f16\u7801\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7f51\u7edc\u4f20\u8f93<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u56fe\u50cf\u6570\u636e\u7f16\u7801\u4e3a\u7279\u5b9a\u683c\u5f0f\uff0c\u4ee5\u4fbf\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u3002\u4f8b\u5982\uff0c\u4f7f\u7528base64\u7f16\u7801\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u56fe\u50cf\u6570\u636e\u5d4c\u5165\u5230JSON\u5bf9\u8c61\u4e2d\uff0c\u4ece\u800c\u7b80\u5316\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u5e94\u7528\u4e2d\uff0c\u7f16\u7801\u540e\u7684\u56fe\u50cf\u6570\u636e\u53ef\u4ee5\u7528\u4e8e\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u7ecf\u8fc7\u7f16\u7801\u540e\u7684\u56fe\u50cf\u53ef\u4ee5\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u67e5\u8be2\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u56fe\u50cf\u5c55\u793a<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u9875\u5f00\u53d1\u4e2d\uff0c\u7f16\u7801\u540e\u7684\u56fe\u50cf\u6570\u636e\u53ef\u4ee5\u5d4c\u5165\u5230HTML\u6587\u6863\u4e2d\uff0c\u4ee5\u4fbf\u5728\u7f51\u9875\u4e0a\u5c55\u793a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06base64\u7f16\u7801\u7684\u56fe\u50cf\u6570\u636e\u5d4c\u5165\u5230<code>&lt;img&gt;<\/code>\u6807\u7b7e\u4e2d\uff0c\u4ece\u800c\u5728\u7f51\u9875\u4e0a\u76f4\u63a5\u5c55\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u5c06\u4e00\u5e45\u56fe\u50cf\u7f16\u7801\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u5b57\u8282\u6570\u636e\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pillow\u5e93\u548cOpenCV\u5e93\u8fdb\u884c\u56fe\u50cf\u7f16\u7801\uff0c\u5e76\u4ecb\u7ecd\u4e86\u56fe\u50cf\u7f16\u7801\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u56fe\u50cf\u7f16\u7801\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\u56fe\u50cf\u5904\u7406\u548c\u7f16\u7801\u65b9\u9762\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684PIL\uff08Pillow\uff09\u5e93\u6765\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528base64\u5e93\u5c06\u5176\u7f16\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u9996\u5148\uff0c\u5b89\u88c5Pillow\u5e93\u5e76\u5bfc\u5165\u76f8\u5173\u6a21\u5757\uff0c\u7136\u540e\u6253\u5f00\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff0c\u6700\u540e\u4f7f\u7528base64\u5bf9\u5b57\u8282\u6d41\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7f16\u7801\u56fe\u50cf\u7684\u5e38\u89c1\u683c\u5f0f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u56fe\u50cf\u7f16\u7801\u683c\u5f0f\u5305\u62ecJPEG\u3001PNG\u3001BMP\u3001GIF\u7b49\u3002\u6bcf\u79cd\u683c\u5f0f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u7279\u6027\uff0c\u4f8b\u5982JPEG\u9002\u5408\u7167\u7247\uff0c\u800cPNG\u5219\u9002\u5408\u9700\u8981\u900f\u660e\u80cc\u666f\u7684\u56fe\u50cf\u3002\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u56fe\u50cf\u8d28\u91cf\u548c\u538b\u7f29\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u7801\u7f16\u7801\u540e\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u89e3\u7801\u7f16\u7801\u540e\u7684\u56fe\u50cf\u4e5f\u53ef\u4ee5\u4f7f\u7528base64\u5e93\u3002\u5c06base64\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u5b57\u8282\u6d41\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u5c06\u5176\u8f6c\u6362\u56de\u56fe\u50cf\u5bf9\u8c61\u3002\u8fd9\u4f7f\u5f97\u56fe\u50cf\u53ef\u4ee5\u5728\u5404\u79cd\u5e94\u7528\u4e2d\u663e\u793a\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u4e00\u5e45\u56fe\u50cf\u7f16\u7801\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u5b57\u8282\u6570\u636e\uff0c\u6bd4\u5982PNG\u3001JPEG\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pi [&hellip;]","protected":false},"author":3,"featured_media":1074933,"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\/1074924"}],"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=1074924"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074924\/revisions"}],"predecessor-version":[{"id":1074936,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074924\/revisions\/1074936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074933"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}