{"id":689,"date":"2023-08-31T20:25:43","date_gmt":"2023-08-31T12:25:43","guid":{"rendered":"https:\/\/scutvk.cn\/?p=689"},"modified":"2023-08-31T20:25:48","modified_gmt":"2023-08-31T12:25:48","slug":"python-%e5%b0%86%e5%9b%be%e7%89%87%e7%99%bd%e8%89%b2%e8%83%8c%e6%99%af%e5%8f%98%e6%88%90%e9%80%8f%e6%98%8e","status":"publish","type":"post","link":"https:\/\/scutvk.cn\/?p=689","title":{"rendered":"Python \u5c06\u56fe\u7247\u767d\u8272\u80cc\u666f\u53d8\u6210\u900f\u660e"},"content":{"rendered":"\n<p>\u5f53\u6d89\u53ca\u5230\u56fe\u50cf\u5904\u7406\u65f6\uff0cPython \u4e2d\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\u53eb\u505a Pillow (PIL)\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 Pillow \u6765\u5b8c\u6210\u5c06\u767d\u8272\u80cc\u666f\u6362\u6210\u900f\u660e\u7684\u4efb\u52a1\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 Pillow \u5e93\uff0c\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c <code>pip install Pillow<\/code> \u6765\u5b89\u88c5\u5b83\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u811a\u672c\uff0c\u5b83\u5c06\u8f93\u5165\u7684 PNG \u56fe\u7247\u4e2d\u7684\u767d\u8272\u80cc\u666f\u8f6c\u6362\u4e3a\u900f\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from PIL import Image\r\n\r\ndef replace_white_with_transparent(image_path, output_path):\r\n    # \u6253\u5f00\u56fe\u50cf\r\n    image = Image.open(image_path)\r\n    \r\n    # \u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a RGBA \u6a21\u5f0f\r\n    image = image.convert(\"RGBA\")\r\n    \r\n    # \u83b7\u53d6\u56fe\u50cf\u7684\u50cf\u7d20\u6570\u636e\r\n    data = image.getdata()\r\n    \r\n    # \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u50cf\u7d20\u5217\u8868\uff0c\u5c06\u767d\u8272\u80cc\u666f\u66ff\u6362\u4e3a\u900f\u660e\r\n    new_data = []\r\n    for item in data:\r\n        # \u5224\u65ad\u50cf\u7d20\u662f\u5426\u4e3a\u767d\u8272\r\n        if item[:3] == (255, 255, 255):\r\n            new_data.append((255, 255, 255, 0))  # \u66ff\u6362\u4e3a\u900f\u660e\r\n        else:\r\n            new_data.append(item)\r\n    \r\n    # \u66f4\u65b0\u56fe\u50cf\u7684\u50cf\u7d20\u6570\u636e\r\n    image.putdata(new_data)\r\n    \r\n    # \u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\r\n    image.save(output_path, format=\"PNG\")\r\n\r\nif __name__ == \"__main__\":\r\n    input_image_path = \"input.png\"       # \u8f93\u5165\u56fe\u7247\u7684\u8def\u5f84\r\n    output_image_path = \"output.png\"     # \u5904\u7406\u540e\u7684\u56fe\u7247\u4fdd\u5b58\u8def\u5f84\r\n    replace_white_with_transparent(input_image_path, output_image_path)\r\n    print(\"\u56fe\u7247\u5904\u7406\u5b8c\u6210\uff01\")\r\n<\/code><\/pre>\n\n\n\n<p>\u5c06 <code>\"input.png\"<\/code> \u66ff\u6362\u4e3a\u4f60\u7684\u8f93\u5165 PNG \u56fe\u7247\u7684\u8def\u5f84\uff0c\u5c06 <code>\"output.png\"<\/code> \u66ff\u6362\u4e3a\u4f60\u60f3\u8981\u4fdd\u5b58\u5904\u7406\u540e\u56fe\u7247\u7684\u8def\u5f84\u3002\u8fd0\u884c\u811a\u672c\u540e\uff0c\u5b83\u5c06\u4f1a\u5c06\u767d\u8272\u80cc\u666f\u8f6c\u6362\u4e3a\u900f\u660e\u5e76\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u7247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6d89\u53ca\u5230\u56fe\u50cf\u5904\u7406\u65f6\uff0cPython \u4e2d\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u56fe&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/689"}],"collection":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=689"}],"version-history":[{"count":1,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/689\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/689\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}