{"id":985904,"date":"2024-12-27T07:40:32","date_gmt":"2024-12-26T23:40:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985904.html"},"modified":"2024-12-27T07:40:34","modified_gmt":"2024-12-26T23:40:34","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%81%af%e5%85%89%e5%8f%98%e4%ba%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985904.html","title":{"rendered":"python\u5982\u4f55\u8c03\u706f\u5149\u53d8\u4eae"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062734\/9c74e9ee-8184-4347-a39d-1ad9c37cf5b9.webp\" alt=\"python\u5982\u4f55\u8c03\u706f\u5149\u53d8\u4eae\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u8282\u706f\u5149\u53d8\u4eae\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528GPIO\u5e93\u63a7\u5236\u786c\u4ef6\u8bbe\u5907\u3001\u5229\u7528\u667a\u80fd\u5bb6\u5c45API\u5b9e\u73b0\u8fdc\u7a0b\u63a7\u5236\u3001\u6216\u8005\u901a\u8fc7\u56fe\u5f62\u5904\u7406\u5e93\u8c03\u6574\u56fe\u50cf\u4eae\u5ea6<\/strong>\u3002\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u5229\u7528\u667a\u80fd\u5bb6\u5c45API\u6765\u8fdc\u7a0b\u63a7\u5236\u706f\u5149\u8bbe\u5907\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u96c6\u6210\u5230\u5bb6\u5ead\u81ea\u52a8\u5316\u7cfb\u7edf\u4e2d\uff0c\u5b9e\u73b0\u667a\u80fd\u5316\u7684\u706f\u5149\u7ba1\u7406\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u706f\u5149\u7684\u8c03\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528GPIO\u5e93\u63a7\u5236\u786c\u4ef6\u8bbe\u5907<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u63a7\u5236\u7269\u7406\u706f\u5149\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7GPIO\u5e93\u4e0e\u6811\u8393\u6d3e\u7b49\u786c\u4ef6\u8bbe\u5907\u63a5\u53e3\u3002GPIO\uff08General Purpose Input\/Output\uff09\u7528\u4e8e\u63a7\u5236\u7269\u7406\u8bbe\u5907\u7684\u8f93\u5165\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528GPIO\u5e93\u63a7\u5236\u706f\u5149\u4eae\u5ea6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u4e0e\u8bbe\u7f6eGPIO\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728\u6811\u8393\u6d3e\u4e0a\u5b89\u88c5RPi.GPIO\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install python3-rpi.gpio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8bbe\u7f6eGPIO\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>GPIO.setmode(GPIO.BCM)  # \u4f7f\u7528BCM\u7f16\u53f7\u6a21\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u4e0e\u63a7\u5236\u706f\u5149<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u706f\u5149\u8bbe\u5907\u8fde\u63a5\u5230\u6811\u8393\u6d3e\u7684GPIO\u5f15\u811a\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u63a7\u5236\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">LED_PIN = 18  # \u5047\u8bbeLED\u8fde\u63a5\u5728GPIO 18\u4e0a<\/p>\n<p>GPIO.setup(LED_PIN, GPIO.OUT)<\/p>\n<h2><strong>\u63a7\u5236\u4eae\u5ea6\uff1aPWM\uff08\u8109\u5bbd\u8c03\u5236\uff09<\/strong><\/h2>\n<p>pwm = GPIO.PWM(LED_PIN, 1000)  # \u8bbe\u7f6e\u9891\u7387\u4e3a1kHz<\/p>\n<p>pwm.start(0)  # \u521d\u59cb\u5360\u7a7a\u6bd4\u4e3a0\uff08\u706f\u5149\u5173\u95ed\uff09<\/p>\n<h2><strong>\u8c03\u8282\u4eae\u5ea6<\/strong><\/h2>\n<p>def set_brightness(brightness):<\/p>\n<p>    pwm.ChangeDutyCycle(brightness)  # \u8c03\u6574\u5360\u7a7a\u6bd4\uff0c\u8303\u56f40-100<\/p>\n<p>set_brightness(70)  # \u5c06\u4eae\u5ea6\u8bbe\u7f6e\u4e3a70%<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e05\u7406\u4e0e\u5173\u95ed<\/strong><\/p>\n<\/p>\n<p><p>\u7ed3\u675f\u7a0b\u5e8f\u65f6\uff0c\u9700\u8981\u6e05\u7406GPIO\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pwm.stop()<\/p>\n<p>GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5229\u7528\u667a\u80fd\u5bb6\u5c45API\u5b9e\u73b0\u8fdc\u7a0b\u63a7\u5236<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u667a\u80fd\u706f\u6ce1\u548c\u7167\u660e\u7cfb\u7edf\u652f\u6301API\u63a7\u5236\uff0c\u6bd4\u5982Philips Hue\u3001LIFX\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9bAPI\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5b9e\u73b0\u706f\u5149\u7684\u8fdc\u7a0b\u63a7\u5236\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5e76\u5b89\u88c5API\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u6839\u636e\u4f7f\u7528\u7684\u667a\u80fd\u8bbe\u5907\u9009\u62e9\u5408\u9002\u7684API\u5e93\u3002\u4f8b\u5982\uff0c\u63a7\u5236Philips Hue\u706f\u6ce1\u53ef\u4ee5\u4f7f\u7528<code>phue<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install phue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u5e76\u63a7\u5236\u706f\u5149<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528API\u8fde\u63a5\u5230\u667a\u80fd\u706f\u6ce1\u5e76\u8c03\u8282\u4eae\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from phue import Bridge<\/p>\n<h2><strong>\u8fde\u63a5\u5230Hue Bridge<\/strong><\/h2>\n<p>bridge = Bridge(&#39;BRIDGE_IP_ADDRESS&#39;)<\/p>\n<p>bridge.connect()<\/p>\n<h2><strong>\u83b7\u53d6\u706f\u6ce1\u5217\u8868<\/strong><\/h2>\n<p>lights = bridge.lights<\/p>\n<h2><strong>\u8bbe\u7f6e\u4eae\u5ea6<\/strong><\/h2>\n<p>def set_light_brightness(light_name, brightness):<\/p>\n<p>    for light in lights:<\/p>\n<p>        if light.name == light_name:<\/p>\n<p>            light.brightness = brightness<\/p>\n<p>            break<\/p>\n<p>set_light_brightness(&#39;Living Room Lamp&#39;, 200)  # \u8bbe\u7f6e\u4eae\u5ea6\uff08\u8303\u56f40-254\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e30\u5bcc\u7684\u63a7\u5236\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u667a\u80fd\u5bb6\u5c45API\u901a\u5e38\u63d0\u4f9b\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u8c03\u8272\u3001\u9884\u8bbe\u573a\u666f\u3001\u5b9a\u65f6\u63a7\u5236\u7b49\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6269\u5c55\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7\u56fe\u5f62\u5904\u7406\u5e93\u8c03\u6574\u56fe\u50cf\u4eae\u5ea6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8c03\u6574\u56fe\u50cf\u4e2d\u7684\u706f\u5149\u4eae\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u56fe\u5f62\u5904\u7406\u5e93\uff0c\u5982PIL\uff08Pillow\uff09\u6216OpenCV\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Pillow\u8c03\u6574\u56fe\u50cf\u4eae\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageEnhance<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path\/to\/image.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u4eae\u5ea6<\/strong><\/h2>\n<p>enhancer = ImageEnhance.Brightness(image)<\/p>\n<p>bright_image = enhancer.enhance(1.5)  # \u589e\u52a0\u4eae\u5ea6<\/p>\n<p>bright_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528OpenCV\u8c03\u6574\u56fe\u50cf\u4eae\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\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;path\/to\/image.jpg&#39;)<\/p>\n<h2><strong>\u589e\u52a0\u4eae\u5ea6<\/strong><\/h2>\n<p>bright_image = cv2.convertScaleAbs(image, alpha=1.2, beta=50)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Brightened Image&#39;, bright_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<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5728Python\u4e2d\u5b9e\u73b0\u706f\u5149\u4eae\u5ea6\u7684\u8c03\u8282\u3002\u65e0\u8bba\u662f\u63a7\u5236\u7269\u7406\u8bbe\u5907\u3001\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\uff0c\u8fd8\u662f\u5904\u7406\u56fe\u50cf\uff0c\u90fd\u53ef\u4ee5\u627e\u5230\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5e94\u7528\u573a\u666f\u548c\u8bbe\u5907\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63a7\u5236\u706f\u5149\u4eae\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5e93\u6765\u63a7\u5236\u706f\u5149\u7684\u4eae\u5ea6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>Raspberry Pi<\/code>\u7684<code>GPIO<\/code>\u5e93\u53ef\u4ee5\u76f4\u63a5\u63a7\u5236LED\u706f\u7684\u4eae\u5ea6\uff0c\u901a\u8fc7PWM\uff08\u8109\u5bbd\u8c03\u5236\uff09\u4fe1\u53f7\u8c03\u8282\u8f93\u51fa\u7535\u538b\uff0c\u4ece\u800c\u6539\u53d8\u4eae\u5ea6\u3002\u6b64\u5916\uff0c\u8bb8\u591a\u667a\u80fd\u706f\u6ce1\u4e5f\u63d0\u4f9b\u4e86API\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7HTTP\u8bf7\u6c42\u6765\u8c03\u6574\u706f\u5149\u4eae\u5ea6\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u63a7\u5236\u706f\u5149\uff1f<\/strong><br \/>\u6709\u51e0\u4e2a\u6d41\u884c\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u63a7\u5236\u706f\u5149\uff0c\u5982<code>RPi.GPIO<\/code>\u7528\u4e8e\u6811\u8393\u6d3e\u3001<code>pySerial<\/code>\u7528\u4e8e\u4e32\u53e3\u901a\u4fe1\u3001\u4ee5\u53ca<code>phue<\/code>\u5e93\u7528\u4e8e\u4e0ePhilips Hue\u706f\u6ce1\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199Python\u4ee3\u7801\u6765\u5b9e\u73b0\u8c03\u5149\u529f\u80fd\uff0c\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684\u786c\u4ef6\u548c\u706f\u5149\u7c7b\u578b\u3002<\/p>\n<p><strong>\u8c03\u8282\u706f\u5149\u4eae\u5ea6\u9700\u8981\u54ea\u4e9b\u786c\u4ef6\u652f\u6301\uff1f<\/strong><br \/>\u8c03\u8282\u706f\u5149\u4eae\u5ea6\u901a\u5e38\u9700\u8981\u5408\u9002\u7684\u786c\u4ef6\u652f\u6301\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u4f20\u7edf\u7684\u706f\u6ce1\uff0c\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u80fd\u591f\u652f\u6301PWM\u7684\u5fae\u63a7\u5236\u5668\uff0c\u5982Arduino\u6216\u6811\u8393\u6d3e\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u667a\u80fd\u706f\u6ce1\uff0c\u786e\u4fdd\u60a8\u62e5\u6709\u76f8\u5e94\u7684\u7f51\u7edc\u8fde\u63a5\u548c\u517c\u5bb9\u7684\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\u3002\u4e86\u89e3\u60a8\u7684\u8bbe\u5907\u53ca\u5176\u89c4\u683c\u5c06\u6709\u52a9\u4e8e\u987a\u5229\u5b9e\u73b0\u8c03\u5149\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u8282\u706f\u5149\u53d8\u4eae\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528GPIO\u5e93\u63a7\u5236\u786c\u4ef6\u8bbe\u5907\u3001\u5229\u7528\u667a\u80fd\u5bb6\u5c45API\u5b9e\u73b0\u8fdc\u7a0b\u63a7\u5236\u3001\u6216\u8005\u901a\u8fc7\u56fe\u5f62\u5904 [&hellip;]","protected":false},"author":3,"featured_media":985911,"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\/985904"}],"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=985904"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985904\/revisions"}],"predecessor-version":[{"id":985913,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985904\/revisions\/985913"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}