{"id":303,"date":"2016-02-10T21:07:09","date_gmt":"2016-02-10T13:07:09","guid":{"rendered":"http:\/\/sqrt-1.me\/?p=303"},"modified":"2016-11-19T14:34:37","modified_gmt":"2016-11-19T06:34:37","slug":"%e7%94%a8python%e7%94%9f%e6%88%90%e4%b8%8d%e5%90%8c%e8%83%8c%e6%99%af%e4%b8%8b%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9%e4%b8%8d%e5%90%8c%e7%9a%84%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/sqrt-1.me\/?p=303","title":{"rendered":"\u7528Python\u751f\u6210\u4e0d\u540c\u80cc\u666f\u4e0b\u663e\u793a\u5185\u5bb9\u4e0d\u540c\u7684\u56fe\u7247"},"content":{"rendered":"<p>\u4eca\u5929\u5728QQ\u7fa4\u91cc\u770b\u5230<a href=\"\/download\/amazing.png\">\u8fd9\u6837\u4e00\u5f20\u56fe\u7247<\/a>\uff0c\u5728\u624b\u673a\u4e0a\u663e\u793a\u7684\u65f6\u5019\uff0c\u7f29\u7565\u56fe\u548c\u70b9\u5f00\u4e4b\u540e\u770b\u5230\u7684\u5927\u56fe\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u4e24\u4e2a\u4eba\u3002\u4ee5\u524d\u4e5f\u5728\u5f88\u591a\u5730\u65b9\u770b\u5230\u8fc7\u70b9\u5f00\u4e4b\u524d\u548c\u70b9\u5f00\u4e4b\u540e\u663e\u793a\u5185\u5bb9\u4e0d\u540c\u7684\u56fe\u7247\uff0c\u4e00\u76f4\u4e0d\u77e5\u9053\u5176\u4e2d\u7684\u539f\u7406\uff0c\u4e8e\u662f\u60f3\u63a2\u7a76\u4e00\u756a\u3002<\/p>\n<p>\u7531\u4e8e\u6211\u4e0d\u662f\u5f88\u559c\u6b22\u539f\u56fe\uff0c\u8fd9\u91cc\u7528\u6211\u559c\u6b22\u7684<a href=\"\/download\/amazing2.png\">\u53e6\u4e00\u5f20\u56fe<\/a>\u6765\u8bf4\u660e\u3002<\/p>\n<p>\u9996\u5148\u628a\u56fe\u7247\u4fdd\u5b58\u5230\u7535\u8111\u4e2d\uff0c\u53d1\u73b0\u56fe\u7247\u662fpng\u683c\u5f0f\uff0c\u800c\u4e0d\u662f\u66f4\u5e38\u89c1\u7684jpg\u3002\u7528Photoshop\u6253\u5f00\u56fe\u7247\uff0c\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing2.jpg\" alt=\"amazing2\" width=\"530\" height=\"741\" class=\"alignnone size-full wp-image-304\" srcset=\"https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing2.jpg 530w, https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing2-215x300.jpg 215w\" sizes=\"auto, (max-width: 530px) 85vw, 530px\" \/><\/p>\n<p>\u8fd9\u4e9b\u7070\u767d\u76f8\u95f4\u7684\u683c\u5b50\u561b\uff0c\u5728Photoshop\u4e2d\u8868\u793a\u900f\u660e\u3002\u8fd9\u8bf4\u660e\u56fe\u7247\u4e2d\u7684\u5927\u90e8\u5206\u662f<strong>\u900f\u660e\u7684<\/strong>\uff01\u90a3\u4e3a\u4ec0\u4e48\u5728QQ\u4e2d\u663e\u793a\u7684\u4e0d\u662f\u8fd9\u4e2a\u6837\u5b50\u5462\uff1f\u56e0\u4e3a\u56fe\u7247\u7684\u540e\u9762\u6709<strong>\u80cc\u666f\u989c\u8272<\/strong>\uff01\u900f\u660e\u56fe\u7247\u7684\u7279\u70b9\u5c31\u662f\u5728\u4e0d\u540c\u80cc\u666f\u4e0b\u663e\u793a\u7684\u6548\u679c\u53ef\u4ee5\u4e0d\u4e00\u6837\u3002\u8fd9\u4e5f\u53ef\u4ee5\u8bf4\u660e\u4e3a\u4ec0\u4e48\u56fe\u7247\u662fpng\u683c\u5f0f\uff0c\u56e0\u4e3apng\u683c\u5f0f\u53ef\u4ee5\u6709Alpha\u901a\u9053\uff0c\u800cjpg\u6ca1\u6709\u3002<\/p>\n<p>\u5728Photoshop\u4e2d\u76f4\u63a5\u9a8c\u8bc1\u8fd9\u4e2a\u60f3\u6cd5\uff1a<\/p>\n<p><!--more--><\/p>\n<p>\u5728\u539f\u56fe\u7684\u56fe\u5c42\u4e0b\u9762\u65b0\u5efa\u4e00\u56fe\u5c42\uff0c\u586b\u5145\u4e3a\u767d\u8272\uff0c\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing3.jpg\" alt=\"amazing3\" width=\"254\" height=\"183\" class=\"alignnone size-full wp-image-305\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing4.jpg\" alt=\"amazing4\" width=\"541\" height=\"751\" class=\"alignnone size-full wp-image-306\" srcset=\"https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing4.jpg 541w, https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing4-216x300.jpg 216w\" sizes=\"auto, (max-width: 541px) 85vw, 541px\" \/><\/p>\n<p>\u5982\u679c\u4e0b\u9762\u7684\u56fe\u5c42\u586b\u5145\u4e3a\u9ed1\u8272\uff0c\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing5.jpg\" alt=\"amazing5\" width=\"252\" height=\"178\" class=\"alignnone size-full wp-image-308\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing6.jpg\" alt=\"amazing6\" width=\"547\" height=\"758\" class=\"alignnone size-full wp-image-309\" srcset=\"https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing6.jpg 547w, https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing6-216x300.jpg 216w\" sizes=\"auto, (max-width: 547px) 85vw, 547px\" \/><\/p>\n<p>\u679c\u7136\u662f\u80cc\u666f\u989c\u8272\u7684\u539f\u56e0\u3002QQ\u3001QQ\u7a7a\u95f4\u7b49\u8f6f\u4ef6\uff08\u5305\u62ec\u7f51\u9875\u7248\uff0c\u751a\u81f3\u5fae\u535a\u3001\u8d34\u5427\u3001twitter\uff09\u9ed8\u8ba4\u9884\u89c8\u65f6\u7684\u80cc\u666f\u662f\u767d\u8272\u7684\uff0c\u800c\u70b9\u5f00\u540e\u67e5\u770b\u56fe\u7247\u7684\u80cc\u666f\u901a\u5e38\u662f\u9ed1\u8272\u7684\uff0c\u6240\u4ee5\u70b9\u5f00\u540e\u56fe\u7247\u663e\u793a\u51fa\u4e86\u4e0d\u540c\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u5236\u4f5c\u8fd9\u79cd\u56fe\u7247\u5462\uff1f\u6211\u4eceAlpha\u901a\u9053\u7684\u539f\u7406\u5f00\u59cb\u5206\u6790\u2014\u2014<\/p>\n<p>\u8fd9\u79cd\u56fe\u7247\u90fd\u662f\u7070\u5ea6\u56fe\u7247\uff0c\u5047\u8bbe\u67d0\u4e2a\u50cf\u7d20\u4f4d\u7f6e\u7684\u7070\u5ea6\u662f\\(C\\)\uff0cAlpha\u901a\u9053\uff08\u5373\u4e0d\u900f\u660e\u5ea6\uff09\u662f\\(A\\)\uff0c\u5728\u9ed1\u8272\u80cc\u666f\u4e0b\u663e\u793a\u7684\u7070\u5ea6\u662f\\(C_1\\)\uff0c\u5728\u767d\u8272\u80cc\u666f\u4e0b\u663e\u793a\u7684\u7070\u5ea6\u662f\\(C_2\\)\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u503c\u7684\u53d6\u503c\u8303\u56f4\u90fd\u662f\\([0,1]\\)\uff0c\u5219\u6211\u4eec\u6709<\/p>\n<p>$$\\begin{equation} \\begin{cases}<br \/>\nC_1 = C \\cdot A + 0 \\cdot \\left( 1 &#8211; A \\right) \\\\<br \/>\nC_2 = C \\cdot A + 1 \\cdot \\left( 1 &#8211; A \\right)<br \/>\n\\end{cases} \\end{equation}$$<\/p>\n<p>\u7531\u6b64\u89e3\u51fa\\(C\\)\u548c\\(A\\)\uff0c\u5373\u5f97\u5230\u7531\u4e24\u5f20\u539f\u59cb\u56fe\u7247\u989c\u8272\u8ba1\u7b97\u53e0\u52a0\u56fe\u7247\u989c\u8272\u7684\u516c\u5f0f<\/p>\n<p>$$\\begin{equation} \\begin{cases}<br \/>\nC = \\frac {C_1} {1 + C_1 &#8211; C_2} \\\\<br \/>\nA = 1 + C_1 &#8211; C_2<br \/>\n\\end{cases} \\end{equation}$$<\/p>\n<p>\u7531\\(1-C_2 \\geq 0\\)\u548c\\(C_1 \\geq 0\\)\u53ef\u77e5\\(C\\)\u4e00\u5b9a\u5728\\([0,1]\\)\u8303\u56f4\u5185\u3002\uff08\u5982\u679c\u5206\u5b50\u5206\u6bcd\u540c\u65f6\u4e3a\u96f6\u5c31\u53d6\u4efb\u610f\u503c\uff0c\u5bf9\u5e94\u7684\u60c5\u51b5\u4e3a\u9ed1\u8272\u80cc\u666f\u4e0b\u4e3a\u7eaf\u9ed1\u3001\u767d\u8272\u80cc\u666f\u4e0b\u4e3a\u7eaf\u767d\uff0c\u6b64\u65f6\u8ba9Alpha\u4e3a\u96f6\u5373\u53ef\u3002\uff09<\/p>\n<p>\u53ef\u662f\\(A\\)\u4e0d\u4e00\u5b9a\u5728\\([0,1]\\)\u8303\u56f4\u5185\uff0c\u53ea\u6709\u5f53\\(1 + C_1 &#8211; C_2 \\leq 1\\)\u5373\\(C_1 \\leq C_2\\)\u65f6\u624d\u53ef\u4ee5\u3002\u4e5f\u5c31\u662f\u8bf4\u9ed1\u80cc\u666f\u4e0b\u56fe\u7247\u7684\u6bcf\u4e2a\u50cf\u7d20\u90fd\u8981\u6bd4\u767d\u80cc\u666f\u4e0b\u56fe\u7247\u5bf9\u5e94\u7684\u50cf\u7d20\u6697\uff0c\u5426\u5219Alpha\u503c\u88ab\u62b9\u5e73\u4e3a1\uff0c\u8fd9\u4e2a\u50cf\u7d20\u70b9\u5c31\u4e0d\u80fd\u5728\u7eaf\u9ed1\u548c\u7eaf\u767d\u80cc\u666f\u4e0b\u51c6\u786e\u8fd8\u539f\u4e24\u5f20\u56fe\u4e86\u3002\u8fd9\u5c31\u8bf4\u660e\uff0c\u9ed1\u80cc\u666f\u4e0b\u663e\u793a\u7684\u56fe\u5e94\u8be5\u5c3d\u91cf\u9009\u62e9\u4e00\u5f20\u6697\u8272\u56fe\uff0c\u767d\u80cc\u666f\u4e0b\u663e\u793a\u7684\u56fe\u5e94\u8be5\u5c3d\u91cf\u9009\u62e9\u4e00\u5f20\u660e\u4eae\u7684\u56fe\u3002<\/p>\n<p>\u4f46\u662f\u6211\u4eec\u8fd9\u6837\u53ea\u80fd\u4ea7\u751f\u7070\u5ea6\u56fe\u7247\uff0c\u5e76\u4e0d\u80fd\u628a\u8fd9\u79cd\u6280\u672f\u7528\u4e8e\u5f69\u8272\u56fe\u7247\u3002\u539f\u56e0\u5c31\u662f\u5728\u5904\u7406\u5f69\u8272\u56fe\u7247\u7684RGB\u4e09\u4e2a\u901a\u9053\u65f6\uff0c\u5f97\u5230\u7684Alpha\u503c\u53ef\u80fd\u4e0d\u76f8\u540c\uff0c\u8fd9\u6837\u5c31\u65e0\u6cd5\u540c\u65f6\u51c6\u786e\u8fd8\u539f\u4e09\u4e2a\u901a\u9053\u7684\u989c\u8272\u4e86\u3002<\/p>\n<p>\u63a8\u5bfc\u51fa\u6570\u5b66\u516c\u5f0f\u540e\uff0c\u6211\u5c31\u7528Python\u7684PIL\u5e93\u5199\u4e86\u4e00\u4e2a\u751f\u6210\u8fd9\u79cd\u56fe\u7247\u7684\u7a0b\u5e8f\u3002<\/p>\n<p>\u7a0b\u5e8f\u529b\u6c42\u7b80\u6d01\uff0c\u6709\u5982\u4e0b\u7ea6\u5b9a\uff1a<\/p>\n<ul>\n<li>\u4e24\u5f20\u539f\u59cb\u56fe\u7247\u5206\u8fa8\u7387\u76f8\u540c<\/li>\n<li>\u60f3\u8981\u5728\u9ed1\u8272\u80cc\u666f\u4e0b\u663e\u793a\u7684\u56fe\u8db3\u591f\u6697<\/li>\n<li>\u60f3\u8981\u5728\u767d\u8272\u80cc\u666f\u4e0b\u663e\u793a\u7684\u56fe\u8db3\u591f\u4eae<\/li>\n<li>\u8f93\u51fa\u7684\u56fe\u7247\u53ea\u6709\u7070\u5ea6\uff0c\u6ca1\u6709\u989c\u8272<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true \" >import sys\r\nfrom PIL import Image\r\n\r\ndark=Image.open(sys.argv[1],'r').convert('LA').split()[0]\r\nbright=Image.open(sys.argv[2],'r').convert('LA').split()[0]\r\nassert dark.size==bright.size\r\n\r\ndef conv(c1,c2):\r\nc=round(255*c1\/(255+c1-c2)) if 255+c1-c2!=0 else 0\r\nalpha=255+c1-c2 if 255+c1-c2<=255 else 255\r\nglobal distortion\r\nif 255+c1-c2>255: distortion+=1\r\nreturn (c,alpha)\r\n\r\ndistortion=0\r\nnewdata=list(map(conv,dark.getdata(),bright.getdata()))\r\nprint('distortion:%.2f%%' % (distortion\/len(newdata)*100))\r\n\r\nimg = Image.new('LA',dark.size)\r\nimg.putdata(newdata)\r\n\r\nimg.save(sys.argv[3], \"PNG\")<\/pre>\n<p>\u7a0b\u5e8f\u4f7f\u7528\u65b9\u6cd5\uff1a <span class=\"lang:sh decode:true  crayon-inline \" >python3 gen.py dark.jpg bright.jpg new.png<\/span> <\/p>\n<p>\u7a0b\u5e8f\u8f93\u51fa\u7684distortion\u662f\u5931\u771f\uff08\u5373\u7b97\u51fa\u7684Alpha\u5927\u4e8e1\u800c\u88ab\u62b9\u5e73\u4e3a1\uff09\u7684\u50cf\u7d20\u7684\u767e\u5206\u6bd4\u3002<\/p>\n<p>\u5728\u7f51\u4e0a\u968f\u4fbf\u627e\u4e86\u4e24\u5f20\u56fe\u7247\uff0c\u4e00\u5f20\u6697\u8272\u8c03\uff0c\u4e00\u5f20\u4eae\u8272\u8c03\uff0c\u7528\u8fd9\u6bb5\u7a0b\u5e8f\u751f\u6210png\u683c\u5f0f\u7684\u56fe\u7247\u540e\u53d1\u5230QQ\uff0c\u786e\u5b9e\u6709\u70b9\u5f00\u524d\u548c\u70b9\u5f00\u540e\u5b8c\u5168\u4e0d\u540c\u7684\u6548\u679c\u3002<\/p>\n<p>\u70b9\u5f00\u524d\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing7.png\" alt=\"amazing7\" width=\"476\" height=\"375\" class=\"alignnone size-full wp-image-312\" srcset=\"https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing7.png 476w, https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing7-300x236.png 300w\" sizes=\"auto, (max-width: 476px) 85vw, 476px\" \/><\/p>\n<p>\u70b9\u5f00\u540e\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing8.png\" alt=\"amazing8\" width=\"1060\" height=\"828\" class=\"alignnone size-full wp-image-313\" srcset=\"https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing8.png 1060w, https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing8-300x234.png 300w, https:\/\/sqrt-1.me\/wp-content\/uploads\/2016\/02\/amazing8-768x600.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728QQ\u7fa4\u91cc\u770b\u5230\u8fd9\u6837\u4e00\u5f20\u56fe\u7247\uff0c\u5728\u624b\u673a\u4e0a\u663e\u793a\u7684\u65f6\u5019\uff0c\u7f29\u7565\u56fe\u548c\u70b9\u5f00\u4e4b\u540e\u770b\u5230\u7684\u5927\u56fe\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u4e24\u4e2a\u4eba\u3002\u4ee5\u524d\u4e5f\u5728\u5f88\u591a &hellip; <a href=\"https:\/\/sqrt-1.me\/?p=303\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u7528Python\u751f\u6210\u4e0d\u540c\u80cc\u666f\u4e0b\u663e\u793a\u5185\u5bb9\u4e0d\u540c\u7684\u56fe\u7247\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[7],"tags":[],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303"}],"version-history":[{"count":8,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}