{"id":1077237,"date":"2025-01-08T12:00:25","date_gmt":"2025-01-08T04:00:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077237.html"},"modified":"2025-01-08T12:00:27","modified_gmt":"2025-01-08T04:00:27","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86bgr%e8%bd%ac%e4%b8%bargb-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077237.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06bgr\u8f6c\u4e3argb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181411\/ee5e61a0-16f5-4db6-bea8-e0af8075c447.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06bgr\u8f6c\u4e3argb\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u56fe\u50cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528OpenCV\u5e93\u3001Pillow\u5e93\u7b49\u5de5\u5177\u3002<strong>OpenCV\u5e93\u3001Pillow\u5e93\u3001NumPy\u5e93<\/strong>\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002OpenCV\u7684cv2\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570<code>cv2.cvtColor<\/code>\uff0c\u53ef\u4ee5\u7528\u6765\u8f6c\u6362\u56fe\u50cf\u7684\u989c\u8272\u7a7a\u95f4\u3002<\/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;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u56fe\u50cf<\/strong><\/h2>\n<p>rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)<\/p>\n<h2><strong>\u663e\u793a\u539f\u56fe\u548c\u8f6c\u6362\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;BGR Image&#39;, image)<\/p>\n<p>cv2.imshow(&#39;RGB Image&#39;, rgb_image)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728OpenCV\u4e2d\uff0c\u56fe\u50cf\u9ed8\u8ba4\u662f\u4ee5BGR\u683c\u5f0f\u8bfb\u53d6\u7684\u3002\u4f7f\u7528<code>cv2.cvtColor<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u56fe\u50cf\u4eceBGR\u8f6c\u6362\u4e3aRGB\u3002<code>cv2.COLOR_BGR2RGB<\/code>\u662f\u6307\u5b9a\u7684\u989c\u8272\u8f6c\u6362\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pillow\u5e93<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u53cb\u597d\u5206\u652f\uff0c\u662f\u4e00\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\u3002Pillow\u4e5f\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u56fe\u50cf\u989c\u8272\u8f6c\u6362\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u56fe\u50cf<\/strong><\/h2>\n<p>r, g, b = image.split()<\/p>\n<p>rgb_image = Image.merge(&quot;RGB&quot;, (b, g, r))<\/p>\n<h2><strong>\u663e\u793a\u539f\u56fe\u548c\u8f6c\u6362\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p>rgb_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728Pillow\u4e2d\uff0c\u56fe\u50cf\u662f\u4ee5RGB\u683c\u5f0f\u8bfb\u53d6\u7684\u3002\u5982\u679c\u4f60\u4ece\u5176\u4ed6\u5e93\u8bfb\u53d6\u4e86BGR\u683c\u5f0f\u7684\u56fe\u50cf\u5e76\u4e14\u60f3\u8f6c\u6362\u4e3aRGB\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u7684<code>split<\/code>\u548c<code>merge<\/code>\u65b9\u6cd5\u5206\u522b\u63d0\u53d6\u548c\u91cd\u65b0\u7ec4\u5408\u56fe\u50cf\u7684\u5404\u4e2a\u989c\u8272\u901a\u9053\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528NumPy\u76f4\u63a5\u64cd\u4f5c\u56fe\u50cf\u6570\u636e\u6765\u8fdb\u884c\u989c\u8272\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;)<\/p>\n<h2><strong>\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u56fe\u50cf<\/strong><\/h2>\n<p>rgb_image = image[:, :, ::-1]<\/p>\n<h2><strong>\u663e\u793a\u539f\u56fe\u548c\u8f6c\u6362\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;BGR Image&#39;, image)<\/p>\n<p>cv2.imshow(&#39;RGB Image&#39;, rgb_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728NumPy\u4e2d\uff0c\u56fe\u50cf\u6570\u636e\u901a\u5e38\u662f\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u7b2c\u4e09\u4e2a\u7ef4\u5ea6\u8868\u793a\u989c\u8272\u901a\u9053\u3002\u901a\u8fc7\u4f7f\u7528\u6570\u7ec4\u5207\u7247\u64cd\u4f5c<code>image[:, :, ::-1]<\/code>\uff0c\u53ef\u4ee5\u5c06BGR\u901a\u9053\u53cd\u8f6c\u4e3aRGB\u901a\u9053\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u56fe\u50cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u6b63\u5728\u4f7f\u7528\u7684\u5e93\u548c\u5177\u4f53\u9700\u6c42\u3002<strong>OpenCV\u5e93\u3001Pillow\u5e93\u3001NumPy\u5e93<\/strong>\u5404\u6709\u4f18\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>OpenCV\u5e93\uff1a<\/strong> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<code>cv2.cvtColor<\/code> \u662f\u8f6c\u6362\u989c\u8272\u7a7a\u95f4\u7684\u76f4\u63a5\u65b9\u6cd5\u3002<\/li>\n<li><strong>Pillow\u5e93\uff1a<\/strong> \u9002\u5408\u5904\u7406\u56fe\u50cf\u7684\u52a0\u8f7d\u3001\u4fdd\u5b58\u548c\u57fa\u672c\u5904\u7406\u529f\u80fd\u3002<code>split<\/code> \u548c <code>merge<\/code> \u65b9\u6cd5\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u989c\u8272\u901a\u9053\u64cd\u4f5c\u3002<\/li>\n<li><strong>NumPy\u5e93\uff1a<\/strong> \u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u80fd\u529b\uff0c\u9002\u5408\u8fdb\u884c\u5927\u91cf\u6570\u636e\u5904\u7406\u548c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u7b80\u5355\u800c\u9ad8\u6548\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u8fdb\u884cBGR\u5230RGB\u7684\u8f6c\u6362\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528OpenCV\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528OpenCV\u5e93\u975e\u5e38\u7b80\u5355\u5730\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.cvtColor()<\/code>\u51fd\u6570\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5OpenCV\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install opencv-python<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li>\u4f7f\u7528<code>cv2.imread()<\/code>\u8bfb\u53d6BGR\u56fe\u50cf\u3002<\/li>\n<li>\u901a\u8fc7<code>cv2.cvtColor(image, cv2.COLOR_BGR2RGB)<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<br \/>\n\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230RGB\u683c\u5f0f\u7684\u56fe\u50cf\u3002<\/li>\n<\/ol>\n<p><strong>\u9664\u4e86OpenCV\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u5b9e\u73b0BGR\u5230RGB\u7684\u8f6c\u6362\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u9664\u4e86OpenCV\uff0cPIL\uff08Pillow\uff09\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0BGR\u5230RGB\u7684\u8f6c\u6362\u3002\u4f7f\u7528PIL\u65f6\uff0c\u9996\u5148\u9700\u8981\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aPIL\u683c\u5f0f\u7684\u56fe\u50cf\uff0c\u7136\u540e\u4f7f\u7528<code>convert(&quot;RGB&quot;)<\/code>\u65b9\u6cd5\u8fdb\u884c\u989c\u8272\u6a21\u5f0f\u8f6c\u6362\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528<code>Image.fromarray()<\/code>\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aPIL\u56fe\u50cf\u3002<\/li>\n<li>\u4f7f\u7528<code>convert(&quot;RGB&quot;)<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<\/li>\n<\/ol>\n<p><strong>\u8f6c\u6362\u540e\u5982\u4f55\u4fdd\u5b58RGB\u56fe\u50cf\uff1f<\/strong><br \/>\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u540e\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5c06\u5176\u4fdd\u5b58\u3002\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite(&quot;output.png&quot;, rgb_image)<\/code>\u6765\u4fdd\u5b58\u8f6c\u6362\u540e\u7684\u56fe\u50cf\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u4e4b\u524d\uff0c\u56fe\u50cf\u7684\u989c\u8272\u683c\u5f0f\u5df2\u7ecf\u662fRGB\u3002\u5982\u679c\u4f7f\u7528PIL\uff0c\u53ef\u4ee5\u4f7f\u7528<code>image.save(&quot;output.png&quot;)<\/code>\u6765\u4fdd\u5b58\u3002<\/p>\n<p><strong>\u8f6c\u6362BGR\u5230RGB\u65f6\uff0c\u662f\u5426\u4f1a\u5f71\u54cd\u56fe\u50cf\u8d28\u91cf\uff1f<\/strong><br \/>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f6c\u6362BGR\u5230RGB\u683c\u5f0f\u4e0d\u4f1a\u5f71\u54cd\u56fe\u50cf\u8d28\u91cf\uff0c\u56e0\u4e3a\u8fd9\u53ea\u662f\u989c\u8272\u901a\u9053\u7684\u91cd\u65b0\u6392\u5217\u3002\u7136\u800c\uff0c\u786e\u4fdd\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u548c\u8303\u56f4\uff08\u4f8b\u5982\uff0c0-255\u7684\u50cf\u7d20\u503c\uff09\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4ee5\u907f\u514d\u53ef\u80fd\u7684\u8d28\u91cf\u635f\u5931\u3002\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u4e5f\u8981\u786e\u4fdd\u4f7f\u7528\u652f\u6301\u6240\u9700\u989c\u8272\u7a7a\u95f4\u7684\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06BGR\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u56fe\u50cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528OpenCV\u5e93\u3001Pillow\u5e93\u7b49\u5de5\u5177\u3002O [&hellip;]","protected":false},"author":3,"featured_media":1077242,"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\/1077237"}],"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=1077237"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077237\/revisions"}],"predecessor-version":[{"id":1077244,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077237\/revisions\/1077244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077242"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}