{"id":1172745,"date":"2025-01-15T16:53:47","date_gmt":"2025-01-15T08:53:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172745.html"},"modified":"2025-01-15T16:53:50","modified_gmt":"2025-01-15T08:53:50","slug":"python%e9%87%8c%e9%9d%a2%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0rgb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172745.html","title":{"rendered":"python\u91cc\u9762\u5982\u4f55\u627e\u5230rgb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074728\/6c6de2c4-6b89-43d5-ae0d-7e2232a6f09a.webp\" alt=\"python\u91cc\u9762\u5982\u4f55\u627e\u5230rgb\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u627e\u5230RGB\u989c\u8272\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u7684\u9014\u5f84\u5305\u62ec\u4f7f\u7528\u5e93\u51fd\u6570\u3001\u56fe\u50cf\u5904\u7406\u5e93\u3001\u4ee5\u53ca\u989c\u8272\u8f6c\u6362\u5de5\u5177\u7b49\u3002<strong>\u4f60\u53ef\u4ee5\u901a\u8fc7<code>matplotlib<\/code>\u3001<code>Pillow<\/code>\u5e93\u3001\u989c\u8272\u8f6c\u6362\u5de5\u5177\u6765\u627e\u5230RGB\u503c\u3001\u4e5f\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u5b9a\u4e49\u989c\u8272\u503c<\/strong>\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>Pillow<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>Pillow\u5e93\u662fPython\u4e2d\u975e\u5e38\u6d41\u884c\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6RGB\u989c\u8272\u503c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u5e76\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;your_image_file.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>width, height = image.size<\/p>\n<h2><strong>\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c<\/strong><\/h2>\n<p>pixel = image.getpixel((width\/\/2, height\/\/2))<\/p>\n<p>print(f&quot;RGB\u503c\u4e3a: {pixel}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b89\u88c5\u53ca\u4f7f\u7528<code>matplotlib<\/code><\/h3>\n<\/p>\n<p><p><code>matplotlib<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5404\u79cd\u56fe\u8868\u548c\u56fe\u5f62\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u989c\u8272\u7684RGB\u503c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u989c\u8272\u540d\u79f0\u6216\u8005\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u8f6c\u6362\u4e3aRGB\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.colors as mcolors<\/p>\n<h2><strong>\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c<\/strong><\/h2>\n<p>rgb = mcolors.to_rgb(&#39;blue&#39;)<\/p>\n<p>print(f&quot;\u84dd\u8272\u7684RGB\u503c\u4e3a: {rgb}&quot;)<\/p>\n<h2><strong>\u901a\u8fc7\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u83b7\u53d6RGB\u503c<\/strong><\/h2>\n<p>rgb_hex = mcolors.hex2color(&#39;#FF5733&#39;)<\/p>\n<p>print(f&quot;\u5341\u516d\u8fdb\u5236\u989c\u8272 #FF5733 \u7684RGB\u503c\u4e3a: {rgb_hex}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u81ea\u5b9a\u4e49\u989c\u8272<\/h3>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u5b9a\u4e49\u989c\u8272\u7684RGB\u503c\uff0c\u53ea\u9700\u8981\u8bb0\u4f4fRGB\u503c\u7684\u8303\u56f4\u662f\u4ece0\u5230255\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7ea2\u8272\u7684RGB\u503c<\/p>\n<p>red = (255, 0, 0)<\/p>\n<p>print(f&quot;\u7ea2\u8272\u7684RGB\u503c\u4e3a: {red}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u6790<\/h3>\n<\/p>\n<p><h4>\u4e00\u3001Pillow\u5e93\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>Pillow\u5e93\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6RGB\u989c\u8272\u503c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u5e76\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5Pillow\u5e93<\/strong><\/p>\n<p>\u5728\u4f7f\u7528Pillow\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u83b7\u53d6RGB\u503c<\/strong><\/p>\n<p>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u5e76\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;your_image_file.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>width, height = image.size<\/p>\n<h2><strong>\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c<\/strong><\/h2>\n<p>pixel = image.getpixel((width\/\/2, height\/\/2))<\/p>\n<p>print(f&quot;RGB\u503c\u4e3a: {pixel}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Pillow\u5e93\u7684<code>Image.open()<\/code>\u65b9\u6cd5\u6765\u6253\u5f00\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\u3002\u7136\u540e\uff0c\u901a\u8fc7<code>image.size<\/code>\u83b7\u53d6\u56fe\u50cf\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5e76\u901a\u8fc7<code>image.getpixel()<\/code>\u65b9\u6cd5\u83b7\u53d6\u56fe\u50cf\u4e2d\u67d0\u4e2a\u50cf\u7d20\u7684RGB\u503c\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u83b7\u53d6\u7684\u662f\u56fe\u50cf\u4e2d\u592e\u4f4d\u7f6e\u7684\u50cf\u7d20RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;your_image_file.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>width, height = image.size<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c<\/strong><\/h2>\n<p>pixels = list(image.getdata())<\/p>\n<p>print(f&quot;\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c\u4e3a: {pixels}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Pillow\u5e93\u7684<code>image.getdata()<\/code>\u65b9\u6cd5\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u4f7f\u7528<code>matplotlib<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>matplotlib<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5404\u79cd\u56fe\u8868\u548c\u56fe\u5f62\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u989c\u8272\u7684RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5<code>matplotlib<\/code>\u5e93<\/strong><\/p>\n<p>\u5728\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5<code>matplotlib<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.colors as mcolors<\/p>\n<h2><strong>\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c<\/strong><\/h2>\n<p>rgb = mcolors.to_rgb(&#39;blue&#39;)<\/p>\n<p>print(f&quot;\u84dd\u8272\u7684RGB\u503c\u4e3a: {rgb}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>matplotlib.colors<\/code>\u6a21\u5757\u7684<code>to_rgb()<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u83b7\u53d6RGB\u503c<\/strong><\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u83b7\u53d6RGB\u503c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.colors as mcolors<\/p>\n<h2><strong>\u901a\u8fc7\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u83b7\u53d6RGB\u503c<\/strong><\/h2>\n<p>rgb_hex = mcolors.hex2color(&#39;#FF5733&#39;)<\/p>\n<p>print(f&quot;\u5341\u516d\u8fdb\u5236\u989c\u8272 #FF5733 \u7684RGB\u503c\u4e3a: {rgb_hex}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>matplotlib.colors<\/code>\u6a21\u5757\u7684<code>hex2color()<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u83b7\u53d6RGB\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u4e09\u3001\u624b\u52a8\u5b9a\u4e49\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u5b9a\u4e49\u989c\u8272\u7684RGB\u503c\uff0c\u53ea\u9700\u8981\u8bb0\u4f4fRGB\u503c\u7684\u8303\u56f4\u662f\u4ece0\u5230255\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9a\u4e49\u7ea2\u8272\u7684RGB\u503c<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7ea2\u8272\u7684RGB\u503c<\/p>\n<p>red = (255, 0, 0)<\/p>\n<p>print(f&quot;\u7ea2\u8272\u7684RGB\u503c\u4e3a: {red}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u5b9a\u4e49\u4e86\u7ea2\u8272\u7684RGB\u503c\u4e3a<code>(255, 0, 0)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u56db\u3001\u989c\u8272\u8f6c\u6362\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8fd8\u6709\u4e00\u4e9b\u989c\u8272\u8f6c\u6362\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u5982RGB\u3001HSV\u3001HSL\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>colorsys<\/code>\u6a21\u5757\u8fdb\u884c\u989c\u8272\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684<code>colorsys<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u529f\u80fd\uff0c\u5982RGB\u4e0eHSV\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5\u5e76\u4f7f\u7528<code>colorsys<\/code>\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p><code>colorsys<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>RGB\u8f6cHSV<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import colorsys<\/p>\n<h2><strong>\u5b9a\u4e49\u7ea2\u8272\u7684RGB\u503c<\/strong><\/h2>\n<p>rgb = (255, 0, 0)<\/p>\n<h2><strong>\u5c06RGB\u503c\u8f6c\u6362\u4e3aHSV\u503c<\/strong><\/h2>\n<p>hsv = colorsys.rgb_to_hsv(rgb[0]\/255, rgb[1]\/255, rgb[2]\/255)<\/p>\n<p>print(f&quot;\u7ea2\u8272\u7684HSV\u503c\u4e3a: {hsv}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>colorsys<\/code>\u6a21\u5757\u7684<code>rgb_to_hsv()<\/code>\u65b9\u6cd5\uff0c\u5c06RGB\u503c\u8f6c\u6362\u4e3aHSV\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>HSV\u8f6cRGB<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import colorsys<\/p>\n<h2><strong>\u5b9a\u4e49\u7ea2\u8272\u7684HSV\u503c<\/strong><\/h2>\n<p>hsv = (0.0, 1.0, 1.0)<\/p>\n<h2><strong>\u5c06HSV\u503c\u8f6c\u6362\u4e3aRGB\u503c<\/strong><\/h2>\n<p>rgb = colorsys.hsv_to_rgb(hsv[0], hsv[1], hsv[2])<\/p>\n<p>print(f&quot;\u7ea2\u8272\u7684RGB\u503c\u4e3a: {rgb}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>colorsys<\/code>\u6a21\u5757\u7684<code>hsv_to_rgb()<\/code>\u65b9\u6cd5\uff0c\u5c06HSV\u503c\u8f6c\u6362\u4e3aRGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>webcolors<\/code>\u5e93\u8fdb\u884c\u989c\u8272\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p><code>webcolors<\/code>\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406Web\u989c\u8272\u7684Python\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u989c\u8272\u540d\u79f0\u548cRGB\u503c\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5<code>webcolors<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>webcolors<\/code>\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5<code>webcolors<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install webcolors<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u989c\u8272\u540d\u79f0\u8f6cRGB<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import webcolors<\/p>\n<h2><strong>\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c<\/strong><\/h2>\n<p>rgb = webcolors.name_to_rgb(&#39;blue&#39;)<\/p>\n<p>print(f&quot;\u84dd\u8272\u7684RGB\u503c\u4e3a: {rgb}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>webcolors<\/code>\u5e93\u7684<code>name_to_rgb()<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7\u989c\u8272\u540d\u79f0\u83b7\u53d6RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>RGB\u8f6c\u989c\u8272\u540d\u79f0<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import webcolors<\/p>\n<h2><strong>\u5b9a\u4e49\u84dd\u8272\u7684RGB\u503c<\/strong><\/h2>\n<p>rgb = (0, 0, 255)<\/p>\n<h2><strong>\u5c06RGB\u503c\u8f6c\u6362\u4e3a\u989c\u8272\u540d\u79f0<\/strong><\/h2>\n<p>color_name = webcolors.rgb_to_name(rgb)<\/p>\n<p>print(f&quot;RGB\u503c\u4e3a {rgb} \u7684\u989c\u8272\u540d\u79f0\u4e3a: {color_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>webcolors<\/code>\u5e93\u7684<code>rgb_to_name()<\/code>\u65b9\u6cd5\uff0c\u5c06RGB\u503c\u8f6c\u6362\u4e3a\u989c\u8272\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>\u4e94\u3001\u4f7f\u7528<code>opencv<\/code>\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p><code>opencv<\/code>\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5<code>opencv<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>opencv<\/code>\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5<code>opencv<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u83b7\u53d6RGB\u503c<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = cv2.imread(&#39;your_image_file.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>height, width, _ = image.shape<\/p>\n<h2><strong>\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c<\/strong><\/h2>\n<p>pixel = image[height\/\/2, width\/\/2]<\/p>\n<p>print(f&quot;RGB\u503c\u4e3a: {pixel}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>opencv<\/code>\u5e93\u7684<code>cv2.imread()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\u3002\u7136\u540e\uff0c\u901a\u8fc7<code>image.shape<\/code>\u83b7\u53d6\u56fe\u50cf\u7684\u9ad8\u5ea6\u548c\u5bbd\u5ea6\uff0c\u5e76\u901a\u8fc7\u76f4\u63a5\u7d22\u5f15\u7684\u65b9\u5f0f\u83b7\u53d6\u56fe\u50cf\u4e2d\u67d0\u4e2a\u50cf\u7d20\u7684RGB\u503c\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u83b7\u53d6\u7684\u662f\u56fe\u50cf\u4e2d\u592e\u4f4d\u7f6e\u7684\u50cf\u7d20RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = cv2.imread(&#39;your_image_file.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>height, width, _ = image.shape<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c<\/strong><\/h2>\n<p>pixels = image.reshape(-1, 3)<\/p>\n<p>print(f&quot;\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c\u4e3a: {pixels}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>opencv<\/code>\u5e93\u7684<code>cv2.imread()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>image.reshape(-1, 3)<\/code>\u5c06\u56fe\u50cf\u4e2d\u7684\u6240\u6709\u50cf\u7d20\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u50cf\u7d20\u7684RGB\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u627e\u5230RGB\u989c\u8272\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u7684\u9014\u5f84\u5305\u62ec\u4f7f\u7528\u5e93\u51fd\u6570\u3001\u56fe\u50cf\u5904\u7406\u5e93\u3001\u4ee5\u53ca\u989c\u8272\u8f6c\u6362\u5de5\u5177\u7b49\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7<code>Pillow<\/code>\u5e93\u3001<code>matplotlib<\/code>\u5e93\u3001\u624b\u52a8\u5b9a\u4e49\u989c\u8272\u3001\u989c\u8272\u8f6c\u6362\u5de5\u5177\u4ee5\u53ca<code>opencv<\/code>\u5e93\u6765\u627e\u5230RGB\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e3b\u8981\u65b9\u6cd5\u603b\u7ed3\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>Pillow<\/code>\u5e93<\/strong>\uff1a\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6RGB\u989c\u8272\u503c\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>matplotlib<\/code>\u5e93<\/strong>\uff1a\u901a\u8fc7\u989c\u8272\u540d\u79f0\u6216\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u83b7\u53d6RGB\u503c\u3002<\/li>\n<li><strong>\u624b\u52a8\u5b9a\u4e49\u989c\u8272<\/strong>\uff1a\u76f4\u63a5\u5b9a\u4e49\u989c\u8272\u7684RGB\u503c\u3002<\/li>\n<li><strong>\u989c\u8272\u8f6c\u6362\u5de5\u5177<\/strong>\uff1a\u5982<code>colorsys<\/code>\u6a21\u5757\u548c<code>webcolors<\/code>\u5e93\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>opencv<\/code>\u5e93<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6\u56fe\u50cf\u5e76\u83b7\u53d6RGB\u503c\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u627e\u5230\u5e76\u5904\u7406RGB\u989c\u8272\u503c\u3002\u65e0\u8bba\u662f\u56fe\u50cf\u5904\u7406\u3001\u6570\u636e\u53ef\u89c6\u5316\u8fd8\u662f\u5176\u4ed6\u5e94\u7528\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u4e3a\u4f60\u63d0\u4f9b\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u56fe\u50cf\u7684RGB\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u5e93\u7684Pillow\u6a21\u5757\u6765\u83b7\u53d6\u56fe\u50cf\u7684RGB\u503c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>getpixel()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00\u56fe\u50cf\nimg = Image.open(&#39;image.jpg&#39;)\n\n# \u83b7\u53d6\u7279\u5b9a\u50cf\u7d20\u7684RGB\u503c\nrgb_value = img.getpixel((x, y))\nprint(rgb_value)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u91cc\uff0c<code>(x, y)<\/code>\u662f\u4f60\u60f3\u8981\u83b7\u53d6RGB\u503c\u7684\u50cf\u7d20\u5750\u6807\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06RGB\u503c\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u60f3\u5c06RGB\u503c\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u5b9e\u73b0\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">def rgb_to_hex(rgb):\n    return &#39;#{:02x}{:02x}{:02x}&#39;.format(*rgb)\n\n# \u793a\u4f8b\nrgb_value = (255, 255, 255)  # \u767d\u8272\nhex_value = rgb_to_hex(rgb_value)\nprint(hex_value)  # \u8f93\u51fa\uff1a#ffffff\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u4efb\u4f55RGB\u503c\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u53d6\u56fe\u50cf\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u7684RGB\u503c\u5e76\u5b58\u50a8\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u63d0\u53d6\u56fe\u50cf\u4e2d\u6240\u6709\u50cf\u7d20\u7684RGB\u503c\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u7684<code>load()<\/code>\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00\u56fe\u50cf\nimg = Image.open(&#39;image.jpg&#39;)\npixels = img.load()  # \u52a0\u8f7d\u56fe\u50cf\u50cf\u7d20\n\n# \u83b7\u53d6\u56fe\u50cf\u7684\u5bbd\u548c\u9ad8\nwidth, height = img.size\n\n# \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8RGB\u503c\nrgb_list = []\n\n# \u904d\u5386\u6240\u6709\u50cf\u7d20\u5e76\u83b7\u53d6RGB\u503c\nfor y in range(height):\n    for x in range(width):\n        rgb_list.append(pixels[x, y])\n\nprint(rgb_list)  # \u8f93\u51fa\u6240\u6709RGB\u503c\u7684\u5217\u8868\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u56fe\u50cf\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u7684RGB\u503c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u4ee5\u4f9b\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u627e\u5230RGB\u989c\u8272\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u7684\u9014\u5f84\u5305\u62ec\u4f7f\u7528\u5e93\u51fd\u6570\u3001\u56fe\u50cf\u5904\u7406\u5e93\u3001\u4ee5\u53ca\u989c\u8272\u8f6c\u6362\u5de5\u5177\u7b49\u3002\u4f60\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1172751,"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\/1172745"}],"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=1172745"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172745\/revisions"}],"predecessor-version":[{"id":1172752,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172745\/revisions\/1172752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172751"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}