{"id":1104096,"date":"2025-01-08T16:17:10","date_gmt":"2025-01-08T08:17:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104096.html"},"modified":"2025-01-08T16:17:16","modified_gmt":"2025-01-08T08:17:16","slug":"python%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e5%9b%9b%e4%b8%aa%e7%82%b9%e5%88%87%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104096.html","title":{"rendered":"python\u5982\u4f55\u6839\u636e\u56db\u4e2a\u70b9\u5207\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065624\/7f7d64c7-d02c-4672-b4ec-2a3cfdd22d30.webp\" alt=\"python\u5982\u4f55\u6839\u636e\u56db\u4e2a\u70b9\u5207\u56fe\" \/><\/p>\n<p><p> <strong>Python\u6839\u636e\u56db\u4e2a\u70b9\u5207\u56fe\u7684\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93Pillow\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528\u591a\u8fb9\u5f62\u88c1\u526a\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u56db\u4e2a\u70b9\u5207\u56fe\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pillow\u5e93<\/h3>\n<\/p>\n<p><p>Python\u7684Pillow\u5e93\uff08PIL\uff09\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff0c\u5305\u62ec\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u7b49\u3002\u5bf9\u4e8e\u56db\u4e2a\u70b9\u7684\u88c1\u526a\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Image.crop()<\/code> \u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u56db\u4e2a\u70b9\u7684\u5750\u6807\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def crop_image_by_points(image_path, points):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u6839\u636e\u56db\u4e2a\u70b9\u88c1\u526a\u56fe\u50cf<\/p>\n<p>    :param image_path: \u56fe\u50cf\u8def\u5f84<\/p>\n<p>    :param points: \u56db\u4e2a\u70b9\u5750\u6807\uff0c\u683c\u5f0f\u4e3a[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]<\/p>\n<p>    :return: \u88c1\u526a\u540e\u7684\u56fe\u50cf<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    image = Image.open(image_path)<\/p>\n<p>    # \u786e\u5b9a\u88c1\u526a\u533a\u57df\u7684\u8fb9\u754c\u6846<\/p>\n<p>    x_min = min(point[0] for point in points)<\/p>\n<p>    x_max = max(point[0] for point in points)<\/p>\n<p>    y_min = min(point[1] for point in points)<\/p>\n<p>    y_max = max(point[1] for point in points)<\/p>\n<p>    # \u88c1\u526a\u56fe\u50cf<\/p>\n<p>    cropped_image = image.crop((x_min, y_min, x_max, y_max))<\/p>\n<p>    return cropped_image<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>image_path = &#39;example.jpg&#39;<\/p>\n<p>points = [(50, 50), (150, 50), (150, 150), (50, 150)]<\/p>\n<p>cropped_image = crop_image_by_points(image_path, points)<\/p>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>OpenCV\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u5e93\u8fdb\u884c\u56db\u4e2a\u70b9\u88c1\u526a\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>def crop_image_by_points(image_path, points):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u6839\u636e\u56db\u4e2a\u70b9\u88c1\u526a\u56fe\u50cf<\/p>\n<p>    :param image_path: \u56fe\u50cf\u8def\u5f84<\/p>\n<p>    :param points: \u56db\u4e2a\u70b9\u5750\u6807\uff0c\u683c\u5f0f\u4e3a[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]<\/p>\n<p>    :return: \u88c1\u526a\u540e\u7684\u56fe\u50cf<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    image = cv2.imread(image_path)<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u63a9\u819c<\/p>\n<p>    mask = np.zeros(image.shape[:2], dtype=np.uint8)<\/p>\n<p>    points = np.array([points], dtype=np.int32)<\/p>\n<p>    cv2.fillPoly(mask, points, 255)<\/p>\n<p>    # \u5e94\u7528\u63a9\u819c<\/p>\n<p>    masked_image = cv2.bitwise_and(image, image, mask=mask)<\/p>\n<p>    # \u63d0\u53d6\u88c1\u526a\u533a\u57df<\/p>\n<p>    x, y, w, h = cv2.boundingRect(points)<\/p>\n<p>    cropped_image = masked_image[y:y+h, x:x+w]<\/p>\n<p>    return cropped_image<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>image_path = &#39;example.jpg&#39;<\/p>\n<p>points = [(50, 50), (150, 50), (150, 150), (50, 150)]<\/p>\n<p>cropped_image = crop_image_by_points(image_path, points)<\/p>\n<p>cv2.imshow(&#39;Cropped Image&#39;, cropped_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><h3>\u4f7f\u7528\u591a\u8fb9\u5f62\u88c1\u526a<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u89c4\u5219\u56db\u8fb9\u5f62\u7684\u88c1\u526a\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u8fb9\u5f62\u88c1\u526a\u7684\u65b9\u6cd5\u3002\u8fd9\u9700\u8981\u4f7f\u7528OpenCV\u5e93\u7684 <code>cv2.getPerspectiveTransform<\/code> \u548c <code>cv2.warpPerspective<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>def crop_image_by_polygon(image_path, points):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u6839\u636e\u56db\u4e2a\u70b9\u88c1\u526a\u56fe\u50cf<\/p>\n<p>    :param image_path: \u56fe\u50cf\u8def\u5f84<\/p>\n<p>    :param points: \u56db\u4e2a\u70b9\u5750\u6807\uff0c\u683c\u5f0f\u4e3a[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]<\/p>\n<p>    :return: \u88c1\u526a\u540e\u7684\u56fe\u50cf<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    image = cv2.imread(image_path)<\/p>\n<p>    height, width = image.shape[:2]<\/p>\n<p>    # \u5b9a\u4e49\u76ee\u6807\u56fe\u50cf\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/p>\n<p>    target_width = max(np.linalg.norm(np.array(points[0]) - np.array(points[1])),<\/p>\n<p>                       np.linalg.norm(np.array(points[2]) - np.array(points[3])))<\/p>\n<p>    target_height = max(np.linalg.norm(np.array(points[0]) - np.array(points[3])),<\/p>\n<p>                        np.linalg.norm(np.array(points[1]) - np.array(points[2])))<\/p>\n<p>    target_points = np.array([<\/p>\n<p>        [0, 0],<\/p>\n<p>        [target_width - 1, 0],<\/p>\n<p>        [target_width - 1, target_height - 1],<\/p>\n<p>        [0, target_height - 1]<\/p>\n<p>    ], dtype=&quot;float32&quot;)<\/p>\n<p>    # \u83b7\u53d6\u53d8\u6362\u77e9\u9635\u5e76\u8fdb\u884c\u900f\u89c6\u53d8\u6362<\/p>\n<p>    matrix = cv2.getPerspectiveTransform(np.array(points, dtype=&quot;float32&quot;), target_points)<\/p>\n<p>    cropped_image = cv2.warpPerspective(image, matrix, (int(target_width), int(target_height)))<\/p>\n<p>    return cropped_image<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>image_path = &#39;example.jpg&#39;<\/p>\n<p>points = [(50, 50), (150, 50), (150, 150), (50, 150)]<\/p>\n<p>cropped_image = crop_image_by_polygon(image_path, points)<\/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>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528Pillow\u5e93\u548cOpenCV\u5e93\uff0c\u6839\u636e\u56db\u4e2a\u70b9\u5750\u6807\u88c1\u526a\u56fe\u50cf\u3002Pillow\u5e93\u9002\u7528\u4e8e\u7b80\u5355\u7684\u77e9\u5f62\u88c1\u526a\uff0c\u800cOpenCV\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u80fd\u591f\u5904\u7406\u4e0d\u89c4\u5219\u7684\u56db\u8fb9\u5f62\u88c1\u526a\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6839\u636e\u56db\u4e2a\u70b9\u5207\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u548cPIL\u7b49\u5e93\u6765\u6839\u636e\u56db\u4e2a\u70b9\u8fdb\u884c\u56fe\u50cf\u5207\u5272\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u5b9a\u56db\u4e2a\u70b9\u7684\u5750\u6807\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u7684\u987a\u5e8f\u662f\u6b63\u786e\u7684\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5750\u6807\u521b\u5efa\u4e00\u4e2a\u591a\u8fb9\u5f62\uff0c\u5e76\u4f7f\u7528\u88c1\u526a\u65b9\u6cd5\u4ece\u539f\u56fe\u4e2d\u63d0\u53d6\u51fa\u8fd9\u4e2a\u533a\u57df\u3002<\/p>\n<p><strong>\u5728\u5207\u56fe\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u56fe\u50cf\u7684\u5750\u6807\u7cfb\uff1f<\/strong><br \/>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u5750\u6807\u7cfb\u7684\u539f\u70b9\u901a\u5e38\u4f4d\u4e8e\u5de6\u4e0a\u89d2\uff0cX\u8f74\u5411\u53f3\uff0cY\u8f74\u5411\u4e0b\u3002\u56e0\u6b64\uff0c\u5728\u63d0\u4f9b\u56db\u4e2a\u70b9\u7684\u5750\u6807\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u5b83\u4eec\u7b26\u5408\u8fd9\u79cd\u5750\u6807\u7cfb\u7edf\u3002\u8fd9\u610f\u5473\u7740\u5de6\u4e0a\u89d2\u7684\u70b9\u5750\u6807\u5e94\u4e3a (x1, y1)\uff0c\u800c\u53f3\u4e0b\u89d2\u7684\u70b9\u5750\u6807\u5e94\u4e3a (x2, y2)\u3002\u786e\u4fdd\u5750\u6807\u987a\u5e8f\u7684\u6b63\u786e\u6027\u5bf9\u4e8e\u51c6\u786e\u88c1\u526a\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5207\u56fe\u540e\u5982\u4f55\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u5207\u56fe\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u7684<code>save<\/code>\u65b9\u6cd5\u6216OpenCV\u7684<code>imwrite<\/code>\u51fd\u6570\u6765\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\u3002\u786e\u4fdd\u6307\u5b9a\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\u548c\u683c\u5f0f\uff0c\u5982JPEG\u6216PNG\u7b49\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u5b58\u50a8\u65f6\u53ef\u4ee5\u8003\u8651\u6587\u4ef6\u7684\u538b\u7f29\u7387\u548c\u8d28\u91cf\uff0c\u4ee5\u5e73\u8861\u56fe\u50cf\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<p><strong>\u5207\u56fe\u65f6\u5982\u4f55\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u56fe\u50cf\u5207\u5272\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u7ed9\u5b9a\u7684\u70b9\u8d85\u51fa\u56fe\u50cf\u8fb9\u754c\u6216\u70b9\u7684\u987a\u5e8f\u4e0d\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u6765\u68c0\u67e5\u8fd9\u4e9b\u60c5\u51b5\uff0c\u5e76\u5728\u9047\u5230\u95ee\u9898\u65f6\u8fd4\u56de\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7a33\u5065\u5730\u8fd0\u884c\uff0c\u907f\u514d\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6839\u636e\u56db\u4e2a\u70b9\u5207\u56fe\u7684\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93Pillow\u3001\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528\u591a\u8fb9\u5f62\u88c1\u526a\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":1104103,"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\/1104096"}],"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=1104096"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104096\/revisions"}],"predecessor-version":[{"id":1104106,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104096\/revisions\/1104106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104103"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}