{"id":3809,"date":"2023-04-13T17:51:10","date_gmt":"2023-04-13T09:51:10","guid":{"rendered":"https:\/\/feizhaojun.com\/?p=3809"},"modified":"2023-04-13T17:51:10","modified_gmt":"2023-04-13T09:51:10","slug":"python%e5%8e%bb%e9%99%a4%e5%9b%be%e7%89%87%e5%9b%9b%e5%91%a8%e7%95%99%e7%99%bd","status":"publish","type":"post","link":"https:\/\/feizhaojun.com\/?p=3809","title":{"rendered":"Python\u53bb\u9664\u56fe\u7247\u56db\u5468\u7559\u767d"},"content":{"rendered":"<p>\u8fd1\u65e5\u9047\u5230\u4e00\u6279\u7c7b\u4f3c\u4e0b\u56fe\u7684\u5546\u54c1\u56fe\uff0c\u56fe\u7247\u4e2d\u4e3b\u8981\u5185\u5bb9\uff08\u76f8\u673a\uff09\u5360\u533a\u57df\u592a\u5c0f\uff0c\u5468\u56f4\u7684\u767d\u8272\u80cc\u666f\u533a\u57df\u592a\u5927\uff0c\u6211\u4eec\u5e0c\u671b\u901a\u8fc7\u88c1\u526a\uff0c\u8ba9\u5185\u5bb9\u5c3d\u91cf\u94fa\u6ee1\u56fe\u7247\uff0c\u51cf\u5c11\u5468\u56f4\u7684\u7559\u767d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.feizhaojun.com\/blog\/3809\/2023041301.jpg\" alt=\"image\" \/><\/p>\n<p>\u56e0\u4e3a\u4e2d\u95f4\u6709\u5185\u5bb9\u7684\u533a\u57df\uff0c\u5927\u5c0f\u662f\u4e0d\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u4e0d\u80fd\u4f7f\u7528\u6309<strong>\u7edf\u4e00\u6bd4\u4f8b<\/strong>\u88c1\u526a\u3002\u4f8b\u5982\u4e0b\u9762\u4e24\u5e45\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.feizhaojun.com\/blog\/3809\/2023041303.jpg\" alt=\"image\" \/><\/p>\n<p>\u6309\u7167\u5e38\u89c4\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u56fe\u7247\u4e2d\u7684\u6bcf\u4e00\u4e2a\u50cf\u7d20\u70b9\uff0c\u627e\u5230\u4f4d\u4e8e\u6700\u4e0a\u3001\u6700\u53f3\u3001\u6700\u4e0b\u3001\u6700\u5de6\u76844\u4e2a<strong>\u975e\u767d\u8272<\/strong>\u7684\u50cf\u7d20\u70b9\uff0c\u4ece\u800c\u7b97\u51fa\u88c1\u5207\u7684\u4f4d\u7f6e\u3002\u4f46\u662f\u8fd9\u6837\u505a\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u904d\u5386\u6240\u6709\u50cf\u7d20\u70b9\uff0c\u5982\u679c\u56fe\u7247\u5f88\u5927\uff0c\u8017\u65f6\u4f1a\u5f88\u4e45\u3002<\/li>\n<li>\u5982\u679c\u767d\u8272\u7559\u767d\u7684\u90e8\u5206\u5b58\u5728\u4e00\u4e9b\u566a\u70b9\uff0c\u4f1a\u5f71\u54cd\u5230\u88c1\u5207\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p>\u6211\u91c7\u7528\u4e86\u4e00\u4e2a\u6298\u4e2d\u7684\u601d\u8def\uff0c\u53ea\u68c0\u67e5\u56fe\u7247\u7684\u4e2d\u7ebf\u548c\u5bf9\u89d2\u7ebf\u4e0a\u7684\u70b9\uff0c\u5982\u4e0b\u56fe\u84dd\u8272\u8f68\u8ff9\u6240\u793a\uff0c\u901a\u8fc7\u904d\u5386\u84dd\u8272\u8f68\u8ff9\uff0c\u786e\u5b9a\u56fe\u4e2d\u7ea2\u8272\u5708\u51fa\u7684 6 \u4e2a\u70b9\uff0c\u6700\u540e\u901a\u8fc7\u8fd9 6 \u4e2a\u70b9\u786e\u5b9a\u88c1\u5207\u4f4d\u7f6e\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.feizhaojun.com\/blog\/3809\/2023041304.jpg\" alt=\"image\" \/><\/p>\n<p>\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff1a<\/p>\n<ol>\n<li>\u8ba1\u7b97\u91cf\u975e\u5e38\u5c11\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u5faa\u73af\u3002<\/li>\n<li>\u5047\u8bbe\u56fe\u4e2d A \u4f4d\u7f6e\u6709\u4e00\u4e2a\u566a\u70b9\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u3002<\/li>\n<\/ol>\n<p>\u4e0d\u8fc7\u8fd9\u6837\u505a\u4e5f\u4f1a\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u5982\u679c\u566a\u70b9\u4f4d\u4e8e\u84dd\u8272\u8f68\u8ff9\u4e0a\uff0c\u5c31\u65e0\u6cd5\u907f\u514d\u3002\u53e6\u5916\uff0c\u5982\u679c\u56fe\u7247\u5185\u5bb9\u4e0d\u662f\u5728\u6b63\u4e2d\uff0c\u800c\u662f\u5728\u4e0b\u56fe\u4e2d\u7ea2\u8272\u533a\u57df\uff08\u84dd\u8272\u8f68\u8ff9\u548c\u5185\u5bb9\u65e0\u6cd5\u786e\u5b9a 6 \u4e2a\u70b9\uff09\uff0c\u5c31\u4f1a\u5f71\u54cd\u88c1\u526a\u7684\u7ed3\u679c\u3002\u6211\u89c9\u5f97\u5bf9\u4e8e\u540e\u8005\uff0c\u5927\u5bb6\u53ef\u4ee5\u5728\u6211\u7684\u4ee3\u7801\u4e0a\u505a\u4e9b\u6539\u8fdb\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.feizhaojun.com\/blog\/3809\/2023041305.jpg\" alt=\"image\" \/><\/p>\n<p>\u8fd9\u662f\u793a\u4f8b\u56fe\u7684\u88c1\u5207\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.feizhaojun.com\/blog\/3809\/2023041306.jpg\" alt=\"image\" \/><\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw\nimport numpy as np\nimport os\n\nimgDir = r\".\/img\"  # \u539f\u59cb\u56fe\u7247\u76ee\u5f55\nside = 512  # \u8f93\u51fa\u8fb9\u957f\n\n\ndef min_not_zero(data):\n    new_data = [x for x in data if x != 0]\n    return min(new_data if len(new_data) != 0 else [0])\n\n\ni = 0\nfor file in os.listdir(imgDir):\n    print(file)\n    if not (file.endswith('.jpg') or file.endswith('.png')):\n        continue\n    path = os.path.join(imgDir, file)\n    img = Image.open(path)\n    imgArr = np.array(img)\n    height = imgArr.shape[0]\n    width = imgArr.shape[1]\n    gap = width * 0.02\n\n    draw = ImageDraw.Draw(img)\n\n    xStart = [width, width, width, width]\n    yStart = [height, height, height, height]\n    xEnd = [0, 0, 0, 0]\n    yEnd = [0, 0, 0, 0]\n    i += 1\n\n    length = min(width, height)\n\n    for x in range(length):\n        y = x  # \u6b63\u65b9\u5f62\u56fe\u7247\u53ef\u4ee5\u76f4\u63a5\u76f8\u7b49\n        # \u5148\u7b80\u5355\u5904\u7406\u975e\u6b63\u65b9\u5f62\n        if x &gt; height:\n            y = height - 1\n\n        # \u5de6\u4e0a\u5230\u53f3\u4e0b\n        if imgArr[y][x][0] &lt; 255 or imgArr[y][x][1] &lt; 255 or imgArr[y][x][2] &lt; 255:\n            if xStart[0] &gt; x:\n                xStart[0] = x\n            if yStart[0] &gt; y:\n                yStart[0] = y\n            if x &gt; xEnd[0]:\n                xEnd[0] = x\n            if y &gt; yEnd[0]:\n                yEnd[0] = y\n        # \u5de6\u4e0b\u5230\u53f3\u4e0a\n        if imgArr[height - y - 1][x][0] &lt; 255 or imgArr[height - y - 1][x][1] &lt; 255 or imgArr[height - y - 1][x][2] &lt; 255:\n            if xStart[1] &gt; x:\n                xStart[1] = x\n            if yStart[1] &gt; height - y:\n                yStart[1] = height - y\n            if x &gt; xEnd[1]:\n                xEnd[1] = x\n            if yEnd[1] == 0:\n                yEnd[1] = height - y\n        # \u4e2d\u6a2a\u7ebf\n        if imgArr[int(height \/ 2)][x][0] &lt; 255 or imgArr[int(height \/ 2)][x][1] &lt; 255 or imgArr[int(height \/ 2)][x][2] &lt; 255:\n            if xStart[2] &gt; x:\n                xStart[2] = x\n            if x &gt; xEnd[2]:\n                xEnd[2] = x\n        # \u4e2d\u7ad6\u7ebf\n        if imgArr[y][int(width \/ 2)][0] &lt; 255 or imgArr[y][int(width \/ 2)][1] &lt; 255 or imgArr[y][int(width \/ 2)][2] &lt; 255:\n            if yStart[3] &gt; y:\n                yStart[3] = y\n            if y &gt; yEnd[3]:\n                yEnd[3] = y\n    xS = min_not_zero(xStart)\n    xE = max(xEnd)\n    yS = min_not_zero(yStart)\n    yE = max(yEnd)\n    if xS == width:\n        xS = 0\n    if yS == height:\n        yS = 0\n    if xE == 0:\n        xE = width\n    if yE == 0:\n        yE = height\n    # \u88c1\u5207\u6b63\u65b9\u5f62\uff0c\u53d6 x y \u5e76\u96c6\n    start = min(xS, yS)\n    end = max(xE, yE)\n    xS = start\n    yS = start\n    xE = end\n    yE = end\n    cropped = img.crop((xS - (gap if xS &gt; gap else xS), yS - (gap if yS &gt; gap else yS), xE + (gap if width - xE &gt; gap else width - xE), yE + (gap if height - yE &gt; gap else height - yE))).resize((side, side))\n    cropped.save(r\".\/temp\/{}.jpg\".format(file[:-4], i))  # \u8f93\u51fa\u56fe\u7247\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd1\u65e5\u9047\u5230\u4e00\u6279\u7c7b\u4f3c\u4e0b\u56fe\u7684\u5546\u54c1\u56fe\uff0c\u56fe\u7247\u4e2d\u4e3b\u8981\u5185\u5bb9\uff08\u76f8\u673a\uff09\u5360\u533a\u57df\u592a\u5c0f\uff0c\u5468\u56f4\u7684\u767d\u8272\u80cc\u666f\u533a\u57df\u592a\u5927\uff0c\u6211\u4eec\u5e0c\u671b\u901a\u8fc7\u88c1\u526a\uff0c\u8ba9\u5185\u5bb9\u5c3d\u91cf\u94fa\u6ee1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3809","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/3809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3809"}],"version-history":[{"count":1,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/3809\/revisions"}],"predecessor-version":[{"id":3810,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/3809\/revisions\/3810"}],"wp:attachment":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}