{"id":173879,"date":"2024-05-08T18:20:38","date_gmt":"2024-05-08T10:20:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173879.html"},"modified":"2024-05-08T18:20:43","modified_gmt":"2024-05-08T10:20:43","slug":"python%e5%a6%82%e4%bd%95%e8%af%86%e5%88%ab%e6%8c%87%e5%ae%9a%e5%9d%90%e6%a0%87%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173879.html","title":{"rendered":"python\u5982\u4f55\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045926\/906fc266-4588-4972-ba44-157938e9eb72.webp\" alt=\"python\u5982\u4f55\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u989c\u8272\" \/><\/p>\n<p><p>Python\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u989c\u8272\u4e3b\u8981\u4f9d\u8d56\u4e8e\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982Pillow\uff08PIL\uff09\u548cOpenCV\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5230\u56fe\u50cf\u4e2d\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u503c\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002<strong>\u6838\u5fc3\u8fc7\u7a0b\u5305\u62ec\uff1a\u5b89\u88c5\u548c\u5bfc\u5165\u6240\u9700\u5e93\u3001\u52a0\u8f7d\u56fe\u50cf\u3001\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u503c\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u503c<\/strong>\u662f\u672c\u6587\u5c06\u8981\u91cd\u70b9\u5c55\u5f00\u63cf\u8ff0\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u503c\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Pillow\u5e93\uff08from PIL import Image\uff09\uff0c\u7136\u540e\u52a0\u8f7d\u56fe\u50cf\uff08img = Image.open(&quot;your_image.png&quot;)\uff09\uff0c\u6700\u540e\u901a\u8fc7img.getpixel((x, y))\u65b9\u6cd5\u5373\u53ef\u83b7\u53d6\u6307\u5b9a\u5750\u6807\uff08x, y\uff09\u7684\u989c\u8272\u503c\u3002\u6b64\u8fd4\u56de\u503c\u6839\u636e\u56fe\u50cf\u7684\u6a21\u5f0f\u4e0d\u540c\uff08\u5982RGB\u3001RGBA\u7b49\uff09\u800c\u53ef\u80fd\u5305\u542b\u4e0d\u540c\u6570\u91cf\u7684\u6570\u636e\uff0c\u4f8b\u5982\uff0cRGB\u6a21\u5f0f\u4e0b\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u6574\u6570\u7684\u5143\u7ec4\uff08R, G, B\uff09\uff0c\u5206\u522b\u4ee3\u8868\u7ea2\u3001\u7eff\u3001\u84dd\u4e09\u79cd\u989c\u8272\u7684\u5f3a\u5ea6\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u6216OpenCV\u5e93\u3002\u8fd9\u4e9b\u5e93\u4e0d\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u5668\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u5982\uff1a<code>pip install Pillow<\/code> \u6216 <code>pip install opencv-python<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u9700\u8981\u5728Python\u811a\u672c\u7684\u5f00\u5934\u5bfc\u5165\u8fd9\u4e9b\u5e93\u3002\u4f7f\u7528Pillow\u5e93\u65f6\uff0c\u901a\u8fc7 <code>from PIL import Image<\/code> \u6765\u5bfc\u5165Image\u6a21\u5757\u3002\u5982\u679c\u60a8\u6253\u7b97\u4f7f\u7528OpenCV\uff0c\u5219\u9700\u8981\u901a\u8fc7 <code>import cv2<\/code> \u6765\u5bfc\u5165cv2\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a0\u8f7d\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u52a0\u8f7d\u56fe\u50cf\u662f\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u989c\u8272\u7684\u7b2c\u4e00\u6b65\u3002\u4e0d\u540c\u7684\u5e93\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u52a0\u8f7d\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528Pillow\u52a0\u8f7d\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528<code>Image.open(\u56fe\u50cf\u8def\u5f84)<\/code>\u5373\u53ef\u3002\u4f8b\u5982\uff1a<code>img = Image.open(&quot;example.jpg&quot;)<\/code>\u3002<\/li>\n<li>\u5bf9\u4e8eOpenCV\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cv2.imread(\u56fe\u50cf\u8def\u5f84)<\/code>\u6765\u52a0\u8f7d\u56fe\u50cf\u3002\u4f8b\u5982\uff1a<code>img = cv2.imread(&quot;example.jpg&quot;)<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u503c<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u503c\u662f\u6838\u5fc3\u6b65\u9aa4\u3002\u8fd9\u8981\u6c42\u6211\u4eec\u5148\u77e5\u9053\u8981\u67e5\u8be2\u989c\u8272\u7684\u5750\u6807\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5728Pillow\u4e2d\uff0c\u4f7f\u7528<code>getpixel((x, y))<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5750\u6807(x, y)\u5904\u7684\u989c\u8272\u503c\u3002\u4f8b\u5982\uff1a<code>color = img.getpixel((50, 100))<\/code>\u3002<\/li>\n<li>\u5728OpenCV\u4e2d\uff0c\u56fe\u50cf\u88ab\u89c6\u4e3aNumPy\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u6765\u83b7\u53d6\u989c\u8272\u503c\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cOpenCV\u4f7f\u7528BGR\u800c\u975eRGB\u987a\u5e8f\u3002\u4f8b\u5982\uff1a<code>color = img[100, 50]<\/code>\uff08\u6ce8\u610f\u6b64\u5904\u662f<code>[y, x]<\/code>\u7684\u987a\u5e8f\uff09\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5904\u7406\u548c\u5206\u6790\u989c\u8272\u503c<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u989c\u8272\u503c\u540e\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\uff0c\u6bd4\u5982\u8f6c\u6362\u989c\u8272\u683c\u5f0f\u3001\u6bd4\u8f83\u989c\u8272\u5dee\u5f02\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u989c\u8272\u683c\u5f0f\u8f6c\u6362<\/strong>\uff1a\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u83b7\u53d6\u7684\u989c\u8272\u503c\u4ece\u4e00\u79cd\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4eceRGB\u8f6c\u6362\u4e3aHEX\u683c\u5f0f\u3002Pillow\u548cOpenCV\u90fd\u63d0\u4f9b\u4e86\u5de5\u5177\u51fd\u6570\u6765\u652f\u6301\u8fd9\u79cd\u8f6c\u6362\u3002<\/li>\n<li><strong>\u989c\u8272\u5dee\u5f02\u6bd4\u8f83<\/strong>\uff1a\u6bd4\u8f83\u4e24\u79cd\u989c\u8272\u4e4b\u95f4\u7684\u5dee\u5f02\u5bf9\u4e8e\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u6765\u8bf4\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u989c\u8272\u503c\u4e4b\u95f4\u7684\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u6765<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u989c\u8272\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u8bc6\u522b\u5e76\u5904\u7406\u56fe\u50cf\u4e2d\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u56fe\u50cf\u7f16\u8f91\u3001\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u8fd8\u662f\u8fdb\u884c\u89c6\u89c9\u8bc6\u522b\u7b49\u9886\u57df\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u90fd\u662f\u975e\u5e38\u6709\u76ca\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u5728\u56fe\u50cf\u4e2d\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\uff08Python Imaging Library\uff09\u548copencv\u5e93\u6765\u5904\u7406\u56fe\u50cf\u3002\u8981\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u7684\u989c\u8272\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a<code>from PIL import Image<\/code>\u548c<code>import cv2<\/code><\/li>\n<li>\u4f7f\u7528PIL\u5e93\u6253\u5f00\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aRGB\u6a21\u5f0f\uff1a<code>image = Image.open(&#039;image.jpg&#039;).convert(&#039;RGB&#039;)<\/code><\/li>\n<li>\u83b7\u53d6\u56fe\u50cf\u4e0a\u6307\u5b9a\u5750\u6807\u7684\u50cf\u7d20\u503c\uff1a<code>r, g, b = image.getpixel((x, y))<\/code><\/li>\n<li>\u540c\u6837\u7684\uff0c\u4f7f\u7528opencv\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\uff0c\u7136\u540e\u4f7f\u7528<code>image[y, x]<\/code>\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u4e0a\u7684\u50cf\u7d20\u503c\u3002<\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u5728Python\u4e2d\u6839\u636e\u5750\u6807\u5224\u65ad\u6307\u5b9a\u533a\u57df\u7684\u989c\u8272\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bf9\u56fe\u50cf\u7684\u50cf\u7d20\u8fdb\u884c\u9010\u4e2a\u904d\u5386\u6765\u5b9e\u73b0\u6839\u636e\u5750\u6807\u5224\u65ad\u6307\u5b9a\u533a\u57df\u7684\u989c\u8272\u3002\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a<code>import cv2<\/code><\/li>\n<li>\u4f7f\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u52a0\u8f7d\u56fe\u50cf<\/li>\n<li>\u904d\u5386\u6307\u5b9a\u533a\u57df\u5185\u7684\u50cf\u7d20\u5750\u6807<\/li>\n<li>\u4f7f\u7528<code>image[y, x]<\/code>\u83b7\u53d6\u6307\u5b9a\u5750\u6807\u4e0a\u7684\u50cf\u7d20\u503c\uff0c\u5e76\u5224\u65ad\u5176\u989c\u8272<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528Python\u7edf\u8ba1\u56fe\u50cf\u4e2d\u67d0\u4e2a\u989c\u8272\u7684\u50cf\u7d20\u6570\u91cf\uff1f<\/strong><\/p>\n<p>\u8981\u7edf\u8ba1\u56fe\u50cf\u4e2d\u67d0\u4e2a\u989c\u8272\u7684\u50cf\u7d20\u6570\u91cf\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a<code>from PIL import Image<\/code><\/li>\n<li>\u4f7f\u7528PIL\u5e93\u6253\u5f00\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aRGB\u6a21\u5f0f\uff1a<code>image = Image.open(&#039;image.jpg&#039;).convert(&#039;RGB&#039;)<\/code><\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u6765\u8ba1\u6570\u6307\u5b9a\u989c\u8272\u7684\u50cf\u7d20\u6570\uff1a<code>count = 0<\/code><\/li>\n<li>\u904d\u5386\u6574\u4e2a\u56fe\u50cf\u7684\u50cf\u7d20<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u50cf\u7d20\uff0c\u4f7f\u7528<code>r, g, b = image.getpixel((x, y))<\/code>\u83b7\u53d6\u5176RGB\u503c\uff0c\u5e76\u4e0e\u76ee\u6807\u989c\u8272\u8fdb\u884c\u6bd4\u8f83<\/li>\n<li>\u5982\u679c\u5339\u914d\u76ee\u6807\u989c\u8272\uff0c\u5219\u5c06\u8ba1\u6570\u5668\u52a01<\/li>\n<li>\u6700\u540e\u5f97\u5230\u7684\u8ba1\u6570\u503c\u5373\u4e3a\u56fe\u50cf\u4e2d\u6307\u5b9a\u989c\u8272\u7684\u50cf\u7d20\u6570\u91cf<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"Python\u8bc6\u522b\u6307\u5b9a\u5750\u6807\u989c\u8272\u4e3b\u8981\u4f9d\u8d56\u4e8e\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982Pillow\uff08PIL\uff09\u548cOpenCV\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":173885,"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\/173879"}],"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=173879"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173879\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173885"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}