{"id":1569,"date":"2021-06-17T19:26:00","date_gmt":"2021-06-17T11:26:00","guid":{"rendered":"http:\/\/www.yatenglg.cn\/?p=1569"},"modified":"2022-10-18T16:34:39","modified_gmt":"2022-10-18T08:34:39","slug":"%e5%9b%be%e7%89%87%e5%ae%8c%e6%95%b4%e6%80%a7%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=1569","title":{"rendered":"\u56fe\u7247\u5b8c\u6574\u6027\u9a8c\u8bc1"},"content":{"rendered":"\n<p>\u901a\u8fc7\u6570\u636e\u6d41\u4e0b\u8f7d\u7684\u56fe\u7247\uff0c\u5f80\u5f80\u4f1a\u7531\u4e8e\u7f51\u7edc\u539f\u56e0\u7b49\uff0c\u9020\u6210\u4e0b\u8f7d\u56fe\u7247\u4e0d\u5b8c\u6574\u3002<\/p>\n\n\n\n<p>\u4e0b\u4f8b\u4ee3\u7801\uff0c\u5b9e\u73b0\u4e86jpg\uff0cpng\u683c\u5f0f\u56fe\u7247\u7684\u5b8c\u6574\u6027\u9a8c\u8bc1\u3002\u901a\u8fc7\u7ee7\u627fBaseInspector\uff0c\u53ef\u65b9\u4fbf\u5730\u6269\u5c55\u81f3\u5176\u4ed6\u7c7b\u578b\u56fe\u7247\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-python\" data-file=\"IsVaildImg.py\" data-lang=\"Python\"><code># -*- coding: utf-8 -*-\n# @Author  : LG\n\n&quot;&quot;&quot;\n    \u56fe\u7247\u5b8c\u6574\u6027\u68c0\u67e5\n    \u53ef\u901a\u8fc7\u7ee7\u627fBaseInspector\u6269\u5c55\u5176\u4ed6\u7c7b\u578b\u56fe\u7247\u53ca\u6587\u4ef6\u7684\u68c0\u67e5\n&quot;&quot;&quot;\n\nimport os\nfrom typing import Tuple\n\n\nclass BaseInspector:\n    def __init__(self, suffix:str, start_end_char:Tuple[str, str]):\n        self.suffix = suffix    # \u6587\u4ef6\u540e\u7f00\n        self.start_end_char = start_end_char    # \u8d77\u59cb\u7b26\u4e0e\u7ed3\u675f\u7b26\n\n    def is_valid(self, path):\n        raise NotImplementedError\n\n\nclass JpgInspector(BaseInspector):\n    def __init__(self, suffix = &#39;jpg&#39;, start_end_char = (b&#39;\\xff\\xd8&#39;, b&#39;\\xff\\xd9&#39;)):\n        super(JpgInspector, self).__init__(suffix, start_end_char)\n        self.end_char_len = len(self.start_end_char[1])\n\n    def is_valid(self, path):\n        with open(path, &#39;rb&#39;) as f:\n            f.seek(-self.end_char_len, 2)  # \u5c06\u6587\u4ef6\u8bfb\u53d6\u6307\u9488\u6307\u5b9a\u5230\u5012\u6570\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\n            return f.read() == self.start_end_char[1]  # jpg\u6587\u4ef6\u7ed3\u675f\u7b26\u662f\\xff\\xd9\n\n\nclass PngInspector(BaseInspector):\n    def __init__(self, suffix = &#39;png&#39;,\n                 start_end_char = (b&#39;\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A&#39;,\n                                   b&#39;\\x00\\x00\\x00\\x00\\x49\\x45\\x4E\\x44\\xAE\\x42\\x60\\x82&#39;)):\n        super(PngInspector, self).__init__(suffix, start_end_char)\n        self.end_char_len = len(self.start_end_char[1])\n\n    def is_valid(self, path):\n        with open(path, &#39;rb&#39;) as f:\n            f.seek(-self.end_char_len, 2)\n            return f.read() == self.start_end_char[1]  # png\u6587\u4ef6\u7ed3\u675f\u7b26\n\n\nclass Inspector(object):\n    def __init__(self):\n        self.fns = {}   # \u68c0\u6d4b\u65b9\u6cd5\uff0c\u901a\u8fc7register\u6ce8\u518c\u5230\u68c0\u6d4b\u5668\n        self.invalid_num = 0    # \u8bb0\u5f55\u65e0\u6548\u56fe\u7247\u6570\u91cf\n        self.root = &#39;&#39;  # \u7edd\u5bf9\u8def\u5f84\u76ee\u5f55\n\n    def register(self, fn:BaseInspector):\n        self.fns[fn.suffix] = fn\n\n    def one_img_inspect(self, path:str):\n        suffix = os.path.splitext(path)[1].lower().lstrip(&#39;.&#39;)\n        if suffix in self.fns:\n            if not self.fns[suffix].is_valid(path):\n                self.invalid_num += 1\n                print(&#39;{}&#39;.format(os.path.relpath(path, self.root)))\n        else:\n            pass\n\n    def root_inspect(self, root:str):\n        fs = os.listdir(root)\n        for f in fs:\n            f = os.path.join(root, f)\n            if os.path.isfile(f):\n                self.one_img_inspect(f)\n            if os.path.isdir(f):\n                self.root_inspect(f)\n\n    def run(self, path):\n\n        if os.path.isfile(path):\n            self.root = os.path.split(path)[0]\n            self.one_img_inspect(path)\n\n        if os.path.isdir(path):\n            self.root = path\n            self.root_inspect(path)\n\n        print(&#39;Found invaild file {}.&#39;.format(self.invalid_num))\n\n\nif __name__ == &#39;__main__&#39;:\n\n    import argparse\n\n    parse = argparse.ArgumentParser(description=&#39;\u56fe\u7247\u5b8c\u6574\u6027\u68c0\u67e5&#39;)\n    parse.add_argument(&#39;--path&#39;, type=str, required=True, help=&#39;\u56fe\u7247\u8def\u5f84\u6216\u6839\u76ee\u5f55&#39;)\n    parse.add_argument(&#39;--types&#39;, type=str, default=[&#39;jpg&#39;, &#39;png&#39;], nargs=&#39;+&#39;, help=&#39;\u6587\u4ef6\u540e\u7f00(\u5c0f\u5199)&#39;)\n    args = parse.parse_args()\n\n    #\n    inspector_types = {&#39;jpg&#39;: JpgInspector(),\n                       &#39;png&#39;: PngInspector()}\n\n    print(&quot;Now support format: {}&quot;.format(&#39; | &#39;.join(inspector_types.keys())))\n\n    inspector = Inspector() # \u68c0\u67e5\u5668\n    for t in args.types:\n        if t in inspector_types:\n            inspector.register(inspector_types[t])\n            print(&#39;[{}] inspector have registed&#39;.format(t))\n\n        else:\n            print(&#39;Not support {} file.&#39;.format(t))\n\n    inspector.run(args.path)\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u6570\u636e\u6d41\u4e0b\u8f7d\u7684\u56fe\u7247\uff0c\u5f80\u5f80\u4f1a\u7531\u4e8e\u7f51\u7edc\u539f\u56e0\u7b49\uff0c\u9020\u6210\u4e0b\u8f7d\u56fe\u7247\u4e0d\u5b8c\u6574\u3002 \u4e0b\u4f8b\u4ee3\u7801\uff0c\u5b9e\u73b0\u4e86jpg\uff0cpng\u683c\u5f0f\u56fe\u7247\u7684\u5b8c\u6574&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1569"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1569"}],"version-history":[{"count":5,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":1586,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions\/1586"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}