{"id":966877,"date":"2024-12-27T04:51:40","date_gmt":"2024-12-26T20:51:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966877.html"},"modified":"2024-12-27T04:51:42","modified_gmt":"2024-12-26T20:51:42","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96png%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966877.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6png\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182433\/e69ee33c-11ca-4136-8695-92640c427ee5.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6png\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6PNG\u6587\u4ef6\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09\u3001OpenCV\u3001matplotlib\u3001scikit-image\u7b49\u3002PIL\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3001OpenCV\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u4e14\u5904\u7406\u901f\u5ea6\u8f83\u5feb\u3001matplotlib\u9002\u5408\u7528\u4e8e\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u3001scikit-image\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PIL\u5e93\u8bfb\u53d6PNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\u4e4b\u4e00\u3002\u5b83\u7684\u6269\u5c55\u7248\u672c\u662fPillow\uff0cPillow\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u517c\u5bb9\u6027\u3002\u8981\u8bfb\u53d6PNG\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6765\u6253\u5f00PNG\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>show()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2aPNG\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09<\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u662fPython\u56fe\u50cf\u5904\u7406\u7684\u57fa\u7840\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\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\u6765\u8bfb\u53d6PNG\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00PNG\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>img.show()<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u7684\u57fa\u672c\u4fe1\u606f<\/strong><\/h2>\n<p>print(f&quot;Format: {img.format}, Size: {img.size}, Mode: {img.mode}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>Image.open()<\/code>\u7528\u4e8e\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c<code>show()<\/code>\u7528\u4e8e\u663e\u793a\u56fe\u50cf\uff0c<code>img.format<\/code>\u3001<code>img.size<\/code>\u548c<code>img.mode<\/code>\u5206\u522b\u8fd4\u56de\u56fe\u50cf\u7684\u683c\u5f0f\u3001\u5c3a\u5bf8\u548c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u8f6c\u6362\u4e0e\u4fdd\u5b58<\/strong><\/p>\n<\/p>\n<p><p>Pillow\u8fd8\u63d0\u4f9b\u4e86\u56fe\u50cf\u683c\u5f0f\u8f6c\u6362\u548c\u4fdd\u5b58\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06PNG\u56fe\u50cf\u8f6c\u6362\u4e3aJPEG\u683c\u5f0f\u5e76\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06PNG\u683c\u5f0f\u8f6c\u6362\u4e3aJPEG\u683c\u5f0f<\/p>\n<p>rgb_img = img.convert(&#39;RGB&#39;)<\/p>\n<p>rgb_img.save(&#39;example_converted.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>convert(&#39;RGB&#39;)<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aRGB\u6a21\u5f0f\uff0c\u56e0\u4e3aJPEG\u4e0d\u652f\u6301Alpha\u901a\u9053\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>Pillow\u652f\u6301\u591a\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u7b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u56fe\u50cf\u65cb\u8f6c90\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>rotated_img = img.rotate(90)<\/p>\n<p>rotated_img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPillow\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u56fe\u50cf\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528OpenCV<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u5b83\u4ee5C++\u8bed\u8a00\u5f00\u53d1\uff0c\u4f46\u4e5f\u63d0\u4f9b\u4e86Python\u63a5\u53e3\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5OpenCV\u7684Python\u7248\u672c\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\u4f7f\u7528OpenCV\u8bfb\u53d6PNG\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6PNG\u6587\u4ef6<\/strong><\/h2>\n<p>img = cv2.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, img)<\/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><code>cv2.imread()<\/code>\u7528\u4e8e\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c<code>cv2.imshow()<\/code>\u7528\u4e8e\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>OpenCV\u652f\u6301\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u975e\u5e38\u5e7f\u6cdb\uff0c\u5305\u62ec\u56fe\u50cf\u6ee4\u6ce2\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u5f62\u6001\u5b66\u64cd\u4f5c\u7b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u9ad8\u65af\u6a21\u7cca\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9ad8\u65af\u6a21\u7cca<\/p>\n<p>blurred_img = cv2.GaussianBlur(img, (5, 5), 0)<\/p>\n<p>cv2.imshow(&#39;Blurred Image&#39;, blurred_img)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>OpenCV\u5728\u5b9e\u65f6\u56fe\u50cf\u5904\u7406\u65b9\u9762\u8868\u73b0\u4f18\u5f02\uff0c\u9002\u5408\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528matplotlib<\/p>\n<\/p>\n<p><p>matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u7684\u663e\u793a\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5matplotlib\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\u8bfb\u53d6\u5e76\u663e\u793aPNG\u6587\u4ef6\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\u6587\u4ef6<\/strong><\/h2>\n<p>img = mpimg.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u9690\u85cf\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>mpimg.imread()<\/code>\u7528\u4e8e\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c<code>plt.imshow()<\/code>\u7528\u4e8e\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>matplotlib\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408numpy\u5bf9\u56fe\u50cf\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_img = np.dot(img[..., :3], [0.2989, 0.5870, 0.1140])<\/p>\n<h2><strong>\u663e\u793a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(gray_img, cmap=&#39;gray&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528scikit-image<\/p>\n<\/p>\n<p><p>scikit-image\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u57fa\u4e8escikit-learn\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5scikit-image\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scikit-image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528scikit-image\u8bfb\u53d6PNG\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io<\/p>\n<h2><strong>\u8bfb\u53d6PNG\u6587\u4ef6<\/strong><\/h2>\n<p>img = io.imread(&#39;example.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>io.imshow(img)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>io.imread()<\/code>\u7528\u4e8e\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\uff0c<code>io.imshow()<\/code>\u7528\u4e8e\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>scikit-image\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5982\u56fe\u50cf\u5206\u5272\u3001\u7279\u5f81\u63d0\u53d6\u3001\u56fe\u50cf\u6062\u590d\u7b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import filters<\/p>\n<h2><strong>\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = filters.sobel(img)<\/p>\n<h2><strong>\u663e\u793a\u8fb9\u7f18\u68c0\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>io.imshow(edges)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>scikit-image\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7814\u7a76\u548c\u5f00\u53d1\u4e2d\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8bfb\u53d6\u548c\u5904\u7406PNG\u6587\u4ef6\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u3002Pillow\u9002\u5408\u4e8e\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0cOpenCV\u9002\u5408\u4e8e\u5b9e\u65f6\u56fe\u50cf\u5904\u7406\uff0cmatplotlib\u9002\u5408\u4e8e\u56fe\u50cf\u7684\u53ef\u89c6\u5316\uff0cscikit-image\u9002\u5408\u4e8e\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6PNG\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6PNG\u6587\u4ef6\u901a\u5e38\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u6216\u5176\u5206\u652fPillow\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6PNG\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00PNG\u6587\u4ef6\nimage = Image.open(&#39;your_image.png&#39;)\n# \u663e\u793a\u56fe\u7247\nimage.show()\n# \u83b7\u53d6\u56fe\u7247\u7684\u5c3a\u5bf8\nprint(image.size)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<p><strong>Python\u8bfb\u53d6PNG\u6587\u4ef6\u540e\u53ef\u4ee5\u8fdb\u884c\u54ea\u4e9b\u64cd\u4f5c\uff1f<\/strong><br \/>\u8bfb\u53d6PNG\u6587\u4ef6\u540e\uff0c\u4f7f\u7528Pillow\u5e93\u53ef\u4ee5\u6267\u884c\u591a\u79cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u526a\u88c1\u3001\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u6ee4\u955c\u5904\u7406\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u50cf\u5927\u5c0f\nresized_image = image.resize((200, 200))\nresized_image.show()\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\u4e3a\u65b0\u7684PNG\u6587\u4ef6\u6216\u5176\u4ed6\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6PNG\u6587\u4ef6\u65f6\u53ef\u80fd\u9047\u5230\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6PNG\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6587\u4ef6\u683c\u5f0f\u4e0d\u652f\u6301\u3002\u4e3a\u4e86\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u7ed3\u6784\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    image = Image.open(&#39;your_image.png&#39;)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u3002&quot;)\nexcept IOError:\n    print(&quot;\u65e0\u6cd5\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u53ef\u80fd\u4e0d\u662f\u6709\u6548\u7684PNG\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u5e76\u63d0\u4f9b\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6PNG\u6587\u4ef6\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\u5e93\uff09\u3001OpenCV\u3001matp [&hellip;]","protected":false},"author":3,"featured_media":966878,"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\/966877"}],"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=966877"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966877\/revisions"}],"predecessor-version":[{"id":966880,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966877\/revisions\/966880"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/966878"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=966877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=966877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=966877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}