{"id":163,"date":"2022-05-21T15:13:17","date_gmt":"2022-05-21T07:13:17","guid":{"rendered":"http:\/\/www.sangco.cn\/?p=163"},"modified":"2022-05-21T15:13:17","modified_gmt":"2022-05-21T07:13:17","slug":"%e6%9e%81%e9%aa%8c%e6%bb%91%e5%9d%97%e9%aa%8c%e8%af%81%e7%a0%811_%e5%8f%8d%e6%8b%bc_js%e5%92%8cpython%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.sangco.cn\/?p=163","title":{"rendered":"\u6781\u9a8c\u6ed1\u5757\u9a8c\u8bc1\u7801(1)_\u53cd\u62fc_JS\u548cPython\u4ee3\u7801"},"content":{"rendered":"<p>\u53cd\u62fcJS\u4ee3\u7801:<\/p>\n<pre class=\"brush:javascript\">function $e(e, t) {\r\n    \/\/ e\u4e3a\u5f85\u8fd8\u539f\u7684\u80cc\u666f\u56fe\u5bf9\u8c61\r\n    e = e[\"lKRC\"];\r\n    \/\/ t\u4e3acanvas\u56fe\u5f62\u5bb9\u5668\u5bf9\u8c61\r\n    t = t[\"lKRC\"];\r\n    \/\/ \u5c06\u539f\u56fe\u7684\u5bbd\u5ea6\u8d4b\u503c\u7ed9r\uff0c312\r\n    var r = e[\"width\"];\r\n    \/\/ \u5c06\u539f\u56fe\u7684\u9ad8\u5ea6\u8d4b\u503c\u7ed9n\uff0c160\r\n    var n = e[\"height\"];\r\n    \/\/ h\u4e3adocument\u5bf9\u8c61\uff0ci\u4e3acanvas\u56fe\u5f62\u5bb9\u5668\u5bf9\u8c61\r\n    var i = h[\"createElement\"](\"canvas\");\r\n    \/\/ \u5b9a\u4e49canvas\u56fe\u5f62\u5bb9\u5668i\u7684\u5bbd\u5ea6\u4e3a312\r\n    i[\"width\"] = r;\r\n    \/\/ \u5b9a\u4e49canvas\u56fe\u5f62\u5bb9\u5668i\u7684\u9ad8\u5ea6\u4e3a160\r\n    i[\"height\"] = n;\r\n    \/\/ o\u4e3a\u56fe\u5f62\u5bb9\u5668i\u7684\u4e8c\u7ef4\u7ed8\u56fe\u73af\u5883\r\n    var o = i[\"getContext\"](\"2d\");\r\n    \/\/ \u753b\u5e03o\u4e0a\u5b9a\u4f4d\u56fe\u50cfe\r\n    o[\"drawImage\"](e, 0, 0);\r\n    \/\/ a\u4e3a\u56fe\u5f62\u5bb9\u5668t\u7684\u4e8c\u7ef4\u7ed8\u56fe\u73af\u5883\r\n    var a = t[\"getContext\"](\"2d\");\r\n    \/\/ \u5b9a\u4e49canvas\u56fe\u5f62\u5bb9\u5668t\u7684\u9ad8\u5ea6\u4e3a160\r\n    t[\"height\"] = n;\r\n    \/\/ \u5b9a\u4e49canvas\u56fe\u5f62\u5bb9\u5668t\u7684\u5bbd\u5ea6\u4e3a260\r\n    t[\"width\"] = Ne;\r\n    \/\/ s = 80\r\n    var s = n \/ 2;\r\n    var u = 10;\r\n    \/\/ Ge\u4e3a\u6570\u7ec4\u5bf9\u8c61\uff0c\u5b9a\u503c\uff0c\u5176\u4e2d\u4e3a\u539f\u56fe\u7684\u8fd8\u539f\u987a\u5e8f\uff0c\u517152\u4e2a\u503c\uff0c\u4e3a0-51\u7684\u6574\u6570\u3002\u56e0\u6b64\u539f\u56fe\u88ab\u5207\u5272\u621052\u5757\u62fc\u56fe\uff0c\u6bcf\u5757\u62fc\u56fe\u5927\u5c0f\u4e3a12*80\u3002\r\n    \/\/ Ge = [39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43, 42, 12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17]\r\n    for (var c = 0; c &lt; 52; c = c + 1) {\r\n        \/\/ \u6bcf\u4e2a\u5c0f\u5757\u5728\u539f\u56fe\u5bbd\u5ea6\u4e0a\u7684\u76f8\u5bf9\u4f4d\u7f6e, \u6ce8\u610f\u6bcf\u4e2a\u5c0f\u5757\u7684\u95f4\u9694\u662f12\r\n        var _ = Ge[c] % 26 * 12 + 1;\r\n        \/\/ \u6bcf\u4e2a\u5c0f\u5757\u5728\u539f\u56fe\u9ad8\u5ea6\u4e0a\u7684\u76f8\u5bf9\u4f4d\u7f6e\r\n        var f = Ge[c] &gt; 25 ? s : 0;\r\n        \/\/ \u4ece\u539f\u56fe\u4e2d\u88c1\u526a\u51fa\u5bf9\u5e94\u7684\u5c0f\u5757, \u6ce8\u610f\u88c1\u526a\u56fe\u7247\u7684\u5bbd\u5ea6\u662f10\r\n        var l = o[\"getImageData\"](_, f, u, s);\r\n        \/\/ \u5c06\u88c1\u526a\u7684\u5c0f\u5757l, \u753b\u5165\u4e8c\u7ef4\u7ed8\u56fe\u73af\u5883a\r\n        a[\"putImageData\"](l, c % 26 * 10, c &gt; 25 ? s : 0);\r\n    }\r\n    \/\/ \u6ce8\u610f\u539f\u56fe\u4e3a312*160\u7684\u56fe\u7247\u5206\u5272\u621052\u4e2a\u5927\u5c0f\u4e3a12*80\u7684\u5c0f\u5757\uff0c\u6700\u540e\u753152\u4e2a10*80\u7684\u5c0f\u5757\u62fc\u63a5\u6210260*160\u5b8c\u6574\u56fe  \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53cd\u62fcPY\u4ee3\u7801<\/p>\n<pre class=\"brush:other\"># -*- coding: utf-8 -*-\r\nimport io\r\nfrom pathlib import Path\r\n\r\nfrom PIL import Image\r\n\r\n\r\ndef parse_bg_captcha(img, im_show=False, save_path=None):\r\n    \"\"\"\r\n    \u6ed1\u5757\u4e71\u5e8f\u80cc\u666f\u56fe\u8fd8\u539f\r\n    :param img: \u56fe\u7247\u8def\u5f84str\/\u56fe\u7247\u8def\u5f84Path\u5bf9\u8c61\/\u56fe\u7247\u4e8c\u8fdb\u5236\r\n        eg: 'assets\/bg.webp'\r\n            Path('assets\/bg.webp')\r\n    :param im_show: \u662f\u5426\u663e\u793a\u8fd8\u539f\u7ed3\u679c, &lt;type 'bool'&gt;; default: False\r\n    :param save_path: \u4fdd\u5b58\u8def\u5f84, &lt;type 'str'&gt;\/&lt;type 'Path'&gt;; default: None\r\n    :return: \u8fd8\u539f\u540e\u80cc\u666f\u56fe RGB\u56fe\u7247\u683c\u5f0f\r\n    \"\"\"\r\n    if isinstance(img, (str, Path)):\r\n        _img = Image.open(img)\r\n    elif isinstance(img, bytes):\r\n        _img = Image.open(io.BytesIO(img))\r\n    else:\r\n        raise ValueError(f'\u8f93\u5165\u56fe\u7247\u7c7b\u578b\u9519\u8bef, \u5fc5\u987b\u662f&lt;type str&gt;\/&lt;type Path&gt;\/&lt;type bytes&gt;: {type(img)}')\r\n    # \u56fe\u7247\u8fd8\u539f\u987a\u5e8f, \u5b9a\u503c\r\n    _Ge = [39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43,\r\n           42, 12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17]\r\n    w_sep, h_sep = 10, 80\r\n\r\n    # \u8fd8\u539f\u540e\u7684\u80cc\u666f\u56fe\r\n    new_img = Image.new('RGB', (260, 160))\r\n\r\n    for idx in range(len(_Ge)):\r\n        x = _Ge[idx] % 26 * 12 + 1\r\n        y = h_sep if _Ge[idx] &gt; 25 else 0\r\n        # \u4ece\u80cc\u666f\u56fe\u4e2d\u88c1\u526a\u51fa\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5c0f\u5757\r\n        img_cut = _img.crop((x, y, x + w_sep, y + h_sep))\r\n        # \u5c06\u5c0f\u5757\u62fc\u63a5\u5230\u65b0\u56fe\u4e2d\r\n        new_x = idx % 26 * 10\r\n        new_y = h_sep if idx &gt; 25 else 0\r\n        new_img.paste(img_cut, (new_x, new_y))\r\n\r\n    if im_show:\r\n        new_img.show()\r\n    if save_path is not None:\r\n        save_path = Path(save_path).resolve().__str__()\r\n        new_img.save(save_path)\r\n    return new_img\r\n\r\n\r\nif __name__ == '__main__':\r\n    parse_bg_captcha(\"bg.webp\", im_show=True, save_path='bg.jpg')\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53cd\u62fcJS\u4ee3\u7801: function $e(e, t) { \/\/ e\u4e3a\u5f85\u8fd8\u539f\u7684\u80cc\u666f\u56fe\u5bf9\u8c61 e = e[&#8220;lKRC&#8221;]; \/\/ t\u4e3acanvas\u56fe\u5f62\u5bb9\u5668\u5bf9\u8c61 t = t[&#8220;lKRC&#8221;]; \/\/ \u5c06\u539f\u56fe\u7684\u5bbd\u5ea6\u8d4b\u503c\u7ed9r\uff0c312 var r = e[&#8220;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"topic":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":1,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":164,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/164"}],"wp:attachment":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}