{"id":630,"date":"2020-03-09T00:49:14","date_gmt":"2020-03-08T16:49:14","guid":{"rendered":"http:\/\/www.mustenaka.cn\/?p=630"},"modified":"2020-03-09T00:49:14","modified_gmt":"2020-03-08T16:49:14","slug":"pythonsetalphatowhite","status":"publish","type":"post","link":"https:\/\/www.mustenaka.cn\/index.php\/2020\/03\/09\/pythonsetalphatowhite\/","title":{"rendered":"Python\u5c06png\u900f\u660e\u53d8\u4e3a\u767d\u8272\u5e76\u4fdd\u5b58\u4e3ajpg\u56fe\u7247"},"content":{"rendered":"<p>\u6211\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u9700\u8981\u5c06\u4e00\u4e2a\u5b58\u5728alpha\u56fe\u5c42\u7684Png\u56fe\u7247\u7ecf\u8fc7\u6b63\u7247\u53e0\u5e95\u540e\uff0c\u5408\u6210\u51fa\u4e00\u4e2a\u65b0\u7684\u6e32\u67d3\u540e\u56fe\u7247\uff0c\u4e4b\u540e\u56fe\u7247\u8fdb\u884c\u5904\u7406\u5bfc\u51fa\u6210jpg\u56fe\u7247\uff0c\u8fd9\u5c31\u9700\u8981\u5254\u9664\u56fe\u7247\u4e2d\u7684alpha\u901a\u9053\uff0c\u7136\u800c\u76f4\u63a5\u7c97\u66b4\u7684\u5254\u9664\u5219\u4f1a\u53d1\u751f\u56fe\u7247\u7684\u6b63\u7247\u53e0\u5e95\u6548\u679c\u5b8c\u5168\u65e0\u7528\uff0c\u53d8\u6210\u4e86\u4e00\u5927\u7247\u56fe\u7247\u7cca\u5728\u4e0a\u9762\uff08\u56e0\u4e3a\u900f\u660e\u5185\u5bb9\u4e22\u5931\uff09<\/p>\n<p>\u6b64\u65f6\u6211\u53c2\u8003\u4e86\u4e00\u7bc7\u8fd9\u4e2a\u6587\u7ae0\uff0c\u56e0\u4e3a\u6211\u6838\u5fc3\u7684\u5904\u7406\u9700\u6c42\u8fd8\u662f\uff08\u900f\u660e\u5e95\u8f6c\u6362\u6210\u767d\u8272\u5e95\uff09<\/p>\n<blockquote><p><a href=\"https:\/\/blog.csdn.net\/comeonbabe_\/article\/details\/89266810\">https:\/\/blog.csdn.net\/comeonbabe_\/article\/details\/89266810<\/a><\/p><\/blockquote>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">#\u5229\u7528Pillow\u8fdb\u884c\u5904\u7406-opencv\u540c\u7406\r\nfrom PIL import Image\r\n \r\ndef alphabg2white_PIL(img):\r\n    img=img.convert('RGBA')\r\n    sp=img.size\r\n    width=sp[0]\r\n    height=sp[1]\r\n    print(sp)\r\n    for yh in range(height):\r\n        for xw in range(width):\r\n            dot=(xw,yh)\r\n            color_d=img.getpixel(dot)\r\n            if(color_d[3]==0):\r\n                color_d=(255,255,255,255)\r\n                img.putpixel(dot,color_d)\r\n    img.show()\r\n    return img<\/code><\/pre>\n<p>\u4f7f\u7528\u6548\u679c\uff0c\u7684\u786e\u80fd\u591f\u628a\u900f\u660e\u5185\u5bb9\u8f6c\u6362\u6210\u767d\u8272\u5185\u5bb9\uff0c\u53ef\u662f\uff1a\u3010\u592a\u3011\u3010\u5361\u3011\u3010\u4e86\u3011\uff01<\/p>\n<div>\u8fd0\u884c\u6548\u7387\u6781\u5176\u4f4e\u4e0b<\/div>\n<p>\u89e3\u5256\u4e00\u4e0b\u4ee3\u7801\u7684\u539f\u7406\uff0c\u65e0\u975e\u5c31\u662f\u904d\u5386\u641c\u7d22\uff08\u4e24\u5c42\u9ad8\u5ea6\u548c\u957f\u5ea6\u7684\u904d\u5386\uff09*\uff08\u6bcf\u4e00\u5c42\u5185\u90e8\u7684RGBA\u503c\u5f97\u904d\u5386\uff09\uff0c\u590d\u6742\u5ea6\u9ad8\u5230\u7206\u70b8\u3002<\/p>\n<p>\u5373\u65f6\u4f7f\u7528numpy\u8fdb\u884c\u4f18\u5316\uff08\u8bf4\u660e\u4e00\u4e0b\u539f\u7406\uff0cnumpy\u4f1a\u628a\u77e9\u9635\u5904\u7406\u7684\u5185\u5bb9\u901a\u8fc7\u9884\u5236\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\uff0c\u76f8\u6bd4Python\u89e3\u91ca\u5668\u6162\u6162\u632a\u52a8\uff0cnumpy\u7b80\u76f4\u5c31\u662f\u706b\u7bad\uff09\uff0c\u4e5f\u4e00\u6837\u4f1a\u82b1\u8d39\u5927\u91cf\u7684\u5b9e\u8df5\u3002<\/p>\n<h4>\u56de\u5934\u4e00\u60f3<\/h4>\n<blockquote><p>\u4e3a\u4ec0\u4e48\u8981\u7528\u904d\u5386\u5462\uff1f\u3010\u7b28\u86cb\uff01\u3011<\/p><\/blockquote>\n<p>\u4e00\u4e2a\u6781\u5176\u7b80\u5355\u7684\u601d\u7ef4\uff0c\u521b\u5efa\u4e00\u4e2a\u767d\u8272\u65e0\u900f\u660e\u7684\u767d\u8272\u6a21\u677f\uff0c\u5c3a\u5bf8\u4e0e\u76f8\u5173\u5904\u7406\u56fe\u7247\u5c3a\u5bf8\u4e00\u81f4\uff0c\u4f5c\u4e3a\u5e95\u8272\u8499\u7248\uff0c\u5229\u7528PIL\u7684alpha\u901a\u9053\u6df7\u5408\u7b97\u6cd5\u5c06\u5df2\u7ecf\u5904\u7406\u540e\u7684\u56fe\u7247\u7c98\u8d34\u4e0a\u53bb\uff0c\u518d\u5c06rgb\u503c\u5355\u72ec\u63d0\u53d6\u51fa\u6765\u4fdd\u5b58\u4e3ajpg\u683c\u5f0f\u4e0d\u662f\u66f4\u597d\u4e48\uff1f<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">#\u6e32\u67d3\u4ee3\u7801---\u9700\u8981\u5c06\u4e24\u5f20\u56fe\u7247\u8fdb\u884c\u5408\u5e76    \r\ndef back_fusion(self):\r\n        savePath = self.savePath + \"\\\\ansBack.png\"\r\n        img1 = Image.open(\"tmp.jpg\")\r\n        img1 = img1.convert(\"RGBA\")\r\n        img2 = Image.open(self.FoldDiagram_back_path)\r\n        #\u521b\u5efa\u767d\u8272\u6a21\u677f\r\n        white = Image.new(\"RGBA\", img2.size, color=(255, 255, 255, 255))\r\n        print(\"\u5f00\u59cb\u6b63\u7247\u53e0\u5e95\uff1a\", img1.mode, \"----\", img2.mode)\r\n        ans = IC.multiply(img2, img1)\r\n        # \u5c06\u767d\u8272\u6a21\u677f\u548c\u5bfc\u51fa\u540e\u7684\u56fe\u7247\u8fdb\u884c\u91cd\u53e0\r\n        ans = Image.alpha_composite(white, ans)\r\n        ans.save(savePath, dpi=(96, 96))\r\n        # \u8c03\u7528\u53bb\u9664alpha\u901a\u9053\u7b97\u6cd5\uff0c\u5982\u679c\u4e0d\u9700\u8981\u53bb\u9664alpha\u901a\u9053\u6ce8\u91ca\u6389\u5373\u53ef\r\n        self.removeAlpha(savePath)\r\n\r\n        if os.path.isfile(savePath):\r\n            os.remove(savePath)\r\n        print(\"\u4fdd\u5b58\u8936\u76b1\u56fe\u3010\u540e\u3011\u6e32\u67d3 \u6210\u529f\")\r\n\r\n\r\n# \u79fb\u9664alpha\u901a\u9053\u5e76\u5c06png\u4fdd\u5b58\u4e3ajpg   \r\n def removeAlpha(self, path):\r\n        img = cv2.imread(path, -1)       # \u8bfb\u53d6\u6e32\u67d3\u56fe\r\n        bgr = img[:, :, :3]\r\n        cv2.imwrite(path[:-3]+\"jpg\", bgr, [\r\n                    int(cv2.IMWRITE_PNG_COMPRESSION), 0])      # 0 ~ 9 (low - high)<\/code><\/pre>\n<p>\u548c\u904d\u5386\u76f8\u6bd4\u8f83\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7eaf\u8272\u7a7a\u95f4\u7684\u56fe\u7247\u8fdb\u884c\u91cd\u53e0\u5904\u7406\uff0c\u6bd4\u957f\u5bbd\u4e0d\u7b49\u6d88\u8017\u5185\u5b58\u8d8a\u6765\u8d8a\u591a\u901f\u5ea6\u8d8a\u6765\u8d8a\u6162\u7684\u5faa\u73af\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u89c4\u907f\u6389O(n^2)\u7684\u590d\u6742\u5ea6\u53d8\u4e3a\u5e38\u6570\u7ea7<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>png\u8f6c\u6362jpg;png\u53bb\u9664\u900f\u660e\u5185\u5bb9;\u900f\u660e\u5e95\u8272\u56fe\u7247\u8f6c\u6362\u4e3a\u767d\u8272\u5e95\u8272\u56fe\u7247<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[31],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"views":3687,"_links":{"self":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":1,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions\/631"}],"wp:attachment":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}