{"id":1006239,"date":"2024-12-27T10:42:36","date_gmt":"2024-12-27T02:42:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1006239.html"},"modified":"2024-12-27T10:42:39","modified_gmt":"2024-12-27T02:42:39","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5png%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1006239.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165png\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082432\/8cd7984a-8d40-401f-87e9-d3c421cfe8ca.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165png\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bfc\u5165PNG\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u7684Image\u6a21\u5757\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u7b49\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5e76\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/strong>\u5176\u4e2d\uff0cPIL\u5e93\u662f\u8f83\u4e3a\u5e38\u7528\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u800cOpenCV\u5219\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u56fe\u50cf\u5904\u7406\u7684\u573a\u666f\u3002Matplotlib\u5219\u66f4\u591a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u5076\u5c14\u9700\u8981\u5c55\u793a\u56fe\u50cf\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u521d\u5b66\u8005\u800c\u8a00\uff0cPIL\u5e93\u662f\u4e00\u4e2a\u8f83\u597d\u7684\u9009\u62e9\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u3001\u663e\u793a\u548c\u5904\u7406PNG\u56fe\u50cf\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PIL\u5e93<\/strong>\uff1aPIL\u5e93\u5df2\u7ecf\u4e0d\u518d\u7ef4\u62a4\uff0c\u56e0\u6b64\u5efa\u8bae\u4f7f\u7528\u5176\u5206\u652fPillow\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li><strong>\u5bfc\u5165Image\u6a21\u5757<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\u901a\u8fc7<code>from PIL import Image<\/code>\u5bfc\u5165PIL\u5e93\u7684Image\u6a21\u5757\u3002<\/li>\n<li><strong>\u6253\u5f00PNG\u56fe\u7247<\/strong>\uff1a\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u3002<\/li>\n<li><strong>\u663e\u793a\u56fe\u7247<\/strong>\uff1a\u5229\u7528Image\u6a21\u5757\u7684<code>show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u7247\u3002<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5bfc\u5165PNG\u56fe\u7247\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528PIL\uff08Pillow\uff09\u5bfc\u5165PNG\u56fe\u7247<\/strong><\/h2>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u662f\u5904\u7406\u56fe\u50cf\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\u3002Pillow\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u9ad8\u6548\u7684API\uff0c\u4f7f\u5f97\u56fe\u50cf\u64cd\u4f5c\u53d8\u5f97\u8f7b\u677e\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165Pillow<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\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<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6253\u5f00\u548c\u663e\u793aPNG\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u6253\u5f00\u548c\u663e\u793a\u56fe\u7247\u975e\u5e38\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00PNG\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>Image.open()<\/code>\u65b9\u6cd5\u7528\u4e8e\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2aImage\u5bf9\u8c61\u3002<code>show()<\/code>\u65b9\u6cd5\u5219\u7528\u4e8e\u663e\u793a\u8be5\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>3. \u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>Pillow\u4e0d\u4ec5\u53ef\u4ee5\u6253\u5f00\u548c\u663e\u793a\u56fe\u7247\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7<code>image.format<\/code>\u3001<code>image.size<\/code>\u3001<code>image.mode<\/code>\u83b7\u53d6\u56fe\u50cf\u7684\u683c\u5f0f\u3001\u5c3a\u5bf8\u3001\u6a21\u5f0f\u7b49\u4fe1\u606f\u3002<\/li>\n<li><strong>\u56fe\u50cf\u8f6c\u6362<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>image.convert()<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u6a21\u5f0f\uff0c\u6bd4\u5982\u4eceRGB\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\uff08L\u6a21\u5f0f\uff09\u3002<\/li>\n<li><strong>\u56fe\u50cf\u4fdd\u5b58<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>image.save()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u56fe\u7247\u5230\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>print(&quot;Format:&quot;, image.format)<\/p>\n<p>print(&quot;Size:&quot;, image.size)<\/p>\n<p>print(&quot;Mode:&quot;, image.mode)<\/p>\n<h2><strong>\u8f6c\u6362\u56fe\u50cf\u4e3a\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>gray_image = image.convert(&#39;L&#39;)<\/p>\n<p>gray_image.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>gray_image.save(&#39;example_gray.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528OpenCV\u5bfc\u5165PNG\u56fe\u7247<\/strong><\/h2>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165OpenCV<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bfb\u53d6\u548c\u663e\u793aPNG\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\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\u53d6PNG\u56fe\u7247<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>cv2.imread()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2anumpy\u6570\u7ec4\u3002<code>cv2.imshow()<\/code>\u65b9\u6cd5\u7528\u4e8e\u663e\u793a\u56fe\u50cf\u3002<code>cv2.waitKey(0)<\/code>\u7528\u4e8e\u7b49\u5f85\u952e\u76d8\u4e8b\u4ef6\uff0c<code>cv2.destroyAllWindows()<\/code>\u7528\u4e8e\u5173\u95ed\u6240\u6709\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>3. \u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u64cd\u4f5c\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7<code>image.shape<\/code>\u83b7\u53d6\u56fe\u50cf\u7684\u5c3a\u5bf8\u548c\u901a\u9053\u6570\u3002<\/li>\n<li><strong>\u56fe\u50cf\u8f6c\u6362<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>cv2.cvtColor()<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u989c\u8272\u7a7a\u95f4\u3002<\/li>\n<li><strong>\u56fe\u50cf\u4fdd\u5b58<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>cv2.imwrite()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u56fe\u7247\u5230\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>print(&quot;Shape:&quot;, image.shape)<\/p>\n<h2><strong>\u8f6c\u6362\u56fe\u50cf\u4e3a\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<h2><strong>\u4fdd\u5b58\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>cv2.imwrite(&#39;example_gray.png&#39;, gray_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528Matplotlib\u5bfc\u5165PNG\u56fe\u7247<\/strong><\/h2>\n<p><p>Matplotlib\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165Matplotlib<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5e93\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<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bfb\u53d6\u548c\u663e\u793aPNG\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\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\u53d6PNG\u56fe\u7247<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>mpimg.imread()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2anumpy\u6570\u7ec4\u3002<code>plt.imshow()<\/code>\u65b9\u6cd5\u7528\u4e8e\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>3. \u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>Matplotlib\u7684\u56fe\u50cf\u64cd\u4f5c\u529f\u80fd\u8f83\u4e3a\u6709\u9650\uff0c\u4f46\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7<code>image.shape<\/code>\u83b7\u53d6\u56fe\u50cf\u7684\u5c3a\u5bf8\u548c\u901a\u9053\u6570\u3002<\/li>\n<li><strong>\u7b80\u5355\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u7ed3\u5408numpy\u8fdb\u884c\u4e00\u4e9b\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u3002<\/li>\n<li><strong>\u56fe\u50cf\u4fdd\u5b58<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>plt.imsave()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u56fe\u7247\u5230\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>print(&quot;Shape:&quot;, image.shape)<\/p>\n<h2><strong>\u8f6c\u6362\u56fe\u50cf\u4e3a\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>import numpy as np<\/p>\n<p>gray_image = np.dot(image[..., :3], [0.2989, 0.5870, 0.1140])<\/p>\n<p>plt.imshow(gray_image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>plt.imsave(&#39;example_gray.png&#39;, gray_image, cmap=&#39;gray&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/strong><\/h2>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u5bfc\u5165\u548c\u5904\u7406PNG\u56fe\u7247\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Pillow<\/strong>\uff1a\u9002\u5408\u9700\u8981\u8fdb\u884c\u7b80\u5355\u56fe\u50cf\u5904\u7406\u3001\u64cd\u4f5c\u548c\u8f6c\u6362\u7684\u573a\u666f\uff0cAPI\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li><strong>OpenCV<\/strong>\uff1a\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u7684\u573a\u666f\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li><strong>Matplotlib<\/strong>\uff1a\u9002\u5408\u9700\u8981\u7ed3\u5408\u6570\u636e\u53ef\u89c6\u5316\u8fdb\u884c\u7b80\u5355\u56fe\u50cf\u5c55\u793a\u7684\u573a\u666f\uff0c\u6613\u4e8e\u96c6\u6210\u3002<\/li>\n<\/ul>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6PNG\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u8bfb\u53d6PNG\u56fe\u7247\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fPIL\uff08Pillow\uff09\u5e93\u3002\u4f7f\u7528Pillow\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u548c\u5904\u7406PNG\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bfb\u53d6PNG\u56fe\u7247\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00PNG\u56fe\u7247\nimage = Image.open(&#39;path\/to\/your\/image.png&#39;)\nimage.show()\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5904\u7406PNG\u56fe\u7247\uff1f<\/strong><br \/>Python\u4e2d\u5904\u7406PNG\u56fe\u7247\u7684\u5e93\u6709\u591a\u4e2a\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ecPillow\u3001OpenCV\u548cMatplotlib\u3002Pillow\u5e93\u4e13\u6ce8\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\uff0c\u5305\u62ecPNG\u3002OpenCV\u5219\u66f4\u503e\u5411\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u64cd\u4f5c\u3002Matplotlib\u5219\u662f\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5904\u7406PNG\u56fe\u7247\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06PNG\u56fe\u7247\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528Pillow\u5e93\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06PNG\u56fe\u7247\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u4f8b\u5982JPEG\u6216BMP\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06PNG\u56fe\u7247\u8f6c\u6362\u4e3aJPEG\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00PNG\u56fe\u7247\nimage = Image.open(&#39;path\/to\/your\/image.png&#39;)\n\n# \u8f6c\u6362\u5e76\u4fdd\u5b58\u4e3aJPEG\u683c\u5f0f\nimage.convert(&#39;RGB&#39;).save(&#39;path\/to\/your\/image.jpg&#39;, &#39;JPEG&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u8f6c\u6362\u65f6\u4f7f\u7528<code>convert(&#39;RGB&#39;)<\/code>\uff0c\u56e0\u4e3aJPEG\u683c\u5f0f\u4e0d\u652f\u6301\u900f\u660e\u5ea6\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u4efb\u4f55\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165PNG\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u7684Image\u6a21\u5757\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u7b49\uff0c\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":1006248,"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\/1006239"}],"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=1006239"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006239\/revisions"}],"predecessor-version":[{"id":1006252,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006239\/revisions\/1006252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1006248"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1006239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1006239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1006239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}