{"id":204056,"date":"2025-05-29T11:54:15","date_gmt":"2025-05-29T03:54:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204056\/"},"modified":"2025-05-29T11:54:15","modified_gmt":"2025-05-29T03:54:15","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-python-%e4%b8%ad%e8%a3%81%e5%89%aa%e5%9b%be%e7%89%87%e5%b9%b6%e8%bd%ac%e6%8d%a2%e5%9d%90%e6%a0%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204056\/","title":{"rendered":"## \u5982\u4f55\u5728 Python \u4e2d\u88c1\u526a\u56fe\u7247\u5e76\u8f6c\u6362\u5750\u6807\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>## \u5982\u4f55\u5728 Python \u4e2d\u88c1\u526a\u56fe\u7247\u5e76\u8f6c\u6362\u5750\u6807\uff1f<\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729911295671c59ff3d93c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u88c1\u526a\u56fe\u7247\u53ca\u8f6c\u6362\u5750\u6807<\/strong><\/p>\n<p>\u5728python\u4e2d\u88c1\u526a\u56fe\u50cf\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u6216pillow\u5e93\u3002\u5c06\u56fe\u50cf\u88c1\u526a\u4e3a\u5b50\u56fe\u50cf\u540e\uff0c\u53ef\u4ee5\u76f8\u5bf9\u4e8e\u539f\u59cb\u56fe\u50cf\u6216\u5b50\u56fe\u50cf\u91cd\u65b0\u8ba1\u7b97\u6846\u7684\u5750\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528numpy\u88c1\u526a\u56fe\u50cf<\/strong><\/p>\n<pre>import numpy as np\nfrom pil import image\n\n# \u52a0\u8f7d\u56fe\u50cf\nimg = image.open(\"image.jpg\")\n\n# \u8f6c\u6362\u4e3anumpy\u6570\u7ec4\nimg_array = np.array(img)\n\n# \u88c1\u526a\u56fe\u50cf\nsub_img = img_array[y1:y2, x1:x2]\n\n# \u521b\u5efa\u4e00\u5f20\u65b0\u7684image\u5bf9\u8c61\nsub_img = image.fromarray(sub_img)<\/pre>\n<p><strong>\u4f7f\u7528pillow\u88c1\u526a\u56fe\u50cf<\/strong><\/p>\n<pre>from pil import image\n\n# \u52a0\u8f7d\u56fe\u50cf\nimg = image.open(\"image.jpg\")\n\n# \u88c1\u526a\u56fe\u50cf\nsub_img = img.crop((x1, y1, x2, y2))<\/pre>\n<p><strong>\u8f6c\u6362\u5750\u6807<\/strong><\/p>\n<p>\u5c06\u5b50\u56fe\u50cf\u4e2d\u7684\u5750\u6807\u8f6c\u6362\u4e3a\u539f\u59cb\u56fe\u50cf\u7684\u5750\u6807\uff0c\u65b9\u7a0b\u5982\u4e0b\uff1a<\/p>\n<pre>x_orig = x_sub * w_orig \/ w_sub\ny_orig = y_sub * h_orig \/ h_sub<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>x_orig \u548c y_orig \u662f\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u5750\u6807<\/li>\n<li>x_sub \u548c y_sub \u662f\u5b50\u56fe\u50cf\u4e2d\u7684\u5750\u6807<\/li>\n<li>w_orig \u548c h_orig \u662f\u539f\u59cb\u56fe\u50cf\u7684\u5bbd\u9ad8<\/li>\n<li>w_sub \u548c h_sub \u662f\u5b50\u56fe\u50cf\u7684\u5bbd\u9ad8<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre># \u5047\u8bbe\u539f\u56fe\u5bbd\u9ad8\u4e3a1000x1000\uff0c\u88c1\u526a\u51fa\u4e00\u4e2a\u5bbd\u9ad8\u4e3a250x250\u7684\u5b50\u56fe\nw_orig = 1000\nh_orig = 1000\nw_sub = 250\nh_sub = 250\n\n# \u5b50\u56fe\u4e2d\u7684\u6846\u5750\u6807\u4e3a(10, 20, 54, 44)\nx_sub = 10\ny_sub = 20\nx2_sub = 54\ny2_sub = 44\n\n# \u8f6c\u6362\u4e3a\u539f\u59cb\u56fe\u50cf\u5750\u6807\nx_orig = x_sub * w_orig \/ w_sub\ny_orig = y_sub * h_orig \/ h_sub\nx2_orig = x2_sub * w_orig \/ w_sub\ny2_orig = y2_sub * h_orig \/ h_sub\n\nprint(f\"\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u6846\u5750\u6807\uff1a(x1={x_orig}, y1={y_orig}, x2={x2_orig}, y2={y2_orig})\")<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u6846\u5750\u6807\uff1a(x1=25.0, y1=50.0, x2=135.0, y2=110.0)<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a## \u5982\u4f55\u5728 Python \u4e2d\u88c1\u526a\u56fe\u7247\u5e76\u8f6c\u6362\u5750\u6807\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>## \u5982\u4f55\u5728 Python \u4e2d\u88c1&#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-204056","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204056","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=204056"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204056\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}