{"id":1149425,"date":"2025-01-13T16:49:19","date_gmt":"2025-01-13T08:49:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149425.html"},"modified":"2025-01-13T16:49:21","modified_gmt":"2025-01-13T08:49:21","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%b1%95%e7%a4%ba%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149425.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u5c55\u793a\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180130\/005d1b00-b791-4c5b-bdac-715b85241a12.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u5c55\u793a\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\uff0c\u5305\u62ecPIL\u5e93\u3001OpenCV\u5e93\u548cMatplotlib\u5e93\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\u3002<\/strong> \u4f8b\u5982\uff0cPIL\u5e93\uff08Pillow\uff09\u56e0\u5176\u7b80\u5355\u6613\u7528\u3001\u529f\u80fd\u5f3a\u5927\u800c\u5e7f\u53d7\u6b22\u8fce\uff0cOpenCV\u5219\u56e0\u5176\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u9886\u57df\u5907\u53d7\u9752\u7750\uff0cMatplotlib\u5219\u9002\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u7684\u56fe\u50cf\u5c55\u793a\u3002<strong>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528PIL\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528PIL\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u8fc7\u7a0b\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\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\u5c31\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u5bfc\u5165\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u6253\u5f00\u5e76\u5c55\u793a\u540d\u4e3a&quot;example.jpg&quot;\u7684\u56fe\u7247\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3Python\u4e2d\u5982\u4f55\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u548cMatplotlib\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001PIL\u5e93\uff08Pillow\uff09<\/strong><\/h2>\n<p><h2>1. \u5b89\u88c5Pillow\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528PIL\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\u3002Pillow\u662fPIL\u5e93\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e76\u4e14\u662f\u76ee\u524d\u6700\u5e38\u7528\u7684\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u5bfc\u5165\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u5bfc\u5165\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf<code>image<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>image.show()<\/code>\u65b9\u6cd5\u5c55\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h2>3. \u64cd\u4f5c\u56fe\u7247<\/h2>\n<\/p>\n<p><p>Pillow\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u7247\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u7247\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>(1) \u7f29\u653e\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f29\u653e\u56fe\u7247<\/p>\n<p>resized_image = image.resize((200, 200))<\/p>\n<p>resized_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(2) \u526a\u88c1\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u526a\u88c1\u56fe\u7247<\/p>\n<p>cropped_image = image.crop((100, 100, 300, 300))<\/p>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(3) \u65cb\u8f6c\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u7247<\/p>\n<p>rotated_image = image.rotate(45)<\/p>\n<p>rotated_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(4) \u8f6c\u6362\u56fe\u7247\u683c\u5f0f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u56fe\u7247\u683c\u5f0f<\/p>\n<p>image.save(&#39;example.png&#39;, &#39;PNG&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001OpenCV\u5e93<\/strong><\/h2>\n<p><h2>1. \u5b89\u88c5OpenCV\u5e93<\/h2>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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<p><h2>2. \u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u4e5f\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u8fc7\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u5bfc\u5165\u56fe\u7247<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c55\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\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>cv2.imread()<\/code>\u65b9\u6cd5\u5bfc\u5165\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf<code>image<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>cv2.imshow()<\/code>\u65b9\u6cd5\u5c55\u793a\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>cv2.waitKey(0)<\/code>\u65b9\u6cd5\u7b49\u5f85\u7528\u6237\u6309\u4e0b\u4efb\u610f\u952e\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>3. \u64cd\u4f5c\u56fe\u7247<\/h2>\n<\/p>\n<p><p>OpenCV\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u7247\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u7247\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>(1) \u7f29\u653e\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f29\u653e\u56fe\u7247<\/p>\n<p>resized_image = cv2.resize(image, (200, 200))<\/p>\n<p>cv2.imshow(&#39;Resized Image&#39;, resized_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(2) \u526a\u88c1\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u526a\u88c1\u56fe\u7247<\/p>\n<p>cropped_image = image[100:300, 100:300]<\/p>\n<p>cv2.imshow(&#39;Cropped Image&#39;, cropped_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(3) \u65cb\u8f6c\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u7247<\/p>\n<p>rows, cols = image.shape[:2]<\/p>\n<p>M = cv2.getRotationMatrix2D((cols \/ 2, rows \/ 2), 45, 1)<\/p>\n<p>rotated_image = cv2.warpAffine(image, M, (cols, rows))<\/p>\n<p>cv2.imshow(&#39;Rotated Image&#39;, rotated_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(4) \u8f6c\u6362\u56fe\u7247\u683c\u5f0f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u56fe\u7247\u683c\u5f0f<\/p>\n<p>cv2.imwrite(&#39;example.png&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001Matplotlib\u5e93<\/strong><\/h2>\n<p><h2>1. \u5b89\u88c5Matplotlib\u5e93<\/h2>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u7684Python\u7ed8\u56fe\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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<p><h2>2. \u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\u4e5f\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u8fc7\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\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>\u5bfc\u5165\u56fe\u7247<\/strong><\/h2>\n<p>image = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u7247<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>mpimg.imread()<\/code>\u65b9\u6cd5\u5bfc\u5165\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf<code>image<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plt.imshow()<\/code>\u65b9\u6cd5\u5c55\u793a\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h2>3. \u64cd\u4f5c\u56fe\u7247<\/h2>\n<\/p>\n<p><p>Matplotlib\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u56fe\u7247\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u7247\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>(1) \u7f29\u653e\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f29\u653e\u56fe\u7247<\/p>\n<p>resized_image = plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(2) \u526a\u88c1\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u526a\u88c1\u56fe\u7247<\/p>\n<p>cropped_image = image[100:300, 100:300]<\/p>\n<p>plt.imshow(cropped_image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(3) \u65cb\u8f6c\u56fe\u7247<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.ndimage import rotate<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u7247<\/strong><\/h2>\n<p>rotated_image = rotate(image, 45)<\/p>\n<p>plt.imshow(rotated_image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>(4) \u8f6c\u6362\u56fe\u7247\u683c\u5f0f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u56fe\u7247\u683c\u5f0f<\/p>\n<p>plt.imsave(&#39;example.png&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/strong><\/h2>\n<p><h2>1. \u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>PIL\u5e93\uff08Pillow\uff09\u3001OpenCV\u5e93\u548cMatplotlib\u5e93\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>(1) PIL\u5e93\uff08Pillow\uff09<\/h3>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\uff0c\u4ee3\u7801\u7b80\u6d01<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u7247\u64cd\u4f5c\u529f\u80fd<\/li>\n<li>\u5e7f\u6cdb\u5e94\u7528\u4e8eWeb\u5f00\u53d1\u548c\u684c\u9762\u5e94\u7528<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6027\u80fd\u8f83OpenCV\u7a0d\u5f31<\/li>\n<li>\u5904\u7406\u5927\u578b\u56fe\u7247\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u74f6\u9888<\/li>\n<\/ul>\n<p><h3>(2) OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd<\/li>\n<li>\u4f18\u5f02\u7684\u6027\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u56fe\u7247\u548c\u5b9e\u65f6\u56fe\u50cf\u5904\u7406<\/li>\n<li>\u5e7f\u6cdb\u5e94\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u673a\u5668\u5b66\u4e60\u9886\u57df<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5b66\u4e60\u66f2\u7ebf\u8f83\u9661\uff0c\u4ee3\u7801\u76f8\u5bf9\u590d\u6742<\/li>\n<li>\u56fe\u50cf\u5c55\u793a\u529f\u80fd\u76f8\u5bf9\u8f83\u5f31<\/li>\n<\/ul>\n<p><h3>(3) Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u529f\u80fd<\/li>\n<li>\u9002\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u79d1\u7814\u9886\u57df<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u56fe\u7247\u64cd\u4f5c\u529f\u80fd\u76f8\u5bf9\u8f83\u5f31<\/li>\n<li>\u6027\u80fd\u8f83OpenCV\u7a0d\u5f31<\/li>\n<\/ul>\n<p><h2>2. \u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528PIL\u5e93\uff08Pillow\uff09\u3001OpenCV\u5e93\u548cMatplotlib\u5e93\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\uff0c\u5e76\u5c55\u793a\u4e86\u5404\u5e93\u7684\u5e38\u89c1\u56fe\u7247\u64cd\u4f5c\u529f\u80fd\u3002<strong>PIL\u5e93\u9002\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u7247\u64cd\u4f5c\u548c\u5c55\u793a\uff0cOpenCV\u5e93\u9002\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\uff0cMatplotlib\u5e93\u9002\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u7684\u56fe\u7247\u5c55\u793a\u3002<\/strong> \u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u56fe\u50cf\u5904\u7406\u548c\u5c55\u793a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\uff0c\u5e38\u7528\u7684\u6709Pillow\u548cMatplotlib\u3002Pillow\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u800cMatplotlib\u5219\u662f\u4e00\u4e2a\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\u3002\u4f7f\u7528Pillow\u53ef\u4ee5\u901a\u8fc7<code>Image.open()<\/code>\u65b9\u6cd5\u5bfc\u5165\u56fe\u7247\uff0c\u4f7f\u7528Matplotlib\u7684<code>imshow()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5c55\u793a\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport matplotlib.pyplot as plt\n\n# \u5bfc\u5165\u56fe\u7247\nimage = Image.open(&#39;your_image_path.jpg&#39;)\n\n# \u663e\u793a\u56fe\u7247\nplt.imshow(image)\nplt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74\nplt.show()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528OpenCV\u5e93\u663e\u793a\u56fe\u7247\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u5bfc\u5165\u548c\u663e\u793a\u56fe\u7247\u3002\u901a\u8fc7<code>cv2.imread()<\/code>\u51fd\u6570\u5bfc\u5165\u56fe\u7247\uff0c\u4f7f\u7528<code>cv2.imshow()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u7a97\u53e3\u4e2d\u5c55\u793a\u56fe\u7247\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u5bfc\u5165\u56fe\u7247\nimage = cv2.imread(&#39;your_image_path.jpg&#39;)\n\n# \u663e\u793a\u56fe\u7247\ncv2.imshow(&#39;Image&#39;, image)\ncv2.waitKey(0)  # \u7b49\u5f85\u6309\u952e\ncv2.destroyAllWindows()  # \u5173\u95ed\u7a97\u53e3\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u5904\u7406\u89c6\u9891\u6d41\u6216\u8fdb\u884c\u5b9e\u65f6\u56fe\u50cf\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728Jupyter Notebook\u4e2d\u5982\u4f55\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u65b9\u5f0f\u6765\u663e\u793a\u56fe\u7247\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>%matplotlib inline<\/code>\u547d\u4ee4\u786e\u4fdd\u56fe\u50cf\u5728Notebook\u4e2d\u76f4\u63a5\u663e\u793a\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">%matplotlib inline\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\n# \u5bfc\u5165\u56fe\u7247\nimage = Image.open(&#39;your_image_path.jpg&#39;)\n\n# \u663e\u793a\u56fe\u7247\nplt.imshow(image)\nplt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u65b9\u4fbf\uff0c\u7279\u522b\u662f\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u6216\u673a\u5668\u5b66\u4e60\u5b9e\u9a8c\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5bfc\u5165\u548c\u5c55\u793a\u56fe\u7247\uff0c\u5305\u62ecPIL\u5e93\u3001OpenCV\u5e93\u548cMatplotlib\u5e93\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709 [&hellip;]","protected":false},"author":3,"featured_media":1149433,"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\/1149425"}],"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=1149425"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149425\/revisions"}],"predecessor-version":[{"id":1149436,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149425\/revisions\/1149436"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149433"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}