{"id":934685,"date":"2024-12-26T18:39:32","date_gmt":"2024-12-26T10:39:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934685.html"},"modified":"2024-12-26T18:39:35","modified_gmt":"2024-12-26T10:39:35","slug":"python-%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934685.html","title":{"rendered":"python \u5982\u4f55\u4fdd\u5b58\u56fe\u50cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071602\/ac985f48-cc85-4f00-a6fa-cb5dd9b62d77.webp\" alt=\"python \u5982\u4f55\u4fdd\u5b58\u56fe\u50cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u50cf\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u3002<\/strong>\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0cPIL\u5e93\uff08\u73b0\u4e3aPillow\uff09\u662fPython\u4e2d\u5904\u7406\u56fe\u50cf\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u8bfb\u5199\uff1bOpenCV\u662f\u4e00\u4e2a\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff1bMatplotlib\u5219\u66f4\u591a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u573a\u666f\uff0c\u4f46\u540c\u6837\u5177\u5907\u4fdd\u5b58\u56fe\u50cf\u7684\u529f\u80fd\u3002<strong>\u5176\u4e2d\uff0cPIL\u5e93\u56e0\u5176\u7b80\u5355\u6613\u7528\u4e14\u529f\u80fd\u5168\u9762\uff0c\u662f\u4fdd\u5b58\u56fe\u50cf\u7684\u5e38\u7528\u9009\u62e9\u3002<\/strong>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4fdd\u5b58\u56fe\u50cf\uff0c\u4ee5\u53ca\u5b83\u4eec\u5404\u81ea\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIL\u5e93\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662fPython\u7684\u4e00\u4e2a\u8457\u540d\u56fe\u50cf\u5904\u7406\u5e93\uff0cPillow\u662f\u5b83\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u589e\u52a0\u4e86\u5bf9Python 3\u7684\u652f\u6301\u3002\u5728PIL\u4e2d\uff0c\u4fdd\u5b58\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u8c03\u7528<code>Image<\/code>\u5bf9\u8c61\u7684<code>save<\/code>\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u5e93<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4fdd\u5b58\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u50cf\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u5230\u65b0\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>img.save(&#39;new_example.png&#39;, &#39;PNG&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>Image.open<\/code>\u65b9\u6cd5\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3a\u65b0\u7684\u6587\u4ef6\u3002<code>save<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u56fe\u50cf\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>Pillow\u7684\u7279\u70b9<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f<\/strong>\uff1aPillow\u652f\u6301JPEG\u3001PNG\u3001BMP\u3001GIF\u7b49\u591a\u79cd\u683c\u5f0f\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u56fe\u50cf\u5904\u7406\u9700\u6c42\u3002<\/li>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u63d0\u4f9b\u4e86\u76f4\u89c2\u7684API\uff0c\u6613\u4e8e\u4e0a\u624b\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u3002<\/li>\n<li><strong>\u529f\u80fd\u5168\u9762<\/strong>\uff1a\u9664\u4e86\u4fdd\u5b58\u56fe\u50cf\uff0c\u8fd8\u652f\u6301\u56fe\u50cf\u7684\u7f29\u653e\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\u591a\u79cd\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u4f7f\u7528OpenCV\u5e93\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u529f\u80fd\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u8fd8\u652f\u6301\u89c6\u9891\u5904\u7406\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5OpenCV\u5e93<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4fdd\u5b58\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u5230\u65b0\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>cv2.imwrite(&#39;new_example.png&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.imread<\/code>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>cv2.imwrite<\/code>\u5c06\u5176\u4fdd\u5b58\u5230\u65b0\u7684\u6587\u4ef6\u4e2d\u3002<code>imwrite<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>OpenCV\u7684\u7279\u70b9<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f<\/strong>\uff1a\u4e0ePillow\u76f8\u4f3c\uff0cOpenCV\u4e5f\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u3002<\/li>\n<li><strong>\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u529f\u80fd\uff0c\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\uff1a\u652f\u6301Windows\u3001Linux\u3001macOS\u7b49\u591a\u79cd\u5e73\u53f0\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528Matplotlib\u5e93\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u7136\u800c\uff0c\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Matplotlib\u5e93<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4fdd\u5b58\u56fe\u50cf<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Matplotlib\u4fdd\u5b58\u56fe\u50cf\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<h2><strong>\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>img = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u5230\u65b0\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>plt.imsave(&#39;new_example.png&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>mpimg.imread<\/code>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>plt.imsave<\/code>\u5c06\u5176\u4fdd\u5b58\u5230\u65b0\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>Matplotlib\u7684\u7279\u70b9<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u529f\u80fd<\/strong>\uff1a\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u7ed8\u56fe\uff0c\u4f46\u4e5f\u652f\u6301\u56fe\u50cf\u8bfb\u5199\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f<\/strong>\uff1a\u652f\u6301PNG\u3001JPEG\u7b49\u5e38\u89c1\u56fe\u50cf\u683c\u5f0f\u3002<\/li>\n<li><strong>\u9002\u5408\u4e0e\u5176\u4ed6\u7ed8\u56fe\u529f\u80fd\u7ed3\u5408\u4f7f\u7528<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5728\u56fe\u50cf\u4e0a\u53e0\u52a0\u7ed8\u56fe\uff0cMatplotlib\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93\u8fdb\u884c\u56fe\u50cf\u4fdd\u5b58<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u5e93\u6765\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u8fdb\u884c\u9009\u62e9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5982\u679c\u53ea\u662f\u7b80\u5355\u5730\u8bfb\u53d6\u548c\u4fdd\u5b58\u56fe\u50cf\uff0cPillow\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9<\/strong>\u3002\u5b83\u7b80\u5355\u6613\u7528\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u3002<\/li>\n<li><strong>\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u6216\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0cOpenCV\u662f\u4e00\u4e2a\u66f4\u5408\u9002\u7684\u9009\u62e9<\/strong>\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/li>\n<li><strong>\u5982\u679c\u4f60\u9700\u8981\u5728\u56fe\u50cf\u4e0a\u53e0\u52a0\u7ed8\u56fe\u6216\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0cMatplotlib\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9<\/strong>\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u56fe\u50cf\u4fdd\u5b58\uff0c\u8fd8\u80fd\u8fdb\u884c\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u5728\u9879\u76ee\u4e2d\u6574\u5408\u56fe\u50cf\u4fdd\u5b58\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684\u9879\u76ee\u4e2d\uff0c\u56fe\u50cf\u4fdd\u5b58\u901a\u5e38\u662f\u56fe\u50cf\u5904\u7406\u6d41\u7a0b\u4e2d\u7684\u4e00\u4e2a\u73af\u8282\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5728\u9879\u76ee\u4e2d\u6574\u5408\u56fe\u50cf\u4fdd\u5b58\u529f\u80fd\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6a21\u5757\u5316\u8bbe\u8ba1<\/strong>\uff1a\u5c06\u56fe\u50cf\u4fdd\u5b58\u529f\u80fd\u5c01\u88c5\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u652f\u6301\u591a\u79cd\u683c\u5f0f<\/strong>\uff1a\u8003\u8651\u5230\u9879\u76ee\u9700\u6c42\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u4fdd\u5b58\uff0c\u4ee5\u63d0\u9ad8\u9002\u5e94\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u505a\u597d\u9519\u8bef\u5904\u7406\uff0c\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408\u5176\u4ed6\u56fe\u50cf\u5904\u7406\u529f\u80fd<\/strong>\uff1a\u5728\u4fdd\u5b58\u56fe\u50cf\u4e4b\u524d\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u5fc5\u8981\u7684\u56fe\u50cf\u5904\u7406\uff0c\u5982\u538b\u7f29\u3001\u8c03\u6574\u5c3a\u5bf8\u3001\u589e\u52a0\u6c34\u5370\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0\uff0c\u56fe\u50cf\u4fdd\u5b58\u529f\u80fd\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u53d1\u6325\u91cd\u8981\u4f5c\u7528\uff0c\u5e2e\u52a9\u5b9e\u73b0\u591a\u6837\u5316\u7684\u56fe\u50cf\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u4fdd\u5b58\u56fe\u50cf\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u4fdd\u5b58\u56fe\u50cf\uff0c\u4f8b\u5982PIL\uff08Pillow\uff09\u548cOpenCV\u3002\u4f7f\u7528Pillow\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u4fdd\u5b58\u56fe\u50cf\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00\u56fe\u50cf\nimg = Image.open(&#39;input_image.jpg&#39;)\n# \u4fdd\u5b58\u56fe\u50cf\nimg.save(&#39;output_image.png&#39;)  # \u53ef\u4ee5\u6307\u5b9a\u4fdd\u5b58\u683c\u5f0f\n<\/code><\/pre>\n<p>OpenCV\u7684\u7528\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d6\u56fe\u50cf\nimg = cv2.imread(&#39;input_image.jpg&#39;)\n# \u4fdd\u5b58\u56fe\u50cf\ncv2.imwrite(&#39;output_image.png&#39;, img)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u5728\u4fdd\u5b58\u65f6\u6307\u5b9a\u4e86\u6b63\u786e\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u56fe\u50cf\u4ee5\u4fbf\u4fdd\u5b58\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u56fe\u50cf\u4e4b\u524d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\uff0c\u6bd4\u5982\u8c03\u6574\u5927\u5c0f\u3001\u88c1\u526a\u6216\u5e94\u7528\u6ee4\u955c\u3002\u4f7f\u7528Pillow\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\uff1a  <\/p>\n<pre><code class=\"language-python\">img = Image.open(&#39;input_image.jpg&#39;)\n# \u8c03\u6574\u5927\u5c0f\nimg_resized = img.resize((800, 600))\n# \u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\nimg_resized.save(&#39;resized_image.png&#39;)\n<\/code><\/pre>\n<p>\u5bf9\u4e8eOpenCV\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">img = cv2.imread(&#39;input_image.jpg&#39;)\n# \u88c1\u526a\u56fe\u50cf\ncropped_img = img[50:200, 50:200]\n# \u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u50cf\ncv2.imwrite(&#39;cropped_image.png&#39;, cropped_img)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u5904\u7406\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u4fdd\u5b58\u7684\u56fe\u50cf\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4fdd\u5b58\u56fe\u50cf\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u6709\u51e0\u4e2a\u5173\u952e\u70b9\u9700\u8981\u8003\u8651\uff1a  <\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u683c\u5f0f<\/strong>\uff1a\u6839\u636e\u9700\u8981\u9009\u62e9\u9002\u5f53\u7684\u56fe\u50cf\u683c\u5f0f\uff08\u5982JPEG\u3001PNG\u3001BMP\u7b49\uff09\uff0c\u4e0d\u540c\u683c\u5f0f\u5728\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u4e0a\u6709\u6240\u4e0d\u540c\u3002<\/li>\n<li><strong>\u56fe\u50cf\u8d28\u91cf<\/strong>\uff1a\u5728\u4fdd\u5b58JPEG\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8d28\u91cf\u53c2\u6570\uff0c\u4ee5\u5e73\u8861\u56fe\u50cf\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u3002<\/li>\n<li><strong>\u6587\u4ef6\u8def\u5f84<\/strong>\uff1a\u786e\u4fdd\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u907f\u514d\u56e0\u8def\u5f84\u9519\u8bef\u5bfc\u81f4\u4fdd\u5b58\u5931\u8d25\u3002<\/li>\n<li><strong>\u5e93\u7248\u672c<\/strong>\uff1a\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684Pillow\u6216OpenCV\uff0c\u4ee5\u786e\u4fdd\u62e5\u6709\u6700\u65b0\u7684\u529f\u80fd\u548c\u4fee\u590d\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u5173\u6ce8\u8fd9\u4e9b\u7ec6\u8282\uff0c\u60a8\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u4f7f\u7528Python\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u50cf\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u3002\u8fd9\u51e0\u79cd\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":934691,"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\/934685"}],"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=934685"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934685\/revisions"}],"predecessor-version":[{"id":934692,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934685\/revisions\/934692"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/934691"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=934685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=934685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=934685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}