{"id":204204,"date":"2025-05-29T09:43:51","date_gmt":"2025-05-29T01:43:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204204\/"},"modified":"2025-05-29T09:43:51","modified_gmt":"2025-05-29T01:43:51","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e8%a3%81%e5%89%aa%e5%9b%be%e7%89%87%e5%b9%b6%e6%9b%b4%e6%96%b0%e5%8e%9f%e5%9b%be%e4%b8%ad%e7%9a%84%e5%9d%90%e6%a0%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204204\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u88c1\u526a\u56fe\u7247\u5e76\u66f4\u65b0\u539f\u56fe\u4e2d\u7684\u5750\u6807\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u88c1\u526a\u56fe\u7247\u5e76\u66f4\u65b0\u539f\u56fe\u4e2d\u7684\u5750\u6807\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/173038647067239a265f324.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u88c1\u526a\u56fe\u7247\u5e76\u66f4\u65b0\u539f\u56fe\u5750\u6807<\/strong><\/p>\n<p><strong>\u95ee\u9898<\/strong><\/p>\n<p>\u5982\u4f55\u5728 python \u4e2d\u88c1\u526a\u56fe\u7247\uff0c\u5e76\u5728\u88c1\u526a\u540e\u66f4\u65b0\u539f\u56fe\u4e2d\u7684\u5750\u6807\uff1f<\/p>\n<p><strong>\u56de\u7b54<\/strong><\/p>\n<p><strong>\u4f7f\u7528 opencv \u88c1\u526a\u56fe\u7247<\/strong><\/p>\n<p>1.\u4f7f\u7528 numpy \u5207\u7247\u5bf9\u56fe\u7247\u8fdb\u884c\u88c1\u526a\uff1aimg[y1:y2, x1:x2].copy()\u3002<br \/>2.\u4e5f\u53ef\u4ee5\u4f7f\u7528 numpy \u7684 numpy.hsplit \u51fd\u6570\u5206\u5272\u56fe\u7247\uff0c\u524d\u63d0\u662f\u5bbd\u9ad8\u53ef\u4ee5\u5e73\u5206\u3002<\/p>\n<p><strong>\u66f4\u65b0\u539f\u56fe\u5750\u6807<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5728\u5c0f\u56fe\u4e2d\u7ed8\u5236\u7684\u77e9\u5f62\uff1a<\/p>\n<ul>\n<li>\u5728\u5c0f\u56fe\u4e2d\u7684\u5750\u6807\uff1a(x1, y1, x2, y2)<\/li>\n<li>\u5728\u539f\u56fe\u4e2d\u7684\u5750\u6807\uff1a(x1 + (\u5c0f\u56fe\u5de6\u4e0a\u89d2x\u5750\u6807 <em> \u5c0f\u56fe\u5bbd\u5ea6\u6bd4\u4f8b)\uff0cy1 + (\u5c0f\u56fe\u5de6\u4e0a\u89d2y\u5750\u6807 <\/em> \u5c0f\u56fe\u9ad8\u5ea6\u6bd4\u4f8b)\uff0cx2 + (\u5c0f\u56fe\u53f3\u4e0a\u89d2x\u5750\u6807 <em> \u5c0f\u56fe\u5bbd\u5ea6\u6bd4\u4f8b)\uff0cy2 + (\u5c0f\u56fe\u53f3\u4e0a\u89d2y\u5750\u6807 <\/em> \u5c0f\u56fe\u9ad8\u5ea6\u6bd4\u4f8b))<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>import cv2\nimport numpy as np\n\n# \u8bfb\u5165\u56fe\u7247\nimg = cv2.imread('image.jpg')\n\n# \u5206\u5272\u56fe\u7247\nh, w = img.shape[:2]\nnrows = 4\nncols = 4\nsub_h, sub_w = h\/\/nrows, w\/\/ncols\nsub_imgs = np.array(np.hsplit(np.array(np.hsplit(img, ncols)), nrows))\n\n# \u7ed9\u5c0f\u56fe\u586b\u5145\u989c\u8272\nsub_imgs[2, 3, 10:44, 10:54] = (0, 0, 255)\n# \u66f4\u65b0\u539f\u56fe\u5750\u6807\nx1, y1 = sub_w * 3 + 10, sub_h * 2 + 10\nx2, y2 = sub_w * 3 + 54, sub_h * 2 + 44\nimg[y1:y2, x1:x2] = (0, 0, 255)\n\n# \u663e\u793a\u56fe\u7247\ncv2.imshow('original image', img)\ncv2.imshow('split image', sub_imgs)\ncv2.waitkey(0)\ncv2.destroyallwindows()<\/pre>\n<p><strong>\u4f7f\u7528 pillow \u88c1\u526a\u56fe\u7247<\/strong><\/p>\n<p>1.\u4f7f\u7528 image \u7684 crop \u65b9\u6cd5\u88c1\u526a\u56fe\u7247\u3002<br \/>2.\u6839\u636e\u88c1\u526a\u533a\u57df\u7684\u6bd4\u4f8b\u8f6c\u6362\u539f\u56fe\u4e2d\u7684\u5750\u6807\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>from PIL import Image\n\n# \u8bfb\u5165\u56fe\u7247\nimg = Image.open('image.jpg')\n\n# \u5206\u5272\u56fe\u7247\nnRows = 3\nnCols = 3\nsub_imgs = split_image(img, nCols, nRows)\nsubboxs = split_box((0, 0, *img.size), nCols, nRows)\n\n# \u7ed9\u5c0f\u56fe\u586b\u5145\u989c\u8272\nsub_imgs[1][2].paste((255, 0, 0), (10, 10, 20, 20))\n# \u66f4\u65b0\u539f\u56fe\u5750\u6807\nsubx, suby = subboxs[1][2][:2]\nimg.paste((255, 0, 0), (subx+10, suby+10, subx+20, suby+20))\n\n# \u663e\u793a\u56fe\u7247\nimg.show()\nsplit_img.show()<\/pre>\n<p>\u5176\u4e2d\uff0csplit_box \u548c split_image \u51fd\u6570\u53ef\u5206\u522b\u7528\u4e8e\u751f\u6210\u88c1\u526a\u533a\u57df\u548c\u5206\u5272\u56fe\u7247\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u88c1\u526a\u56fe\u7247\u5e76\u66f4\u65b0\u539f\u56fe\u4e2d\u7684\u5750\u6807\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u88c1\u526a\u56fe\u7247&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204204","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204204"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204204\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}