{"id":1108022,"date":"2025-01-08T16:55:43","date_gmt":"2025-01-08T08:55:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108022.html"},"modified":"2025-01-08T16:55:47","modified_gmt":"2025-01-08T08:55:47","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e6%98%be%e7%a4%ba%e4%b8%a4%e5%bc%a0%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108022.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071845\/0095be7c-ca13-4ffe-aa9b-08b5d0d946e2.webp\" alt=\"python\u4e2d\u5982\u4f55\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u501f\u52a9<code>matplotlib<\/code>\u3001<code>PIL<\/code>\uff08Pillow\uff09\u548c<code>OpenCV<\/code>\u5e93\u3002<strong>matplotlib\u5e93\u3001PIL\u5e93\u3001OpenCV\u5e93<\/strong>\u662f\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93<\/h3>\n<\/p>\n<p><p><code>matplotlib<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u663e\u793a\u591a\u5f20\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>matplotlib<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\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<p><h4>2\u3001\u4f7f\u7528subplot\u663e\u793a\u591a\u5f20\u56fe\u7247<\/h4>\n<\/p>\n<p><p><code>matplotlib<\/code>\u5e93\u4e2d\u7684<code>subplot<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u7ed8\u56fe\u533a\u57df\u5206\u6210\u591a\u4e2a\u5b50\u533a\u57df\uff0c\u4ece\u800c\u5728\u540c\u4e00\u4e2a\u7a97\u53e3\u4e2d\u663e\u793a\u591a\u5f20\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\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\u53d6\u56fe\u7247<\/strong><\/h2>\n<p>img1 = mpimg.imread(&#39;path_to_first_image.jpg&#39;)<\/p>\n<p>img2 = mpimg.imread(&#39;path_to_second_image.jpg&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b1\u884c2\u5217\u7684\u5b50\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, axs = plt.subplots(1, 2, figsize=(10, 5))<\/p>\n<h2><strong>\u5728\u7b2c\u4e00\u4e2a\u5b50\u56fe\u533a\u57df\u663e\u793a\u7b2c\u4e00\u5f20\u56fe\u7247<\/strong><\/h2>\n<p>axs[0].imshow(img1)<\/p>\n<p>axs[0].axis(&#39;off&#39;)  # \u9690\u85cf\u5750\u6807\u8f74<\/p>\n<p>axs[0].set_title(&#39;First Image&#39;)<\/p>\n<h2><strong>\u5728\u7b2c\u4e8c\u4e2a\u5b50\u56fe\u533a\u57df\u663e\u793a\u7b2c\u4e8c\u5f20\u56fe\u7247<\/strong><\/h2>\n<p>axs[1].imshow(img2)<\/p>\n<p>axs[1].axis(&#39;off&#39;)  # \u9690\u85cf\u5750\u6807\u8f74<\/p>\n<p>axs[1].set_title(&#39;Second Image&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PIL\u5e93<\/h3>\n<\/p>\n<p><p><code>PIL<\/code>\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u56fe\u7247\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002<code>PIL<\/code>\u5e93\u7684\u73b0\u4ee3\u7248\u672c\u662f<code>Pillow<\/code>\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>PIL<\/code>\u5e93\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>Pillow<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\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 pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u4e24\u5f20\u56fe\u7247\u62fc\u63a5\u5728\u4e00\u8d77\u5e76\u663e\u793a<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u4e24\u5f20\u56fe\u7247\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u5e76\u4f7f\u7528<code>Pillow<\/code>\u5e93\u7684<code>show<\/code>\u65b9\u6cd5\u663e\u793a\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u7247<\/strong><\/h2>\n<p>img1 = Image.open(&#39;path_to_first_image.jpg&#39;)<\/p>\n<p>img2 = Image.open(&#39;path_to_second_image.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u7247\u5c3a\u5bf8<\/strong><\/h2>\n<p>width1, height1 = img1.size<\/p>\n<p>width2, height2 = img2.size<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u7247\uff0c\u5bbd\u5ea6\u4e3a\u4e24\u5f20\u56fe\u7247\u5bbd\u5ea6\u4e4b\u548c\uff0c\u9ad8\u5ea6\u4e3a\u4e24\u5f20\u56fe\u7247\u7684\u6700\u5927\u9ad8\u5ea6<\/strong><\/h2>\n<p>new_img = Image.new(&#39;RGB&#39;, (width1 + width2, max(height1, height2)))<\/p>\n<h2><strong>\u5c06\u4e24\u5f20\u56fe\u7247\u7c98\u8d34\u5230\u65b0\u7684\u56fe\u7247\u4e0a<\/strong><\/h2>\n<p>new_img.paste(img1, (0, 0))<\/p>\n<p>new_img.paste(img2, (width1, 0))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>new_img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528OpenCV\u5e93<\/h3>\n<\/p>\n<p><p><code>OpenCV<\/code>\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>OpenCV<\/code>\u5e93\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5OpenCV\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>OpenCV<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\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 opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u4e24\u5f20\u56fe\u7247\u62fc\u63a5\u5728\u4e00\u8d77\u5e76\u663e\u793a<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u4e24\u5f20\u56fe\u7247\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u5e76\u4f7f\u7528<code>OpenCV<\/code>\u5e93\u7684<code>imshow<\/code>\u65b9\u6cd5\u663e\u793a\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u7247<\/strong><\/h2>\n<p>img1 = cv2.imread(&#39;path_to_first_image.jpg&#39;)<\/p>\n<p>img2 = cv2.imread(&#39;path_to_second_image.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u4e24\u5f20\u56fe\u7247\u62fc\u63a5\u5728\u4e00\u8d77\uff08\u6c34\u5e73\u62fc\u63a5\uff09<\/strong><\/h2>\n<p>combined_img = cv2.hconcat([img1, img2])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>cv2.imshow(&#39;Combined Image&#39;, combined_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><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>matplotlib\u5e93\u3001PIL\u5e93\u3001OpenCV\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<code>matplotlib<\/code>\u5e93\u9002\u5408\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c<code>PIL<\/code>\u5e93\u9002\u5408\u7528\u4e8e\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u800c<code>OpenCV<\/code>\u5e93\u5219\u662f\u529f\u80fd\u6700\u4e3a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5e76\u6392\u663e\u793a\u4e24\u5f20\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u5e76\u6392\u663e\u793a\u4e24\u5f20\u56fe\u7247\u3002\u901a\u8fc7\u4f7f\u7528<code>subplot<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u56fe\u5f62\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u6392\u663e\u793a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\n# \u8bfb\u53d6\u56fe\u7247\nimg1 = mpimg.imread(&#39;image1.jpg&#39;)\nimg2 = mpimg.imread(&#39;image2.jpg&#39;)\n\n# \u521b\u5efa\u5b50\u56fe\nfig, (ax1, ax2) = plt.subplots(1, 2)\n\n# \u663e\u793a\u56fe\u7247\nax1.imshow(img1)\nax1.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74\nax2.imshow(img2)\nax2.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74\n\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u5728\u540c\u4e00\u7a97\u53e3\u4e2d\u5e76\u6392\u663e\u793a\u4e24\u5f20\u56fe\u7247\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u600e\u6837\u8c03\u6574\u663e\u793a\u56fe\u7247\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u4f7f\u7528Matplotlib\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>figsize<\/code>\u53c2\u6570\u6765\u8c03\u6574\u663e\u793a\u56fe\u7247\u7684\u5927\u5c0f\u3002<code>figsize<\/code>\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u8868\u793a\u56fe\u5f62\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff08\u4ee5\u82f1\u5bf8\u4e3a\u5355\u4f4d\uff09\u3002\u4ee5\u4e0b\u662f\u8c03\u6574\u56fe\u5f62\u5927\u5c0f\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))  # 10x5\u82f1\u5bf8\u7684\u56fe\u5f62\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8c03\u6574<code>figsize<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u83b7\u5f97\u66f4\u9002\u5408\u9700\u6c42\u7684\u663e\u793a\u6548\u679c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u540c\u65f6\u663e\u793a\u591a\u5f20\u56fe\u7247\u800c\u4e0d\u53ea\u662f\u4e24\u5f20\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u4f7f\u7528Matplotlib\u7684<code>subplots<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6307\u5b9a\u884c\u6570\u548c\u5217\u6570\u6765\u663e\u793a\u4efb\u610f\u6570\u91cf\u7684\u56fe\u7247\u3002\u4f8b\u5982\uff0c\u60f3\u8981\u663e\u793a\u56db\u5f20\u56fe\u7247\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">fig, axs = plt.subplots(2, 2)  # 2\u884c2\u5217\u7684\u5e03\u5c40\n\naxs[0, 0].imshow(img1)\naxs[0, 1].imshow(img2)\naxs[1, 0].imshow(img3)\naxs[1, 1].imshow(img4)\n\nfor ax in axs.flat:\n    ax.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74\n\nplt.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5c55\u793a\u591a\u5f20\u56fe\u7247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u540c\u65f6\u663e\u793a\u4e24\u5f20\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u501f\u52a9matplotlib\u3001PIL\uff08Pillo [&hellip;]","protected":false},"author":3,"featured_media":1108031,"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\/1108022"}],"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=1108022"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108022\/revisions"}],"predecessor-version":[{"id":1108034,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108022\/revisions\/1108034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108031"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}