{"id":1166,"date":"2022-07-06T16:17:37","date_gmt":"2022-07-06T08:17:37","guid":{"rendered":"http:\/\/175.24.184.226\/?p=1166"},"modified":"2022-07-06T16:17:37","modified_gmt":"2022-07-06T08:17:37","slug":"python-%e5%9b%be%e7%89%87%e5%8e%8b%e7%bc%a9","status":"publish","type":"post","link":"\/?p=1166","title":{"rendered":"python \u56fe\u7247\u538b\u7f29"},"content":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/www.it610.com\/article\/1296767807555444736.htm\">https:\/\/www.it610.com\/article\/1296767807555444736.htm<\/a><br \/>\npython\u4e0d\u6539\u53d8\u56fe\u7247\u5c3a\u5bf8\u538b\u7f29\u5230\u6307\u5b9a\u5927\u5c0f<\/p>\n<pre><code class=\"language-python\">import base64\nimport io\nimport os\nfrom PIL import Image\nfrom PIL import ImageFile\n\n# \u538b\u7f29\u56fe\u7247\u6587\u4ef6\ndef compress_image(outfile, mb=190, quality=85, k=0.9):\n    &quot;&quot;&quot;\u4e0d\u6539\u53d8\u56fe\u7247\u5c3a\u5bf8\u538b\u7f29\u5230\u6307\u5b9a\u5927\u5c0f\n    :param outfile: \u538b\u7f29\u6587\u4ef6\u4fdd\u5b58\u5730\u5740\n    :param mb: \u538b\u7f29\u76ee\u6807\uff0cKB\n    :param step: \u6bcf\u6b21\u8c03\u6574\u7684\u538b\u7f29\u6bd4\u7387\n    :param quality: \u521d\u59cb\u538b\u7f29\u6bd4\u7387\n    :return: \u538b\u7f29\u6587\u4ef6\u5730\u5740\uff0c\u538b\u7f29\u6587\u4ef6\u5927\u5c0f\n    &quot;&quot;&quot;\n\n    o_size = os.path.getsize(outfile) \/\/ 1024\n    print(o_size, mb)\n    if o_size &lt;= mb:\n        return outfile\n\n    ImageFile.LOAD_TRUNCATED_IMAGES = True\n    while o_size &gt; mb:\n        im = Image.open(outfile)\n        x, y = im.size\n        out = im.resize((int(x*k), int(y*k)), Image.ANTIALIAS)\n        try:\n            out.save(outfile, quality=quality)\n        except Exception as e:\n            print(e)\n            break\n        o_size = os.path.getsize(outfile) \/\/ 1024\n    return outfile\n\n# \u538b\u7f29base64\u7684\u56fe\u7247\ndef compress_image_bs4(b64, mb=190, k=0.9):\n    &quot;&quot;&quot;\u4e0d\u6539\u53d8\u56fe\u7247\u5c3a\u5bf8\u538b\u7f29\u5230\u6307\u5b9a\u5927\u5c0f\n    :param outfile: \u538b\u7f29\u6587\u4ef6\u4fdd\u5b58\u5730\u5740\n    :param mb: \u538b\u7f29\u76ee\u6807\uff0cKB\n    :param step: \u6bcf\u6b21\u8c03\u6574\u7684\u538b\u7f29\u6bd4\u7387\n    :param quality: \u521d\u59cb\u538b\u7f29\u6bd4\u7387\n    :return: \u538b\u7f29\u6587\u4ef6\u5730\u5740\uff0c\u538b\u7f29\u6587\u4ef6\u5927\u5c0f\n    &quot;&quot;&quot;\n    f = base64.b64decode(b64)\n    with io.BytesIO(f) as im:\n        o_size = len(im.getvalue()) \/\/ 1024\n        if o_size &lt;= mb:\n            return b64\n        im_out = im\n        while o_size &gt; mb:\n            img = Image.open(im_out)\n            x, y = img.size\n            out = img.resize((int(x*k), int(y*k)), Image.ANTIALIAS)\n            im_out.close()\n            im_out = io.BytesIO()\n            out.save(im_out, &#039;jpeg&#039;)\n            o_size = len(im_out.getvalue()) \/\/ 1024\n        b64 = base64.b64encode(im_out.getvalue())\n        im_out.close()\n        return str(b64, encoding=&#039;utf8&#039;)<\/code><\/pre>\n<p>\u6211\u7684\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-python\">def compress_image_bs4(b64, mb=23, k=0.9):\n    &quot;&quot;&quot;\u4e0d\u6539\u53d8\u56fe\u7247\u5c3a\u5bf8\u538b\u7f29\u5230\u6307\u5b9a\u5927\u5c0f\n    :param outfile: \u538b\u7f29\u6587\u4ef6\u4fdd\u5b58\u5730\u5740\n    :param mb: \u538b\u7f29\u76ee\u6807\uff0cKB\n    :param step: \u6bcf\u6b21\u8c03\u6574\u7684\u538b\u7f29\u6bd4\u7387\n    :param quality: \u521d\u59cb\u538b\u7f29\u6bd4\u7387\n    :return: \u538b\u7f29\u6587\u4ef6\u5730\u5740\uff0c\u538b\u7f29\u6587\u4ef6\u5927\u5c0f\n    &quot;&quot;&quot;\n    f = base64.b64decode(b64)\n    with io.BytesIO(f) as im:\n        o_size = len(im.getvalue()) \/\/ 1024\n        if o_size &lt;= mb:\n            return b64\n        im_out = im\n        while o_size &gt; mb:\n            img = Image.open(im_out)\n            x, y = img.size\n            out = img.resize((int(x * k), int(y * k)), Image.ANTIALIAS)\n            im_out.close()\n            im_out = io.BytesIO()\n            if out.mode == &quot;RGBA&quot;:\n                out.save(im_out, &#039;png&#039;)\n            elif out.mode == &quot;RGB&quot;:\n                out.save(im_out, &#039;jpg&#039;)\n            else:\n                return &quot;&quot;\n            o_size = len(im_out.getvalue()) \/\/ 1024\n        b64 = base64.b64encode(im_out.getvalue())\n        im_out.close()\n        return str(b64, encoding=&#039;utf8&#039;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/www.it610.com\/article\/1296767807555444736.htm python\u4e0d\u6539\u53d8   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[53],"tags":[16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1166"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1166"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1166\/revisions"}],"predecessor-version":[{"id":1167,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1166\/revisions\/1167"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1166"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}