{"id":6081,"date":"2023-06-05T10:35:56","date_gmt":"2023-06-05T02:35:56","guid":{"rendered":"http:\/\/www.aiersheng.cc\/?p=6081"},"modified":"2024-01-02T21:33:54","modified_gmt":"2024-01-02T13:33:54","slug":"python%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e9%aa%8c%e8%af%81%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.aiersheng.cc\/?p=6081","title":{"rendered":"Python\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801"},"content":{"rendered":"<p>Python\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801<br \/>\n\u5b89\u88c5<\/p>\n<p>pip install pillow<br \/>\n\u57fa\u672c\u4f7f\u7528<br \/>\n1. \u56fe\u7247\u751f\u6210<br \/>\nfrom PIL import Image<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<\/p>\n<p># \u5728\u56fe\u7247\u67e5\u770b\u5668\u4e2d\u6253\u5f00<br \/>\n# img.show() <\/p>\n<p># \u4fdd\u5b58\u5728\u672c\u5730<br \/>\nwith open(&#8216;code.png&#8217;,&#8217;wb&#8217;) as f:<br \/>\n    img.save(f,format=&#8217;png&#8217;)<br \/>\n2. \u521b\u5efa\u753b\u7b14\uff0c\u7528\u4e8e\u5728\u56fe\u7247\u4e0a\u753b\u4efb\u610f\u5185\u5bb9<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<br \/>\ndraw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<br \/>\n3. \u753b\u70b9<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<br \/>\ndraw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<br \/>\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5750\u6807<br \/>\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272<br \/>\ndraw.point([100, 100], fill=&#8221;red&#8221;)<br \/>\ndraw.point([300, 300], fill=(255, 255, 255))<br \/>\n4.\u753b\u7ebf<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<br \/>\ndraw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<br \/>\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807\u548c\u7ed3\u675f\u5750\u6807<br \/>\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272<br \/>\ndraw.line((100,100,100,300), fill=&#8217;red&#8217;)<br \/>\ndraw.line((100,100,300,100), fill=(255, 255, 255))<br \/>\n5. \u753b\u5706<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<br \/>\ndraw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<br \/>\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807\u548c\u7ed3\u675f\u5750\u6807\uff08\u5706\u8981\u753b\u5728\u5176\u4e2d\u95f4\uff09<br \/>\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5f00\u59cb\u89d2\u5ea6<br \/>\n# \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u7ed3\u675f\u89d2\u5ea6<br \/>\n# \u7b2c\u56db\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272<br \/>\ndraw.arc((100,100,300,300),0,90,fill=&#8221;red&#8221;)<br \/>\n6. \u5199\u6587\u672c<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<br \/>\ndraw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<br \/>\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807<br \/>\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5199\u5165\u5185\u5bb9<br \/>\n# \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272<br \/>\ndraw.text([0,0],&#8217;python&#8217;,&#8221;red&#8221;)<br \/>\n7. \u7279\u6b8a\u5b57\u4f53\u6587\u5b57<br \/>\nimg = Image.new(mode=&#8217;RGB&#8217;, size=(120, 30), color=(255, 255, 255))<br \/>\ndraw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<br \/>\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5b57\u4f53\u6587\u4ef6\u8def\u5f84<br \/>\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5b57\u4f53\u5927\u5c0f<br \/>\nfont = ImageFont.truetype(&#8220;kumo.ttf&#8221;, 28)<br \/>\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807<br \/>\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5199\u5165\u5185\u5bb9<br \/>\n# \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272<br \/>\n# \u7b2c\u56db\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272<br \/>\ndraw.text([0, 0], &#8216;python&#8217;, &#8220;red&#8221;, font=font)<br \/>\n\u56fe\u7247\u9a8c\u8bc1\u7801<br \/>\nimport random<\/p>\n<p>def check_code(width=120, height=30, char_length=5, font_file=&#8217;kumo.ttf&#8217;, font_size=28):<br \/>\n    code = []<br \/>\n    img = Image.new(mode=&#8217;RGB&#8217;, size=(width, height), color=(255, 255, 255))<br \/>\n    draw = ImageDraw.Draw(img, mode=&#8217;RGB&#8217;)<\/p>\n<p>    def rndChar():<br \/>\n        &#8220;&#8221;&#8221;<br \/>\n        \u751f\u6210\u968f\u673a\u5b57\u6bcd<br \/>\n        :return:<br \/>\n        &#8220;&#8221;&#8221;<br \/>\n        return chr(random.randint(65, 90))<\/p>\n<p>    def rndColor():<br \/>\n        &#8220;&#8221;&#8221;<br \/>\n        \u751f\u6210\u968f\u673a\u989c\u8272<br \/>\n        :return:<br \/>\n        &#8220;&#8221;&#8221;<br \/>\n        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))<\/p>\n<p>    # \u5199\u6587\u5b57<br \/>\n    font = ImageFont.truetype(font_file, font_size)<br \/>\n    for i in range(char_length):<br \/>\n        char = rndChar()<br \/>\n        code.append(char)<br \/>\n        h = random.randint(0, 4)<br \/>\n        draw.text([i * width \/ char_length, h], char, font=font, fill=rndColor())<\/p>\n<p>    # \u5199\u5e72\u6270\u70b9<br \/>\n    for i in range(40):<br \/>\n        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())<\/p>\n<p>    # \u5199\u5e72\u6270\u5706\u5708<br \/>\n    for i in range(40):<br \/>\n        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())<br \/>\n        x = random.randint(0, width)<br \/>\n        y = random.randint(0, height)<br \/>\n        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())<\/p>\n<p>    # \u753b\u5e72\u6270\u7ebf<br \/>\n    for i in range(5):<br \/>\n        x1 = random.randint(0, width)<br \/>\n        y1 = random.randint(0, height)<br \/>\n        x2 = random.randint(0, width)<br \/>\n        y2 = random.randint(0, height)<\/p>\n<p>        draw.line((x1, y1, x2, y2), fill=rndColor())<\/p>\n<p>    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)<br \/>\n    return img,&#8221;.join(code)<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<br \/>\n    # 1. \u76f4\u63a5\u6253\u5f00<br \/>\n    # img,code = check_code()<br \/>\n    # img.show()<\/p>\n<p>    # 2. \u5199\u5165\u6587\u4ef6<br \/>\n    # img,code = check_code()<br \/>\n    # with open(&#8216;code.png&#8217;,&#8217;wb&#8217;) as f:<br \/>\n    #     img.save(f,format=&#8217;png&#8217;)<\/p>\n<p>    # 3. \u5199\u5165\u5185\u5b58(Python3)<br \/>\n    # from io import BytesIO<br \/>\n    # stream = BytesIO()<br \/>\n    # img.save(stream, &#8216;png&#8217;)<br \/>\n    # stream.getvalue()<\/p>\n<p>    # 4. \u5199\u5165\u5185\u5b58\uff08Python2\uff09<br \/>\n    # import StringIO<br \/>\n    # stream = StringIO.StringIO()<br \/>\n    # img.save(stream, &#8216;png&#8217;)<br \/>\n    # stream.getvalue()<\/p>\n<p>    pass<\/p>\n<p><a href=\"https:\/\/www.aiersheng.cc\/wp-content\/uploads\/2023\/06\/1685932545-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.aiersheng.cc\/wp-content\/uploads\/2023\/06\/1685932545-2.png\" alt=\"\" width=\"609\" height=\"534\" class=\"aligncenter size-full wp-image-6082\" srcset=\"https:\/\/www.aiersheng.cc\/wp-content\/uploads\/2023\/06\/1685932545-2.png 609w, https:\/\/www.aiersheng.cc\/wp-content\/uploads\/2023\/06\/1685932545-2-600x526.png 600w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801 \u5b89\u88c5 pip&#8230;<\/p>\n","protected":false},"author":1,"featured_media":7742,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-6081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-zzjy"],"_links":{"self":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/posts\/6081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6081"}],"version-history":[{"count":0,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/posts\/6081\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/media\/7742"}],"wp:attachment":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}