{"id":1169936,"date":"2025-01-15T16:16:28","date_gmt":"2025-01-15T08:16:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169936.html"},"modified":"2025-01-15T16:16:30","modified_gmt":"2025-01-15T08:16:30","slug":"%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e8%a7%86%e9%a2%91%e6%96%87%e4%bb%b6python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169936.html","title":{"rendered":"\u5982\u4f55\u5efa\u7acb\u89c6\u9891\u6587\u4ef6python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070803\/f700399a-4a5e-4e93-be36-6763a723628e.webp\" alt=\"\u5982\u4f55\u5efa\u7acb\u89c6\u9891\u6587\u4ef6python\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5efa\u7acb\u89c6\u9891\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3001NumPy\u5e93\u3001Pillow\u5e93\u7b49\u5de5\u5177\uff0c\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fdb\u884c\u89c6\u9891\u6355\u83b7\u3001\u5904\u7406\u548c\u4fdd\u5b58\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff1a<\/strong>  <\/p>\n<\/p>\n<ol>\n<li><strong>OpenCV\u5e93\uff08cv2\uff09<\/strong>\uff1a\u7528\u4e8e\u89c6\u9891\u6355\u83b7\u3001\u5904\u7406\u548c\u5199\u5165\u3002<\/li>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u89c6\u9891\u5e27\u6570\u636e\u3002<\/li>\n<li><strong>Pillow\u5e93\uff08PIL\uff09<\/strong>\uff1a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u521b\u5efa\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p>pip install numpy<\/p>\n<p>pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u89c6\u9891\u6587\u4ef6\u7684\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bbe\u7f6e\u89c6\u9891\u6587\u4ef6\u7684\u53c2\u6570\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u521b\u5efa\u89c6\u9891\u6587\u4ef6\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u8bbe\u7f6e\u4e00\u4e9b\u53c2\u6570\uff0c\u4f8b\u5982\u89c6\u9891\u5e27\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u5e27\u7387\u3001\u7f16\u89e3\u7801\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u89c6\u9891\u53c2\u6570<\/p>\n<p>frame_width = 640<\/p>\n<p>frame_height = 480<\/p>\n<p>frame_rate = 30<\/p>\n<p>output_file = &#39;output_video.avi&#39;<\/p>\n<h2><strong>\u8bbe\u7f6e\u89c6\u9891\u7f16\u89e3\u7801\u5668<\/strong><\/h2>\n<p>fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u521b\u5efa\u89c6\u9891\u5199\u5165\u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528OpenCV\u7684<code>VideoWriter<\/code>\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u89c6\u9891\u5199\u5165\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u89c6\u9891\u5199\u5165\u5bf9\u8c61<\/p>\n<p>out = cv2.VideoWriter(output_file, fourcc, frame_rate, (frame_width, frame_height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u751f\u6210\u89c6\u9891\u5e27\u5e76\u5199\u5165\u89c6\u9891\u6587\u4ef6\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u4f60\u53ef\u4ee5\u4ece\u5404\u79cd\u6765\u6e90\u751f\u6210\u89c6\u9891\u5e27\uff0c\u4f8b\u5982\u4ece\u6444\u50cf\u5934\u6355\u83b7\u3001\u4ece\u56fe\u50cf\u5e8f\u5217\u751f\u6210\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u4f7f\u7528NumPy\u751f\u6210\u4e00\u4e9b\u968f\u673a\u989c\u8272\u7684\u5e27\uff0c\u5e76\u5c06\u5176\u5199\u5165\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u5e76\u5199\u5165\u89c6\u9891\u5e27<\/p>\n<p>for i in range(300):  # \u521b\u5efa10\u79d2\u7684\u89c6\u9891\uff08300\u5e27\uff09<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u989c\u8272\u7684\u5e27<\/p>\n<p>    frame = np.random.randint(0, 256, (frame_height, frame_width, 3), dtype=np.uint8)<\/p>\n<p>    # \u5c06\u5e27\u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>    out.write(frame)<\/p>\n<h2><strong>\u91ca\u653e\u89c6\u9891\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>out.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u4ece\u56fe\u50cf\u5e8f\u5217\u521b\u5efa\u89c6\u9891\u6587\u4ef6\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u6709\u4e00\u7cfb\u5217\u56fe\u50cf\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u56fe\u50cf\u5408\u5e76\u6210\u4e00\u4e2a\u89c6\u9891\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u4f7f\u7528Pillow\u52a0\u8f7d\u56fe\u50cf\u5e76\u5c06\u5176\u5199\u5165\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u56fe\u50cf\u5e8f\u5217\u521b\u5efa\u89c6\u9891\u6587\u4ef6<\/p>\n<p>image_files = [&#39;image1.png&#39;, &#39;image2.png&#39;, &#39;image3.png&#39;, ...]  # \u66ff\u6362\u4e3a\u4f60\u7684\u56fe\u50cf\u6587\u4ef6\u5217\u8868<\/p>\n<h2><strong>\u521b\u5efa\u89c6\u9891\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>out = cv2.VideoWriter(output_file, fourcc, frame_rate, (frame_width, frame_height))<\/p>\n<p>for image_file in image_files:<\/p>\n<p>    # \u52a0\u8f7d\u56fe\u50cf<\/p>\n<p>    image = Image.open(image_file)<\/p>\n<p>    # \u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/p>\n<p>    frame = np.array(image)<\/p>\n<p>    # \u786e\u4fdd\u56fe\u50cf\u5927\u5c0f\u4e0e\u89c6\u9891\u5e27\u5927\u5c0f\u4e00\u81f4<\/p>\n<p>    frame = cv2.resize(frame, (frame_width, frame_height))<\/p>\n<p>    # \u5c06\u5e27\u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>    out.write(frame)<\/p>\n<h2><strong>\u91ca\u653e\u89c6\u9891\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>out.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6355\u83b7\u6444\u50cf\u5934\u89c6\u9891\u5e76\u4fdd\u5b58<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528OpenCV\u6355\u83b7\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6444\u50cf\u5934<\/p>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<h2><strong>\u68c0\u67e5\u6444\u50cf\u5934\u662f\u5426\u6210\u529f\u6253\u5f00<\/strong><\/h2>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>    exit()<\/p>\n<h2><strong>\u83b7\u53d6\u6444\u50cf\u5934\u7684\u5e27\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/strong><\/h2>\n<p>frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))<\/p>\n<p>frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))<\/p>\n<h2><strong>\u521b\u5efa\u89c6\u9891\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>out = cv2.VideoWriter(output_file, fourcc, frame_rate, (frame_width, frame_height))<\/p>\n<p>while True:<\/p>\n<p>    # \u6355\u83b7\u5e27<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    # \u68c0\u67e5\u662f\u5426\u6210\u529f\u6355\u83b7\u5e27<\/p>\n<p>    if not ret:<\/p>\n<p>        print(&quot;\u65e0\u6cd5\u6355\u83b7\u89c6\u9891\u5e27&quot;)<\/p>\n<p>        break<\/p>\n<p>    # \u5c06\u5e27\u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>    out.write(frame)<\/p>\n<p>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;Frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b&#39;Q&#39;\u952e\u9000\u51fa\u5faa\u73af<\/p>\n<p>    if cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u6444\u50cf\u5934\u548c\u89c6\u9891\u5199\u5165\u5bf9\u8c61<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528OpenCV\u3001NumPy\u548cPillow\u7b49\u5e93\u6765\u521b\u5efa\u89c6\u9891\u6587\u4ef6\u3002<strong>OpenCV\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5904\u7406\u89c6\u9891\u6d41\u3001\u6355\u83b7\u6444\u50cf\u5934\u89c6\u9891\u3001\u751f\u6210\u89c6\u9891\u5e27\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/strong>\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u89c6\u9891\u6570\u636e\uff0c\u5e76\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u89c6\u9891\u6587\u4ef6\uff0c\u4f8b\u5982\u4ece\u6444\u50cf\u5934\u6355\u83b7\u7684\u89c6\u9891\u3001\u4ece\u56fe\u50cf\u5e8f\u5217\u751f\u6210\u7684\u89c6\u9891\u7b49\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u4ee3\u7801\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u89c6\u9891\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u89c6\u9891\u6587\u4ef6\u901a\u5e38\u4f7f\u7528OpenCV\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5OpenCV\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install opencv-python<\/code>\u6765\u5b9e\u73b0\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.VideoWriter<\/code>\u7c7b\u6765\u5b9a\u4e49\u89c6\u9891\u7684\u53c2\u6570\uff0c\u5305\u62ec\u89c6\u9891\u7684\u6587\u4ef6\u540d\u3001\u7f16\u7801\u683c\u5f0f\u3001\u5e27\u7387\u548c\u89c6\u9891\u5c3a\u5bf8\u3002\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e00\u5e27\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u5199\u5165\u89c6\u9891\u6587\u4ef6\u4e2d\uff0c\u6700\u7ec8\u60a8\u5c31\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<p><strong>Python\u4e2d\u652f\u6301\u54ea\u4e9b\u89c6\u9891\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>Python\u7684OpenCV\u5e93\u652f\u6301\u591a\u79cd\u89c6\u9891\u7f16\u7801\u683c\u5f0f\uff0c\u5982MJPG\u3001XVID\u3001H264\u7b49\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\u6765\u538b\u7f29\u89c6\u9891\u6587\u4ef6\u3002\u7f16\u7801\u683c\u5f0f\u7684\u9009\u62e9\u4f1a\u5f71\u54cd\u89c6\u9891\u6587\u4ef6\u7684\u5927\u5c0f\u548c\u64ad\u653e\u6548\u679c\uff0c\u56e0\u6b64\u5728\u521b\u5efa\u89c6\u9891\u65f6\u8981\u6839\u636e\u76ee\u6807\u5e73\u53f0\u548c\u8bbe\u5907\u7684\u517c\u5bb9\u6027\u6765\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u89c6\u9891\u4e2d\u7684\u97f3\u9891\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u521b\u5efa\u7684\u89c6\u9891\u4e2d\u6dfb\u52a0\u97f3\u9891\uff0cOpenCV\u672c\u8eab\u5e76\u4e0d\u652f\u6301\u97f3\u9891\u5904\u7406\u3002\u60a8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u5e93\uff0c\u5982MoviePy\u6216FFmpeg\uff0c\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u89c6\u9891\u548c\u97f3\u9891\u7684\u5408\u6210\u3002\u5728\u751f\u6210\u89c6\u9891\u540e\uff0c\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5c06\u97f3\u9891\u8f68\u9053\u4e0e\u89c6\u9891\u6587\u4ef6\u5408\u5e76\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u97f3\u9891\u7684\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5efa\u7acb\u89c6\u9891\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3001NumPy\u5e93\u3001Pillow\u5e93\u7b49\u5de5\u5177\uff0c\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60 [&hellip;]","protected":false},"author":3,"featured_media":1169939,"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\/1169936"}],"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=1169936"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169936\/revisions"}],"predecessor-version":[{"id":1169942,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169936\/revisions\/1169942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169939"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}