{"id":929795,"date":"2024-12-26T17:02:51","date_gmt":"2024-12-26T09:02:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/929795.html"},"modified":"2024-12-26T17:02:53","modified_gmt":"2024-12-26T09:02:53","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5jpg","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/929795.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165jpg"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064821\/e54b266e-9258-47f6-bf93-a1d1227f35ee.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165jpg\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bfc\u5165JPG\u56fe\u50cf\uff0c\u901a\u5e38\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u6216matplotlib\u5e93\u3002PIL\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u56fe\u50cf\u6253\u5f00\u548c\u5904\u7406\u529f\u80fd\u3001OpenCV\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3001matplotlib\u5219\u66f4\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\u3002<\/strong>\u5176\u4e2d\uff0cPIL\u5e93\u4e2d\u7684Pillow\u662f\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\u4e4b\u4e00\uff0c\u56e0\u5176\u4f7f\u7528\u7b80\u5355\u4e14\u529f\u80fd\u5168\u9762\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u5bfc\u5165JPG\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u5bfc\u5165JPG\u56fe\u50cf\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5229\u7528<code>Image.show()<\/code>\u67e5\u770b\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\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<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#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\u3001PIL\u5e93\uff08Pillow\uff09\u7684\u5b89\u88c5\u4e0e\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3002\u5b89\u88c5Pillow\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528pip\u547d\u4ee4\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\u5e93\u4e2d\u7684Image\u6a21\u5757\u6765\u6253\u5f00\u548c\u5904\u7406JPG\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u6253\u5f00JPG\u56fe\u50cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\uff0c\u7136\u540e\u901a\u8fc7<code>Image.show()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\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<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7Pillow\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u56fe\u50cf\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u4f8b\u5982\u56fe\u50cf\u7684\u683c\u5f0f\u3001\u5927\u5c0f\u548c\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u683c\u5f0f<\/p>\n<p>format = img.format<\/p>\n<p>print(&quot;\u56fe\u50cf\u683c\u5f0f\uff1a&quot;, format)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>size = img.size<\/p>\n<p>print(&quot;\u56fe\u50cf\u5927\u5c0f\uff1a&quot;, size)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u6a21\u5f0f<\/strong><\/h2>\n<p>mode = img.mode<\/p>\n<p>print(&quot;\u56fe\u50cf\u6a21\u5f0f\uff1a&quot;, mode)<\/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\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u8c03\u6574\u5927\u5c0f\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u50cf<\/p>\n<p>cropped_img = img.crop((100, 100, 400, 400))<\/p>\n<p>cropped_img.show()<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_img = img.rotate(45)<\/p>\n<p>rotated_img.show()<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_img = img.resize((200, 200))<\/p>\n<p>resized_img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001OpenCV\u5e93\u7684\u5b89\u88c5\u4e0e\u4f7f\u7528<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u867d\u7136OpenCV\u66f4\u9002\u5408\u7528\u4e8e\u590d\u6742\u7684\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u4efb\u52a1\uff0c\u4f46\u5176\u57fa\u672c\u7684\u56fe\u50cf\u5bfc\u5165\u529f\u80fd\u540c\u6837\u7b80\u5355\u5b9e\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5OpenCV<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bfc\u5165\u548c\u663e\u793a\u56fe\u50cf<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528OpenCV\u5bfc\u5165\u548c\u663e\u793a\u56fe\u50cf\u53ef\u4ee5\u901a\u8fc7cv2\u6a21\u5757\u6765\u5b9e\u73b0\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>img = cv2.imread(&#39;example.jpg&#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<ol start=\"3\">\n<li><strong>\u83b7\u53d6\u56fe\u50cf\u4fe1\u606f<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0ePillow\u7c7b\u4f3c\uff0cOpenCV\u4e5f\u53ef\u4ee5\u83b7\u53d6\u56fe\u50cf\u7684\u57fa\u672c\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u7684\u5f62\u72b6\uff08\u9ad8\u5ea6\uff0c\u5bbd\u5ea6\uff0c\u901a\u9053\u6570\uff09<\/p>\n<p>shape = img.shape<\/p>\n<p>print(&quot;\u56fe\u50cf\u5f62\u72b6\uff1a&quot;, shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001matplotlib\u5e93\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u663e\u793a\u3002\u4f7f\u7528matplotlib\u663e\u793a\u56fe\u50cf\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5matplotlib<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5matplotlib\uff0c\u53ef\u4ee5\u4f7f\u7528\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<ol start=\"2\">\n<li><strong>\u5bfc\u5165\u548c\u663e\u793a\u56fe\u50cf<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528matplotlib\u5bfc\u5165\u548c\u663e\u793a\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u901a\u5e38\u4e0ePillow\u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u4f7f\u7528matplotlib\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img)<\/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>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u5165JPG\u56fe\u50cf\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u9879\u76ee\u7684\u590d\u6742\u6027\u3002<strong>Pillow\u5e93\uff08PIL\uff09\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3001OpenCV\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3001matplotlib\u5219\u66f4\u9002\u5408\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6JPG\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\uff08Pillow\uff09\u6765\u8bfb\u53d6JPG\u6587\u4ef6\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u6765\u5b8c\u6210\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bfb\u53d6\u5e76\u663e\u793aJPG\u56fe\u7247\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00JPG\u6587\u4ef6\nimage = Image.open(&#39;your_image.jpg&#39;)\n# \u663e\u793a\u56fe\u7247\nimage.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5f00\u6307\u5b9a\u8def\u5f84\u7684JPG\u6587\u4ef6\uff0c\u5e76\u5728\u9ed8\u8ba4\u7684\u56fe\u50cf\u67e5\u770b\u5668\u4e2d\u663e\u793a\u5b83\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5bfc\u5165\u7684JPG\u56fe\u7247\uff1f<\/strong><br \/>\u4f7f\u7528PIL\u5e93\u540e\uff0c\u53ef\u4ee5\u5bf9\u5bfc\u5165\u7684JPG\u56fe\u7247\u8fdb\u884c\u591a\u79cd\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u8fc7\u6ee4\u7b49\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u8c03\u6574\u56fe\u7247\u5927\u5c0f\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u7247\u5927\u5c0f\nnew_image = image.resize((800, 600))\nnew_image.show()\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u56fe\u7247\u8c03\u6574\u5230800&#215;600\u7684\u5c3a\u5bf8\u5e76\u663e\u793a\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u5e93\u5bfc\u5165JPG\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86PIL\u5e93\uff0cOpenCV\u4e5f\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u9879\uff0c\u5e7f\u6cdb\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u4f7f\u7528OpenCV\u5bfc\u5165JPG\u6587\u4ef6\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d6JPG\u6587\u4ef6\nimage = cv2.imread(&#39;your_image.jpg&#39;)\n# \u663e\u793a\u56fe\u7247\ncv2.imshow(&#39;Image&#39;, image)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p>OpenCV\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u64cd\u4f5c\u7684\u7528\u6237\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165JPG\u56fe\u50cf\uff0c\u901a\u5e38\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u6216matplotlib\u5e93\u3002PIL\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u56fe [&hellip;]","protected":false},"author":3,"featured_media":929800,"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\/929795"}],"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=929795"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929795\/revisions"}],"predecessor-version":[{"id":929801,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929795\/revisions\/929801"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/929800"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=929795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=929795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=929795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}