{"id":1119979,"date":"2025-01-08T18:50:18","date_gmt":"2025-01-08T10:50:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119979.html"},"modified":"2025-01-08T18:50:31","modified_gmt":"2025-01-08T10:50:31","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%a8%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0%e4%bf%9d%e5%ad%98%e5%9b%be%e7%89%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119979.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u7528\u4ee3\u7801\u5b9e\u73b0\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\/25082834\/09f3fec6-c96a-4180-8412-04f3f8fac6fa.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u7528\u4ee3\u7801\u5b9e\u73b0\u4fdd\u5b58\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u7247\uff0c\u4f8b\u5982\u4f7f\u7528Pillow\u5e93\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Pillow\u5e93<\/strong>\u662fPython Imaging Library (PIL) \u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c<strong>OpenCV\u5e93<\/strong>\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c<strong>Matplotlib\u5e93<\/strong>\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u5e76\u53ef\u4ee5\u4fdd\u5b58\u751f\u6210\u7684\u56fe\u8868\u6216\u56fe\u7247\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u4fdd\u5b58\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pillow\u5e93\u4fdd\u5b58\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Pillow\u5e93\u662fPIL\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u56fe\u50cf\u6587\u4ef6\u7684\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Pillow\u5e93\u4fdd\u5b58\u56fe\u7247\u7684\u793a\u4f8b\u4ee3\u7801\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>image = Image.open(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u8fdb\u884c\u4e00\u4e9b\u56fe\u50cf\u5904\u7406\uff08\u4f8b\u5982\uff1a\u65cb\u8f6c\u56fe\u50cf\uff09<\/strong><\/h2>\n<p>rotated_image = image.rotate(45)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image.save(&quot;saved_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u4e86Pillow\u5e93\uff0c\u7136\u540e\u6253\u5f00\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u8fdb\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV\u5e93\u4fdd\u5b58\u56fe\u7247<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u9886\u57df\u3002\u4e0b\u9762\u662f\u4f7f\u7528OpenCV\u5e93\u4fdd\u5b58\u56fe\u7247\u7684\u793a\u4f8b\u4ee3\u7801\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>image = cv2.imread(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u8fdb\u884c\u4e00\u4e9b\u56fe\u50cf\u5904\u7406\uff08\u4f8b\u5982\uff1a\u7070\u5ea6\u5904\u7406\uff09<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&quot;saved_image.jpg&quot;, gray_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u4e86OpenCV\u5e93\uff0c\u7136\u540e\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u8fdb\u884c\u7070\u5ea6\u5904\u7406\uff0c\u6700\u540e\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Matplotlib\u5e93\u4fdd\u5b58\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58\u56fe\u50cf\u3002\u4e0b\u9762\u662f\u4f7f\u7528Matplotlib\u5e93\u4fdd\u5b58\u56fe\u7247\u7684\u793a\u4f8b\u4ee3\u7801\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\u56fe\u50cf<\/strong><\/h2>\n<p>image = mpimg.imread(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u8fdb\u884c\u4e00\u4e9b\u56fe\u50cf\u5904\u7406\uff08\u4f8b\u5982\uff1a\u663e\u793a\u56fe\u50cf\uff09<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&quot;saved_image.png&quot;, bbox_inches=&#39;tight&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u4e86Matplotlib\u5e93\uff0c\u7136\u540e\u8bfb\u53d6\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u663e\u793a\u56fe\u50cf\uff0c\u6700\u540e\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u7247\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u51b3\u5b9a\u3002Pillow\u5e93\u9002\u5408\u5904\u7406\u9759\u6001\u56fe\u50cf\uff0cOpenCV\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0cMatplotlib\u5e93\u9002\u5408\u5728\u6570\u636e\u53ef\u89c6\u5316\u7684\u540c\u65f6\u4fdd\u5b58\u56fe\u50cf\u3002\u603b\u7684\u6765\u8bf4\uff0c<strong>Pillow\u5e93\u6613\u4e8e\u4f7f\u7528\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1bOpenCV\u5e93\u529f\u80fd\u5168\u9762\uff0c\u9002\u5408\u9ad8\u7ea7\u7528\u6237\uff1bMatplotlib\u5e93\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\u548c\u62a5\u544a\u751f\u6210<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528Pillow\u5e93\u7684\u4fdd\u5b58\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p><strong>Pillow\u5e93<\/strong>\u63d0\u4f9b\u4e86\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u7684\u529f\u80fd\uff0c\u5305\u62ec\u6253\u5f00\u3001\u4fdd\u5b58\u3001\u8f6c\u6362\u3001\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u4fdd\u5b58\u56fe\u50cf\uff0c\u5e76\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\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<p><h4>2\u3001\u6253\u5f00\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u6253\u5f00\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6765\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&quot;example.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u4fdd\u5b58\u56fe\u50cf\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528<code>save()<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>image.save(&quot;saved_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u56fe\u50cf\u5904\u7406\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Pillow\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/p>\n<p>resized_image = image.resize((800, 600))<\/p>\n<p>resized_image.save(&quot;resized_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>rotated_image = image.rotate(45)<\/p>\n<p>rotated_image.save(&quot;rotated_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u50cf<\/p>\n<p>cropped_image = image.crop((100, 100, 400, 400))<\/p>\n<p>cropped_image.save(&quot;cropped_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u8f6c\u6362\u56fe\u50cf\u6a21\u5f0f<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u56fe\u50cf\u6a21\u5f0f\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = image.convert(&quot;L&quot;)<\/p>\n<p>gray_image.save(&quot;gray_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u6dfb\u52a0\u6c34\u5370<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6c34\u5370<\/p>\n<p>watermark = Image.open(&quot;watermark.png&quot;)<\/p>\n<p>image.paste(watermark, (50, 50), watermark)<\/p>\n<p>image.save(&quot;watermarked_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u6253\u5f00\u56fe\u50cf\u3001\u8fdb\u884c\u4e00\u7cfb\u5217\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5e76\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageEnhance<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = image.resize((800, 600))<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = resized_image.rotate(45)<\/p>\n<h2><strong>\u8f6c\u6362\u56fe\u50cf\u6a21\u5f0f\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = rotated_image.convert(&quot;L&quot;)<\/p>\n<h2><strong>\u589e\u5f3a\u56fe\u50cf\u5bf9\u6bd4\u5ea6<\/strong><\/h2>\n<p>enhancer = ImageEnhance.Contrast(gray_image)<\/p>\n<p>enhanced_image = enhancer.enhance(2.0)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>enhanced_image.save(&quot;processed_image.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u6253\u5f00\u4e86\u4e00\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u4f9d\u6b21\u8fdb\u884c\u4e86\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u3001\u65cb\u8f6c\u56fe\u50cf\u3001\u8f6c\u6362\u56fe\u50cf\u6a21\u5f0f\u4e3a\u7070\u5ea6\u56fe\u50cf\u3001\u589e\u5f3a\u56fe\u50cf\u5bf9\u6bd4\u5ea6\u7b49\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\u7684\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51fa\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u4fdd\u5b58\u662f\u975e\u5e38\u65b9\u4fbf\u7684\u3002\u603b\u7684\u6765\u8bf4\uff0c<strong>\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u4fdd\u5b58\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u5e93\u3001\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u3001\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3001\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5e76\u7ec4\u5408\u4f7f\u7528\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5b8c\u6210\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5efa\u8bae\u5728\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u65f6\uff0c<strong>\u5c3d\u91cf\u4fdd\u6301\u56fe\u50cf\u7684\u539f\u59cb\u5206\u8fa8\u7387\u548c\u8d28\u91cf<\/strong>\uff0c\u4ee5\u786e\u4fdd\u56fe\u50cf\u7684\u6e05\u6670\u5ea6\u548c\u7ec6\u8282\u3002\u540c\u65f6\uff0c\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u53ef\u4ee5\u5c06\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u5c01\u88c5\u6210\u51fd\u6570\u6216\u7c7b\uff0c\u4fbf\u4e8e\u590d\u7528\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u4fdd\u5b58\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u4fdd\u5b58\u56fe\u7247\uff0c\u5982PIL\uff08Pillow\uff09\u3001OpenCV\u548cMatplotlib\u7b49\u3002\u4f7f\u7528Pillow\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u4fdd\u5b58\u56fe\u7247\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image  \nimage = Image.open(&#39;input_image.jpg&#39;)  \nimage.save(&#39;output_image.png&#39;)  \n<\/code><\/pre>\n<p>\u5bf9\u4e8eOpenCV\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import cv2  \nimage = cv2.imread(&#39;input_image.jpg&#39;)  \ncv2.imwrite(&#39;output_image.png&#39;, image)  \n<\/code><\/pre>\n<p>\u800c\u5728Matplotlib\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt  \nplt.imshow(image)  \nplt.axis(&#39;off&#39;)  \nplt.savefig(&#39;output_image.png&#39;)  \n<\/code><\/pre>\n<p><strong>\u4fdd\u5b58\u56fe\u7247\u65f6\u53ef\u4ee5\u9009\u62e9\u54ea\u4e9b\u683c\u5f0f\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f\uff0c\u5305\u62ecJPEG\u3001PNG\u3001BMP\u3001GIF\u7b49\u3002\u6bcf\u79cd\u683c\u5f0f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u4f18\u70b9\uff0c\u4f8b\u5982JPEG\u9002\u5408\u4fdd\u5b58\u9ad8\u8d28\u91cf\u7167\u7247\uff0cPNG\u5219\u652f\u6301\u900f\u660e\u80cc\u666f\uff0cGIF\u5219\u9002\u5408\u5b58\u50a8\u7b80\u5355\u52a8\u753b\u3002\u5728\u4f7f\u7528Pillow\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u6269\u5c55\u540d\u6307\u5b9a\u683c\u5f0f\uff0c\u5982<code>.jpg<\/code>, <code>.png<\/code>\u7b49\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4fdd\u5b58\u56fe\u7247\u65f6\u9047\u5230\u7684\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u56fe\u7247\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u6587\u4ef6\u8def\u5f84\u9519\u8bef\u3001\u6743\u9650\u4e0d\u8db3\u6216\u683c\u5f0f\u4e0d\u652f\u6301\u7b49\u3002\u786e\u4fdd\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\u5b58\u5728\u4e14\u53ef\u5199\uff0c\u540c\u65f6\u68c0\u67e5\u56fe\u7247\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u5982\u679c\u4f7f\u7528\u4e86\u4e0d\u652f\u6301\u7684\u683c\u5f0f\uff0c\u4ee3\u7801\u5c06\u5f15\u53d1\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u4fdd\u5b58\u4e4b\u524d\u9a8c\u8bc1\u683c\u5f0f\u662f\u4e2a\u597d\u4e60\u60ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u7247\uff0c\u4f8b\u5982\u4f7f\u7528Pillow\u5e93\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1119999,"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\/1119979"}],"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=1119979"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119979\/revisions"}],"predecessor-version":[{"id":1120002,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119979\/revisions\/1120002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119999"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}