{"id":966746,"date":"2024-12-27T04:50:27","date_gmt":"2024-12-26T20:50:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966746.html"},"modified":"2024-12-27T04:50:29","modified_gmt":"2024-12-26T20:50:29","slug":"python%e4%bd%a0%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/966746.html","title":{"rendered":"python\u4f60\u5982\u4f55\u5b9a\u4e49\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182403\/6c28200f-9239-4b40-bb01-e35f29646422.webp\" alt=\"python\u4f60\u5982\u4f55\u5b9a\u4e49\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93\u3001Matplotlib\u5e93\u3001OpenCV\u5e93\u7b49\u591a\u79cd\u65b9\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0cPIL\uff08Pillow\uff09\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7PIL\u5e93\uff0c\u4f60\u53ef\u4ee5\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u7247\uff0c\u5982JPEG\u3001PNG\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528PIL\u5e93\u5b9a\u4e49\u548c\u64cd\u4f5c\u56fe\u7247\u7684\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PIL\uff08Pillow\uff09\u5e93<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e76\u4e14\u662fPython\u4e2d\u5904\u7406\u56fe\u50cf\u7684\u4e00\u4e2a\u91cd\u8981\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Pillow\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\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. \u6253\u5f00\u548c\u663e\u793a\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u7247\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u56fe\u7247\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Pillow\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u5bf9\u56fe\u7247\u8fdb\u884c\u57fa\u672c\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u88c1\u526a\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u6574\u5927\u5c0f<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/p>\n<p>resized_image = image.resize((200, 200))<\/p>\n<p>resized_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u65cb\u8f6c\u56fe\u7247<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u7247<\/p>\n<p>rotated_image = image.rotate(90)<\/p>\n<p>rotated_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u88c1\u526a\u56fe\u7247<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u7247<\/p>\n<p>cropped_image = image.crop((100, 100, 400, 400))<\/p>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>Matplotlib\u5e93\u901a\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u5c55\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Matplotlib\u5e93\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Matplotlib\u663e\u793a\u56fe\u7247<\/h4>\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\u548c\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>img = mpimg.imread(&#39;example.jpg&#39;)<\/p>\n<p>plt.imshow(img)<\/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><h3>\u4e09\u3001OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5OpenCV\u5e93<\/h4>\n<\/p>\n<p><p>\u4f60\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><h4>2. \u4f7f\u7528OpenCV\u6253\u5f00\u548c\u663e\u793a\u56fe\u7247<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u7247<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#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><h4>3. \u56fe\u7247\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>OpenCV\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u56fe\u7247\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u6574\u5927\u5c0f<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/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<ul>\n<li><strong>\u65cb\u8f6c\u56fe\u7247<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u7247<\/p>\n<p>(h, w) = image.shape[:2]<\/p>\n<p>center = (w \/ 2, h \/ 2)<\/p>\n<p>M = cv2.getRotationMatrix2D(center, 90, 1.0)<\/p>\n<p>rotated_image = cv2.warpAffine(image, M, (w, h))<\/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<ul>\n<li><strong>\u88c1\u526a\u56fe\u7247<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u7247<\/p>\n<p>cropped_image = image[100:400, 100:400]<\/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>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9a\u4e49\u548c\u64cd\u4f5c\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u5176\u4e2dPillow\u3001Matplotlib\u548cOpenCV\u662f\u6700\u5e38\u7528\u7684\u5e93\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u4f18\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002Pillow\u9002\u5408\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0cMatplotlib\u9002\u5408\u5feb\u901f\u7684\u56fe\u50cf\u663e\u793a\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800cOpenCV\u5219\u9002\u5408\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u56fe\u50cf\u5904\u7406\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecPillow\u548cOpenCV\u3002\u4f7f\u7528Pillow\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image  \nimport matplotlib.pyplot as plt  \n\nimg = Image.open(&#39;image.jpg&#39;)  \nplt.imshow(img)  \nplt.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74  \nplt.show()  \n<\/code><\/pre>\n<p>\u5bf9\u4e8eOpenCV\uff0c\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247\u7684\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import cv2  \n\nimg = cv2.imread(&#39;image.jpg&#39;)  \ncv2.imshow(&#39;Image&#39;, img)  \ncv2.waitKey(0)  \ncv2.destroyAllWindows()  \n<\/code><\/pre>\n<p>\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u5373\u53ef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u56fe\u7247\u7684\u5927\u5c0f\u548c\u683c\u5f0f\uff1f<\/strong><br \/>\u5904\u7406\u56fe\u7247\u7684\u5927\u5c0f\u548c\u683c\u5f0f\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u901a\u8fc7<code>resize()<\/code>\u65b9\u6cd5\u8c03\u6574\u56fe\u7247\u5927\u5c0f\uff0c\u4f7f\u7528<code>save()<\/code>\u65b9\u6cd5\u6539\u53d8\u56fe\u7247\u683c\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image  \n\nimg = Image.open(&#39;image.jpg&#39;)  \nimg_resized = img.resize((200, 200))  # \u8c03\u6574\u56fe\u7247\u5927\u5c0f\u4e3a200x200  \nimg_resized.save(&#39;resized_image.png&#39;, format=&#39;PNG&#39;)  # \u4fdd\u5b58\u4e3aPNG\u683c\u5f0f  \n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u56fe\u7247\u7684\u5927\u5c0f\u548c\u683c\u5f0f\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u4f7f\u7528\u9700\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e93\u63a8\u8350\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u5e93\u53ef\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684\u5e93\uff1a  <\/p>\n<ol>\n<li><strong>Pillow<\/strong>\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u529f\u80fd\u4e30\u5bcc\uff0c\u6613\u4e8e\u4f7f\u7528\u3002  <\/li>\n<li><strong>OpenCV<\/strong>\uff1a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u9ad8\u6548\u5904\u7406\u548c\u5206\u6790\u56fe\u50cf\u7684\u5e94\u7528\u3002  <\/li>\n<li><strong>scikit-image<\/strong>\uff1a\u57fa\u4e8eNumPy\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u7b97\u6cd5\u548c\u5de5\u5177\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u56fe\u50cf\u5206\u6790\u3002  <\/li>\n<li><strong>Matplotlib<\/strong>\uff1a\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u7ed8\u56fe\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\uff0c\u9002\u5408\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\u4f7f\u7528\u3002<br \/>\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u80fd\u591f\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u56fe\u50cf\u5904\u7406\u6548\u679c\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93\u3001Matplotlib\u5e93\u3001OpenCV\u5e93\u7b49\u591a\u79cd\u65b9\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":966755,"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\/966746"}],"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=966746"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966746\/revisions"}],"predecessor-version":[{"id":966756,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966746\/revisions\/966756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/966755"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=966746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=966746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=966746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}