{"id":170088,"date":"2023-07-29T06:56:46","date_gmt":"2023-07-28T22:56:46","guid":{"rendered":"http:\/\/www.idc.net\/help\/170088\/"},"modified":"2023-07-29T06:56:46","modified_gmt":"2023-07-28T22:56:46","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e6%80%8e%e6%a0%b7%e5%ae%89%e8%a3%85pil","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/170088\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u600e\u6837\u5b89\u88c5pil"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/003\/5e09a65879858882.jpg\" class=\"aligncenter\"><\/p>\n<p>PIL\uff1aPython Imaging Library\uff0c\u5df2\u7ecf\u662fPython\u5e73\u53f0\u4e8b\u5b9e\u4e0a\u7684\u56fe\u50cf\u5904\u7406\u6807\u51c6\u5e93\u4e86\u3002PIL\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u4f46API\u5374\u975e\u5e38\u7b80\u5355\u6613\u7528\u3002<\/p>\n<p><strong>\u5b89\u88c5PIL<\/strong><\/p>\n<p>\u5728Debian\/Ubuntu Linux\u4e0b\u76f4\u63a5\u901a\u8fc7apt\u5b89\u88c5\uff1a<\/p>\n<pre>$&nbsp;sudo&nbsp;apt-get&nbsp;install&nbsp;python-imaging<\/pre>\n<p>Mac\u548c\u5176\u4ed6\u7248\u672c\u7684Linux\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528easy_install\u6216pip\u5b89\u88c5\uff0c\u5b89\u88c5\u524d\u9700\u8981\u628a\u7f16\u8bd1\u73af\u5883\u88c5\u597d\uff1a<\/p>\n<pre>$&nbsp;sudo&nbsp;easy_install&nbsp;PIL<\/pre>\n<p>\u5982\u679c\u5b89\u88c5\u5931\u8d25\uff0c\u6839\u636e\u63d0\u793a\u5148\u628a\u7f3a\u5931\u7684\u5305\uff08\u6bd4\u5982openjpeg\uff09\u88c5\u4e0a\u3002<\/p>\n<p>Windows\u5e73\u53f0\u5c31\u53bbPIL\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7dexe\u5b89\u88c5\u5305\u3002<\/p>\n<p><strong>\u64cd\u4f5c\u56fe\u50cf<\/strong><\/p>\n<p>\u6765\u770b\u770b\u6700\u5e38\u89c1\u7684\u56fe\u50cf\u7f29\u653e\u64cd\u4f5c\uff0c\u53ea\u9700\u4e09\u56db\u884c\u4ee3\u7801\uff1a<\/p>\n<pre>import&nbsp;Image\n#&nbsp;\u6253\u5f00\u4e00\u4e2ajpg\u56fe\u50cf\u6587\u4ef6\uff0c\u6ce8\u610f\u8def\u5f84\u8981\u6539\u6210\u4f60\u81ea\u5df1\u7684:\nim&nbsp;=&nbsp;Image.open('\/Users\/michael\/test.jpg')\n#&nbsp;\u83b7\u5f97\u56fe\u50cf\u5c3a\u5bf8:\nw,&nbsp;h&nbsp;=&nbsp;im.size\n#&nbsp;\u7f29\u653e\u523050%:\nim.thumbnail((w\/\/2,&nbsp;h\/\/2))\n#&nbsp;\u628a\u7f29\u653e\u540e\u7684\u56fe\u50cf\u7528jpeg\u683c\u5f0f\u4fdd\u5b58:\nim.save('\/Users\/michael\/thumbnail.jpg',&nbsp;'jpeg')<\/pre>\n<p>\u5176\u4ed6\u529f\u80fd\u5982\u5207\u7247\u3001\u65cb\u8f6c\u3001\u6ee4\u955c\u3001\u8f93\u51fa\u6587\u5b57\u3001\u8c03\u8272\u677f\u7b49\u4e00\u5e94\u4ff1\u5168\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u6a21\u7cca\u6548\u679c\u4e5f\u53ea\u9700\u51e0\u884c\u4ee3\u7801\uff1a<\/p>\n<pre>import&nbsp;Image,&nbsp;ImageFilter\nim&nbsp;=&nbsp;Image.open('\/Users\/michael\/test.jpg')\nim2&nbsp;=&nbsp;im.filter(ImageFilter.BLUR)\nim2.save('\/Users\/michael\/blur.jpg',&nbsp;'jpeg')<\/pre>\n<p>\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/989\/499\/288\/1577690125287000.png\" class=\"aligncenter\"><\/p>\n<p>PIL\u7684ImageDraw\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7ed8\u56fe\u65b9\u6cd5\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u7ed8\u56fe\u3002\u6bd4\u5982\u8981\u751f\u6210\u5b57\u6bcd\u9a8c\u8bc1\u7801\u56fe\u7247\uff1a<\/p>\n<pre>import&nbsp;Image,&nbsp;ImageDraw,&nbsp;ImageFont,&nbsp;ImageFilter\nimport&nbsp;random\n#&nbsp;\u968f\u673a\u5b57\u6bcd:\ndef&nbsp;rndChar():\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;chr(random.randint(65,&nbsp;90))\n#&nbsp;\u968f\u673a\u989c\u82721:\ndef&nbsp;rndColor():\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(random.randint(64,&nbsp;255),&nbsp;random.randint(64,&nbsp;255),&nbsp;random.randint(64,&nbsp;255))\n#&nbsp;\u968f\u673a\u989c\u82722:\ndef&nbsp;rndColor2():\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(random.randint(32,&nbsp;127),&nbsp;random.randint(32,&nbsp;127),&nbsp;random.randint(32,&nbsp;127))\n#&nbsp;240&nbsp;x&nbsp;60:\nwidth&nbsp;=&nbsp;60&nbsp;*&nbsp;4\nheight&nbsp;=&nbsp;60\nimage&nbsp;=&nbsp;Image.new('RGB',&nbsp;(width,&nbsp;height),&nbsp;(255,&nbsp;255,&nbsp;255))\n#&nbsp;\u521b\u5efaFont\u5bf9\u8c61:\nfont&nbsp;=&nbsp;ImageFont.truetype('Arial.ttf',&nbsp;36)\n#&nbsp;\u521b\u5efaDraw\u5bf9\u8c61:\ndraw&nbsp;=&nbsp;ImageDraw.Draw(image)\n#&nbsp;\u586b\u5145\u6bcf\u4e2a\u50cf\u7d20:\nfor&nbsp;x&nbsp;in&nbsp;range(width):\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;y&nbsp;in&nbsp;range(height):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw.point((x,&nbsp;y),&nbsp;fill=rndColor())\n#&nbsp;\u8f93\u51fa\u6587\u5b57:\nfor&nbsp;t&nbsp;in&nbsp;range(4):\n&nbsp;&nbsp;&nbsp;&nbsp;draw.text((60&nbsp;*&nbsp;t&nbsp;+&nbsp;10,&nbsp;10),&nbsp;rndChar(),&nbsp;font=font,&nbsp;fill=rndColor2())\n#&nbsp;\u6a21\u7cca:\nimage&nbsp;=&nbsp;image.filter(ImageFilter.BLUR)\nimage.save('code.jpg',&nbsp;'jpeg');<\/pre>\n<p>\u6211\u4eec\u7528\u968f\u673a\u989c\u8272\u586b\u5145\u80cc\u666f\uff0c\u518d\u753b\u4e0a\u6587\u5b57\uff0c\u6700\u540e\u5bf9\u56fe\u50cf\u8fdb\u884c\u6a21\u7cca\uff0c\u5f97\u5230\u9a8c\u8bc1\u7801\u56fe\u7247\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/909\/435\/391\/1577690065239607.png\" class=\"aligncenter\"><\/p>\n<p>\u5982\u679c\u8fd0\u884c\u7684\u65f6\u5019\u62a5\u9519\uff1a<\/p>\n<pre>IOError:&nbsp;cannot&nbsp;open&nbsp;resource<\/pre>\n<p>\u8fd9\u662f\u56e0\u4e3aPIL\u65e0\u6cd5\u5b9a\u4f4d\u5230\u5b57\u4f53\u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7edd\u5bf9\u8def\u5f84\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre>'\/Library\/Fonts\/Arial.ttf'<\/pre>\n<p>python\u5b66\u4e60\u7f51\uff0c\u514d\u8d39\u7684\u5728\u7ebf\u5b66\u4e60python\u5e73\u53f0\uff0c\u6b22\u8fce\u5173\u6ce8\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PIL\uff1aPython Imaging Library\uff0c\u5df2\u7ecf\u662fPython\u5e73\u53f0\u4e8b\u5b9e\u4e0a\u7684\u56fe\u50cf\u5904\u7406\u6807\u51c6\u5e93\u4e86\u3002PIL\u529f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":170089,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-170088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/170088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=170088"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/170088\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/170089"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=170088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=170088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=170088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}