{"id":1150259,"date":"2025-01-13T16:59:33","date_gmt":"2025-01-13T08:59:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150259.html"},"modified":"2025-01-13T16:59:35","modified_gmt":"2025-01-13T08:59:35","slug":"python-opencv%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e5%9b%be%e7%89%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150259.html","title":{"rendered":"python opencv\u5982\u4f55\u4fdd\u5b58\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180748\/d5f1f532-6745-468d-be3b-1521d3d33a8c.webp\" alt=\"python opencv\u5982\u4f55\u4fdd\u5b58\u56fe\u7247\" \/><\/p>\n<p><p> \u4fdd\u5b58\u56fe\u7247\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u56fe\u50cf\u5904\u7406\u9886\u57df\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f7f\u7528Python\u7684OpenCV\u5e93\uff08cv2\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u5730\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\u3002<strong>Python OpenCV\u4fdd\u5b58\u56fe\u7247\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1acv2.imwrite()\u51fd\u6570\u3001\u4fdd\u5b58\u56fe\u50cf\u65f6\u8bbe\u7f6e\u538b\u7f29\u53c2\u6570\u3001\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>cv2.imwrite()\u51fd\u6570<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>cv2.imwrite()\u51fd\u6570<\/strong>\u662fOpenCV\u4e2d\u7528\u4e8e\u4fdd\u5b58\u56fe\u50cf\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u8be5\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cv2.imwrite(filename, img[, params])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>filename<\/code> \u662f\u4fdd\u5b58\u56fe\u50cf\u7684\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002<\/li>\n<li><code>img<\/code> \u662f\u9700\u8981\u4fdd\u5b58\u7684\u56fe\u50cf\u6570\u7ec4\u3002<\/li>\n<li><code>params<\/code> \u662f\u4fdd\u5b58\u56fe\u50cf\u65f6\u7684\u53ef\u9009\u53c2\u6570\uff0c\u5982JPEG\u8d28\u91cf\u6216PNG\u538b\u7f29\u7ea7\u522b\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;saved_image.jpg&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001cv2.imwrite()\u51fd\u6570\u8be6\u89e3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528cv2.imwrite()\u51fd\u6570\u4fdd\u5b58\u56fe\u50cf\u662fOpenCV\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8be5\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u4fdd\u5b58\u56fe\u50cf\uff0c\u8fd8\u53ef\u4ee5\u6307\u5b9a\u56fe\u50cf\u7684\u4fdd\u5b58\u683c\u5f0f\u548c\u538b\u7f29\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5982\u4e0b\u6240\u793a\uff0c\u901a\u8fc7cv2.imread()\u8bfb\u53d6\u56fe\u50cf\uff0c\u7136\u540e\u4f7f\u7528cv2.imwrite()\u4fdd\u5b58\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;saved_image.jpg&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u8bfb\u53d6\u5230\u53d8\u91cf<code>img<\/code>\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>cv2.imwrite()<\/code>\u5c06\u5176\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>saved_image.jpg<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4fdd\u5b58\u56fe\u50cf\u65f6\u8bbe\u7f6e\u538b\u7f29\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u538b\u7f29\u53c2\u6570\u6765\u63a7\u5236\u56fe\u50cf\u8d28\u91cf\u3002\u5bf9\u4e8eJPEG\u683c\u5f0f\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u538b\u7f29\u8d28\u91cf\uff1b\u5bf9\u4e8ePNG\u683c\u5f0f\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u538b\u7f29\u7ea7\u522b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6eJPEG\u8d28\u91cf\uff080-100\uff09\uff0c\u503c\u8d8a\u9ad8\u8d28\u91cf\u8d8a\u597d\uff0c\u6587\u4ef6\u8d8a\u5927<\/strong><\/h2>\n<p>cv2.imwrite(&#39;saved_image_quality_90.jpg&#39;, img, [int(cv2.IMWRITE_JPEG_QUALITY), 90])<\/p>\n<h2><strong>\u8bbe\u7f6ePNG\u538b\u7f29\u7ea7\u522b\uff080-9\uff09\uff0c\u503c\u8d8a\u4f4e\u8d28\u91cf\u8d8a\u597d\uff0c\u6587\u4ef6\u8d8a\u5927<\/strong><\/h2>\n<p>cv2.imwrite(&#39;saved_image_compression_3.png&#39;, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 3])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u8bbe\u7f6e\u4e86JPEG\u56fe\u50cf\u7684\u8d28\u91cf\u548cPNG\u56fe\u50cf\u7684\u538b\u7f29\u7ea7\u522b\u3002JPEG\u8d28\u91cf\u8d8a\u9ad8\uff0c\u56fe\u50cf\u8d28\u91cf\u8d8a\u597d\uff0c\u4f46\u6587\u4ef6\u5927\u5c0f\u4e5f\u8d8a\u5927\uff1bPNG\u538b\u7f29\u7ea7\u522b\u8d8a\u4f4e\uff0c\u56fe\u50cf\u8d28\u91cf\u8d8a\u597d\uff0c\u4f46\u6587\u4ef6\u5927\u5c0f\u4e5f\u8d8a\u5927\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>cv2.imwrite()\u51fd\u6570\u53ef\u4ee5\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\uff0c\u4f8b\u5982JPG\u3001PNG\u3001BMP\u3001TIFF\u7b49\u3002\u53ea\u9700\u4fee\u6539\u6587\u4ef6\u540d\u7684\u540e\u7f00\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aBMP\u683c\u5f0f<\/strong><\/h2>\n<p>cv2.imwrite(&#39;saved_image.bmp&#39;, img)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aTIFF\u683c\u5f0f<\/strong><\/h2>\n<p>cv2.imwrite(&#39;saved_image.tiff&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u5206\u522b\u4fdd\u5b58\u4e3aBMP\u548cTIFF\u683c\u5f0f\u3002\u53ea\u9700\u4fee\u6539\u6587\u4ef6\u540d\u7684\u540e\u7f00\uff0ccv2.imwrite()\u51fd\u6570\u4f1a\u81ea\u52a8\u8bc6\u522b\u5e76\u4fdd\u5b58\u4e3a\u76f8\u5e94\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u56fe\u50cf\u4fdd\u5b58\u7684\u5e38\u89c1\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528cv2.imwrite()\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4fdd\u5b58\u8def\u5f84\u4e0d\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6307\u5b9a\u7684\u4fdd\u5b58\u8def\u5f84\u4e0d\u5b58\u5728\uff0ccv2.imwrite()\u51fd\u6570\u4f1a\u8fd4\u56deFalse\uff0c\u8868\u793a\u4fdd\u5b58\u5931\u8d25\u3002\u786e\u4fdd\u4fdd\u5b58\u8def\u5f84\u5b58\u5728\u6216\u4f7f\u7528os.makedirs()\u51fd\u6570\u521b\u5efa\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import os<\/p>\n<h2><strong>\u521b\u5efa\u4fdd\u5b58\u8def\u5f84<\/strong><\/h2>\n<p>save_path = &#39;images\/saved_image.jpg&#39;<\/p>\n<p>save_dir = os.path.dirname(save_path)<\/p>\n<p>if not os.path.exists(save_dir):<\/p>\n<p>    os.makedirs(save_dir)<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>if cv2.imwrite(save_path, img):<\/p>\n<p>    print(f&quot;Image saved to {save_path}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to save image&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.makedirs()\u51fd\u6570\u521b\u5efa\u4fdd\u5b58\u8def\u5f84\uff0c\u7136\u540e\u518d\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u56fe\u50cf\u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e<\/h4>\n<\/p>\n<p><p>cv2.imwrite()\u51fd\u6570\u9700\u8981\u8f93\u5165\u56fe\u50cf\u6570\u636e\u4e3anumpy\u6570\u7ec4\u3002\u5982\u679c\u56fe\u50cf\u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u4fdd\u5b58\u56fe\u50cf\u4f1a\u5931\u8d25\u3002\u786e\u4fdd\u8f93\u5165\u7684\u56fe\u50cf\u6570\u636e\u4e3anumpy\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf<\/strong><\/h2>\n<p>img = np.zeros((100, 100, 3), dtype=np.uint8)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;blank_image.jpg&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>blank_image.jpg<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u56fe\u50cf\u4fdd\u5b58\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u540e\u518d\u4fdd\u5b58\uff0c\u6216\u8005\u6279\u91cf\u4fdd\u5b58\u591a\u4e2a\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u9ad8\u7ea7\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u56fe\u50cf\u540e\u518d\u4fdd\u5b58<\/h4>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u56fe\u50cf\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u5904\u7406\uff0c\u5982\u7070\u5ea6\u5316\u3001\u7f29\u653e\u3001\u65cb\u8f6c\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u7f29\u653e\u56fe\u50cf<\/strong><\/h2>\n<p>resized_img = cv2.resize(img, (200, 200))<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>M = cv2.getRotationMatrix2D((img.shape[1] \/ 2, img.shape[0] \/ 2), 45, 1)<\/p>\n<p>rotated_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;gray_image.jpg&#39;, gray_img)<\/p>\n<p>cv2.imwrite(&#39;resized_image.jpg&#39;, resized_img)<\/p>\n<p>cv2.imwrite(&#39;rotated_image.jpg&#39;, rotated_img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3001\u7f29\u653e\u56fe\u50cf\u548c\u65cb\u8f6c\u56fe\u50cf\uff0c\u5e76\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u4fdd\u5b58\u591a\u4e2a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u5728\u6279\u91cf\u5904\u7406\u56fe\u50cf\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4fdd\u5b58\u591a\u4e2a\u56fe\u50cf\u3002\u4f8b\u5982\uff0c\u904d\u5386\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u540e\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import os<\/p>\n<h2><strong>\u8f93\u5165\u56fe\u50cf\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>input_dir = &#39;input_images&#39;<\/p>\n<h2><strong>\u8f93\u51fa\u56fe\u50cf\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>output_dir = &#39;output_images&#39;<\/p>\n<p>if not os.path.exists(output_dir):<\/p>\n<p>    os.makedirs(output_dir)<\/p>\n<h2><strong>\u904d\u5386\u8f93\u5165\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf<\/strong><\/h2>\n<p>for filename in os.listdir(input_dir):<\/p>\n<p>    if filename.endswith(&#39;.jpg&#39;) or filename.endswith(&#39;.png&#39;):<\/p>\n<p>        # \u8bfb\u53d6\u56fe\u50cf<\/p>\n<p>        img = cv2.imread(os.path.join(input_dir, filename))<\/p>\n<p>        # \u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<\/p>\n<p>        # \u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/p>\n<p>        output_path = os.path.join(output_dir, filename)<\/p>\n<p>        cv2.imwrite(output_path, gray_img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u8f93\u5165\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\uff0c\u5c06\u6bcf\u4e2a\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff0c\u5e76\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u5230\u8f93\u51fa\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u56fe\u50cf\u4fdd\u5b58\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u56fe\u50cf\u65f6\uff0c\u56fe\u50cf\u4fdd\u5b58\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u6027\u80fd\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u6279\u91cf\u5904\u7406\u56fe\u50cf\u65f6\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import os<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def process_and_save_image(filename):<\/p>\n<p>    # \u8bfb\u53d6\u56fe\u50cf<\/p>\n<p>    img = cv2.imread(os.path.join(input_dir, filename))<\/p>\n<p>    # \u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/p>\n<p>    output_path = os.path.join(output_dir, filename)<\/p>\n<p>    cv2.imwrite(output_path, gray_img)<\/p>\n<h2><strong>\u8f93\u5165\u56fe\u50cf\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>input_dir = &#39;input_images&#39;<\/p>\n<h2><strong>\u8f93\u51fa\u56fe\u50cf\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>output_dir = &#39;output_images&#39;<\/p>\n<p>if not os.path.exists(output_dir):<\/p>\n<p>    os.makedirs(output_dir)<\/p>\n<h2><strong>\u4f7f\u7528ThreadPoolExecutor\u8fdb\u884c\u591a\u7ebf\u7a0b\u5904\u7406<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    for filename in os.listdir(input_dir):<\/p>\n<p>        if filename.endswith(&#39;.jpg&#39;) or filename.endswith(&#39;.png&#39;):<\/p>\n<p>            executor.submit(process_and_save_image, filename)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ThreadPoolExecutor\u8fdb\u884c\u591a\u7ebf\u7a0b\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u6279\u91cf\u5904\u7406\u56fe\u50cf\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u51cf\u5c11I\/O\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u56fe\u50cf\u65f6\uff0cI\/O\u64cd\u4f5c\uff08\u5982\u8bfb\u53d6\u548c\u4fdd\u5b58\u56fe\u50cf\uff09\u901a\u5e38\u662f\u6027\u80fd\u74f6\u9888\u3002\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684I\/O\u64cd\u4f5c\uff0c\u4f8b\u5982\u907f\u514d\u91cd\u590d\u8bfb\u53d6\u548c\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f18\u5316\u56fe\u50cf\u5904\u7406\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u56fe\u50cf\u65f6\uff0c\u9009\u62e9\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528OpenCV\u63d0\u4f9b\u7684\u9ad8\u6548\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u7f16\u5199\u4f4e\u6548\u7684\u56fe\u50cf\u5904\u7406\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u56fe\u50cf\u4fdd\u5b58\u7684\u5e38\u89c1\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u4fdd\u5b58\u7684\u683c\u5f0f\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u683c\u5f0f\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u4fdd\u5b58\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001JPEG<\/h4>\n<\/p>\n<p><p>JPEG\uff08Joint Photographic Experts Group\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6709\u635f\u538b\u7f29\u56fe\u50cf\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u7167\u7247\u548c\u590d\u6742\u56fe\u50cf\u3002JPEG\u56fe\u50cf\u7684\u6587\u4ef6\u5927\u5c0f\u8f83\u5c0f\uff0c\u4f46\u7531\u4e8e\u662f\u6709\u635f\u538b\u7f29\uff0c\u56fe\u50cf\u8d28\u91cf\u4f1a\u6709\u6240\u4e0b\u964d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6587\u4ef6\u5927\u5c0f\u8f83\u5c0f<\/li>\n<li>\u5e7f\u6cdb\u652f\u6301<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6709\u635f\u538b\u7f29\uff0c\u56fe\u50cf\u8d28\u91cf\u4e0b\u964d<\/li>\n<li>\u4e0d\u652f\u6301\u900f\u660e\u901a\u9053<\/li>\n<\/ul>\n<p><h4>2\u3001PNG<\/h4>\n<\/p>\n<p><p>PNG\uff08Portable Network Graphics\uff09\u662f\u4e00\u79cd\u65e0\u635f\u538b\u7f29\u56fe\u50cf\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u56fe\u50cf\u8d28\u91cf\u8981\u6c42\u8f83\u9ad8\u7684\u56fe\u50cf\u3002PNG\u56fe\u50cf\u652f\u6301\u900f\u660e\u901a\u9053\uff0c\u4f46\u6587\u4ef6\u5927\u5c0f\u8f83\u5927\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u65e0\u635f\u538b\u7f29\uff0c\u56fe\u50cf\u8d28\u91cf\u9ad8<\/li>\n<li>\u652f\u6301\u900f\u660e\u901a\u9053<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6587\u4ef6\u5927\u5c0f\u8f83\u5927<\/li>\n<li>\u538b\u7f29\u901f\u5ea6\u8f83\u6162<\/li>\n<\/ul>\n<p><h4>3\u3001BMP<\/h4>\n<\/p>\n<p><p>BMP\uff08Bitmap\uff09\u662f\u4e00\u79cd\u65e0\u538b\u7f29\u6216\u7b80\u5355\u538b\u7f29\u7684\u56fe\u50cf\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u56fe\u50cf\u8d28\u91cf\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\u3002BMP\u56fe\u50cf\u6587\u4ef6\u5927\u5c0f\u8f83\u5927\uff0c\u4f46\u56fe\u50cf\u8d28\u91cf\u65e0\u635f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u56fe\u50cf\u8d28\u91cf\u65e0\u635f<\/li>\n<li>\u7b80\u5355\u6613\u7528<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6587\u4ef6\u5927\u5c0f\u6781\u5927<\/li>\n<li>\u4e0d\u652f\u6301\u900f\u660e\u901a\u9053<\/li>\n<\/ul>\n<p><h4>4\u3001TIFF<\/h4>\n<\/p>\n<p><p>TIFF\uff08Tagged Image File Format\uff09\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u56fe\u50cf\u683c\u5f0f\uff0c\u652f\u6301\u591a\u79cd\u538b\u7f29\u65b9\u5f0f\uff0c\u5305\u62ec\u65e0\u635f\u538b\u7f29\u548c\u6709\u635f\u538b\u7f29\u3002TIFF\u683c\u5f0f\u9002\u7528\u4e8e\u5b58\u50a8\u9ad8\u8d28\u91cf\u56fe\u50cf\u548c\u591a\u9875\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u591a\u79cd\u538b\u7f29\u65b9\u5f0f<\/li>\n<li>\u652f\u6301\u591a\u9875\u56fe\u50cf<\/li>\n<li>\u9ad8\u8d28\u91cf\u56fe\u50cf<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6587\u4ef6\u5927\u5c0f\u8f83\u5927<\/li>\n<li>\u4e0d\u5e7f\u6cdb\u652f\u6301<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u9664\u4e86OpenCV\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58\u56fe\u50cf\uff0c\u4f8b\u5982Pillow\uff08PIL\uff09\u3001scikit-image\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pillow\u548cscikit-image\u4fdd\u5b58\u56fe\u50cf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>Pillow\uff08PIL\uff09\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u4fdd\u5b58\u3002\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u50cf\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>img.save(&#39;saved_image.jpg&#39;, quality=90)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pillow\u8bfb\u53d6\u56fe\u50cf\u5e76\u4fdd\u5b58\u56fe\u50cf\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>quality<\/code>\u53c2\u6570\u6765\u63a7\u5236JPEG\u56fe\u50cf\u7684\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528scikit-image\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>scikit-image\u662f\u4e00\u4e2a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4f7f\u7528scikit-image\u4fdd\u5b58\u56fe\u50cf\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = io.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>io.imsave(&#39;saved_image.jpg&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528scikit-image\u8bfb\u53d6\u56fe\u50cf\u5e76\u4fdd\u5b58\u56fe\u50cf\u3002scikit-image\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u56fe\u50cf\u8bfb\u53d6\u548c\u4fdd\u5b58\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684OpenCV\u5e93\u4fdd\u5b58\u56fe\u50cf\uff0c\u5305\u62ec\u57fa\u672c\u4f7f\u7528\u3001\u8bbe\u7f6e\u538b\u7f29\u53c2\u6570\u3001\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\u3001\u5e38\u89c1\u95ee\u9898\u3001\u6027\u80fd\u4f18\u5316\u3001\u5e38\u89c1\u56fe\u50cf\u683c\u5f0f\u4ee5\u53ca\u5176\u4ed6\u5e93\u7684\u4f7f\u7528\u3002<strong>\u4f7f\u7528cv2.imwrite()\u51fd\u6570\u4fdd\u5b58\u56fe\u50cf\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5<\/strong>\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u538b\u7f29\u53c2\u6570\u548c\u4fdd\u5b58\u683c\u5f0f\u3002<strong>\u5904\u7406\u56fe\u50cf\u548c\u6279\u91cf\u4fdd\u5b58\u56fe\u50cf\u662f\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u5e38\u89c1\u9700\u6c42<\/strong>\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u548c\u51cf\u5c11I\/O\u64cd\u4f5c\u6765\u4f18\u5316\u6027\u80fd\u3002\u9664\u4e86OpenCV\uff0c<strong>\u8fd8\u53ef\u4ee5\u4f7f\u7528Pillow\u548cscikit-image\u7b49\u5176\u4ed6\u5e93\u4fdd\u5b58\u56fe\u50cf<\/strong>\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u683c\u5f0f\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u56fe\u50cf\u4fdd\u5b58\u65b9\u9762\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python OpenCV\u4fdd\u5b58\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6307\u5b9a\u8def\u5f84\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d6\u56fe\u50cf\nimage = cv2.imread(&#39;input_image.jpg&#39;)\n\n# \u4fdd\u5b58\u56fe\u50cf\ncv2.imwrite(&#39;output_image.jpg&#39;, image)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u8bfb\u53d6\u4e86\u4e00\u5f20\u540d\u4e3a&quot;input_image.jpg&quot;\u7684\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a&quot;output_image.jpg&quot;\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u6587\u4ef6\u540d\u548c\u8def\u5f84\u3002<\/p>\n<p><strong>\u4fdd\u5b58\u56fe\u50cf\u65f6\u53ef\u4ee5\u9009\u62e9\u54ea\u4e9b\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>OpenCV\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u4fdd\u5b58\uff0c\u5305\u62ecJPEG\u3001PNG\u3001BMP\u7b49\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>.jpg<\/code>\u540e\u7f00\u4fdd\u5b58\u4e3aJPEG\u683c\u5f0f\uff0c\u4f7f\u7528<code>.png<\/code>\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u3002\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\u5728\u8d28\u91cf\u548c\u538b\u7f29\u7387\u4e0a\u6709\u6240\u4e0d\u540c\uff0c\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u53ef\u4ee5\u786e\u4fdd\u56fe\u50cf\u8d28\u91cf\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\u8c03\u6574\u5176\u8d28\u91cf\uff1f<\/strong><br \/>\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u6765\u8c03\u6574\u56fe\u50cf\u7684\u8d28\u91cf\u3002\u4f8b\u5982\uff0c\u5728\u4fdd\u5b58JPEG\u683c\u5f0f\u7684\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u56fe\u50cf\u7684\u8d28\u91cf\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">cv2.imwrite(&#39;output_image.jpg&#39;, image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8d28\u91cf\u53c2\u6570\u8bbe\u7f6e\u4e3a90\uff0c\u8303\u56f4\u901a\u5e38\u57280\u5230100\u4e4b\u95f4\uff0c\u6570\u5b57\u8d8a\u5927\u8868\u793a\u8d28\u91cf\u8d8a\u9ad8\uff0c\u6587\u4ef6\u5927\u5c0f\u4e5f\u968f\u4e4b\u589e\u52a0\u3002\u901a\u8fc7\u8c03\u6574\u6b64\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u5728\u56fe\u50cf\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u4e4b\u95f4\u627e\u5230\u5e73\u8861\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4fdd\u5b58\u56fe\u7247\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u56fe\u50cf\u5904\u7406\u9886\u57df\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f7f\u7528Python\u7684OpenCV\u5e93\uff08cv2\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u8f7b [&hellip;]","protected":false},"author":3,"featured_media":1150261,"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\/1150259"}],"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=1150259"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150259\/revisions"}],"predecessor-version":[{"id":1150262,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150259\/revisions\/1150262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150261"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}