{"id":1004718,"date":"2024-12-27T10:29:04","date_gmt":"2024-12-27T02:29:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004718.html"},"modified":"2024-12-27T10:29:07","modified_gmt":"2024-12-27T02:29:07","slug":"%e4%bd%bf%e7%94%a8python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004718.html","title":{"rendered":"\u4f7f\u7528python\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\/25081537\/a09ac286-96f4-4d83-ac8b-2ac7be82d302.webp\" alt=\"\u4f7f\u7528python\u5982\u4f55\u4fdd\u5b58\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u4fdd\u5b58\u56fe\u7247\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09\u3001OpenCV\u548cMatplotlib\u7b49\u5e93\u3002PIL\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u3001OpenCV\u9002\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3001Matplotlib\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PIL\u5e93\u4fdd\u5b58\u56fe\u7247\u3002<\/strong><\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u56fe\u50cf\u7684\u529f\u80fd\u3002Pillow\u662fPIL\u7684\u4e00\u4e2a\u6d3e\u751f\u7248\u672c\uff0c\u589e\u52a0\u4e86\u4e00\u4e9b\u65b0\u7279\u6027\u548c\u652f\u6301\u3002\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u50cf\u7684\u6b65\u9aa4\u76f8\u5bf9\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Pillow\u5e93\uff1a<\/strong> \u5728\u4f7f\u7528Pillow\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<code>pip install pillow<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a0\u8f7d\u56fe\u50cf\uff1a<\/strong> \u4f7f\u7528Pillow\u7684<code>Image<\/code>\u6a21\u5757\u53ef\u4ee5\u52a0\u8f7d\u548c\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6765\u6253\u5f00\u73b0\u6709\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406\uff08\u53ef\u9009\uff09\uff1a<\/strong> \u5728\u4fdd\u5b58\u56fe\u50cf\u4e4b\u524d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u67d0\u4e9b\u5904\u7406\uff0c\u6bd4\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u5e94\u7528\u6ee4\u955c\u7b49\u3002Pillow\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u56fe\u50cf\uff1a<\/strong> \u4f7f\u7528<code>Image.save()<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u548c\u683c\u5f0f\u3002\u53ef\u4ee5\u9009\u62e9\u4fdd\u5b58\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u6bd4\u5982JPEG\u3001PNG\u3001BMP\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pillow\u5e93\u4fdd\u5b58\u56fe\u50cf\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\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u56fe\u50cf\u5904\u7406\uff08\u53ef\u9009\uff09<\/strong><\/h2>\n<h2><strong>\u6bd4\u5982\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>image = image.resize((800, 600))<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output.jpg&#39;, &#39;JPEG&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5176\u4ed6\u5e93\u5982\u4f55\u4fdd\u5b58\u56fe\u7247\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u8df5\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PIL\uff08Pillow\u5e93\uff09<\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u53cb\u597d\u7684\u754c\u9762\u548c\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u56fe\u50cf\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>Pillow\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u6253\u5f00\u548c\u4fdd\u5b58\u56fe\u50cf\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(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff08\u53ef\u9009\uff09<\/strong><\/h2>\n<p>image = image.rotate(90)  # \u65cb\u8f6c90\u5ea6<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output.png&#39;, &#39;PNG&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u683c\u5f0f\u8f6c\u6362<\/strong><\/li>\n<\/ol>\n<p><p>Pillow\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u683c\u5f0f\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>image.save(&#39;example.png&#39;)  # \u4fdd\u5b58\u4e3aPNG\u683c\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u526a\u5207\u3001\u8c03\u6574\u5927\u5c0f\u3001\u6ee4\u955c\u5e94\u7528\u7b49\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u5927\u5c0f<\/p>\n<p>image = image.resize((300, 300))<\/p>\n<h2><strong>\u5e94\u7528\u6ee4\u955c<\/strong><\/h2>\n<p>from PIL import ImageFilter<\/p>\n<p>image = image.filter(ImageFilter.BLUR)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;processed_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001OpenCV<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>OpenCV\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf\u7684\u57fa\u672c\u793a\u4f8b\u5982\u4e0b\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(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff08\u53ef\u9009\uff09<\/strong><\/h2>\n<p>image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # \u8f6c\u4e3a\u7070\u5ea6\u56fe<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output.jpg&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u6bd4\u5982\u8fb9\u7f18\u68c0\u6d4b\u3001\u989c\u8272\u8f6c\u6362\u3001\u5f62\u6001\u5b66\u53d8\u6362\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fb9\u7f18\u68c0\u6d4b<\/p>\n<p>edges = cv2.Canny(image, 100, 200)<\/p>\n<h2><strong>\u4fdd\u5b58\u8fb9\u7f18\u68c0\u6d4b\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;edges.jpg&#39;, edges)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001Matplotlib<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u9664\u4e86\u7ed8\u5236\u5404\u79cd\u56fe\u8868\u5916\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u7684\u8bfb\u53d6\u548c\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>Matplotlib\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u4fdd\u5b58\u56fe\u50cf\u7684\u57fa\u672c\u793a\u4f8b\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\u56fe\u50cf<\/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<\/strong><\/h2>\n<p>plt.imsave(&#39;output.png&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>Matplotlib\u4e3b\u8981\u7528\u4e8e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\uff0c\u6bd4\u5982\u8c03\u6574\u989c\u8272\u901a\u9053\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u989c\u8272\u901a\u9053<\/p>\n<p>img[:,:,0] = 0  # \u53bb\u6389\u7ea2\u8272\u901a\u9053<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imsave(&#39;processed_image.png&#39;, img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff1a\u5982\u679c\u9700\u8981\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\uff0cPillow\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5982\u679c\u6d89\u53ca\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u6216\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0cOpenCV\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\uff1b\u5982\u679c\u4e3b\u8981\u5173\u6ce8\u6570\u636e\u53ef\u89c6\u5316\uff0cMatplotlib\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5904\u7406\u5927\u56fe\u50cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5904\u7406\u5927\u56fe\u50cf\u65f6\u8981\u6ce8\u610f\u5185\u5b58\u5360\u7528\uff0c\u7279\u522b\u662f\u5728\u5185\u5b58\u6709\u9650\u7684\u73af\u5883\u4e2d\uff0c\u5c3d\u91cf\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u8fc7\u591a\u7684\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u683c\u5f0f\u548c\u8d28\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u4fdd\u5b58\u56fe\u50cf\u65f6\u8981\u6ce8\u610f\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u548c\u8d28\u91cf\u53c2\u6570\uff0c\u4ee5\u4fdd\u8bc1\u56fe\u50cf\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u7684\u5e73\u8861\u3002\u4f8b\u5982\uff0c\u4fdd\u5b58JPEG\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>quality<\/code>\u53c2\u6570\u6765\u8c03\u6574\u56fe\u50cf\u8d28\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image.save(&#39;output.jpg&#39;, &#39;JPEG&#39;, quality=85)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u65f6\uff0c\u8981\u6ce8\u610f\u6355\u83b7\u5f02\u5e38\uff0c\u7279\u522b\u662f\u5728\u6253\u5f00\u548c\u4fdd\u5b58\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u95ee\u9898\u5bfc\u81f4\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    image = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>    image.save(&#39;output.jpg&#39;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u6709\u6548\u5730\u4fdd\u5b58\u548c\u5904\u7406\u56fe\u50cf\u6587\u4ef6\uff0c\u6ee1\u8db3\u5404\u79cd\u9879\u76ee\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u7247\u7684\u4efb\u52a1\u4e2d\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\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\u3002\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\u662fPIL\uff08Pillow\uff09\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>Image.save()<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\nimg = Image.new(&#39;RGB&#39;, (100, 100), color=&#39;red&#39;)\n\n# \u4fdd\u5b58\u56fe\u50cf\nimg.save(&#39;image.png&#39;)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u7ea2\u8272\u7684100&#215;100\u50cf\u7d20\u56fe\u50cf\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u3002<\/p>\n<p><strong>Python\u4fdd\u5b58\u56fe\u7247\u65f6\u652f\u6301\u54ea\u4e9b\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528Pillow\u5e93\uff0cPython\u53ef\u4ee5\u4fdd\u5b58\u591a\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\uff0c\u5305\u62ecJPEG\u3001PNG\u3001GIF\u3001BMP\u3001TIFF\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u53ef\u4ee5\u6839\u636e\u7528\u9014\u6765\u51b3\u5b9a\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u9ad8\u8d28\u91cf\u7684\u7167\u7247\uff0cJPEG\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u900f\u660e\u80cc\u666f\u7684\u56fe\u50cf\uff0cPNG\u683c\u5f0f\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<p><strong>\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\u5982\u4f55\u5904\u7406\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u786e\u4fdd\u63d0\u4f9b\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002\u5982\u679c\u8def\u5f84\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\uff0c\u786e\u4fdd\u8def\u5f84\u7684\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nfrom PIL import Image\n\n# \u5b9a\u4e49\u6587\u4ef6\u8def\u5f84\ndirectory = &#39;images&#39;\nif not os.path.exists(directory):\n    os.makedirs(directory)\n\n# \u521b\u5efa\u56fe\u50cf\u5e76\u4fdd\u5b58\nimg = Image.new(&#39;RGB&#39;, (100, 100), color=&#39;blue&#39;)\nimg.save(os.path.join(directory, &#39;image.png&#39;))\n<\/code><\/pre>\n<p>\u6b64\u793a\u4f8b\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cimages\u201d\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4fdd\u5b58\u56fe\u7247\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09\u3001OpenCV\u548cMatplotlib\u7b49\u5e93\u3002PIL [&hellip;]","protected":false},"author":3,"featured_media":1004732,"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\/1004718"}],"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=1004718"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004718\/revisions"}],"predecessor-version":[{"id":1004733,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004718\/revisions\/1004733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004732"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}