{"id":594,"date":"2024-11-21T13:44:18","date_gmt":"2024-11-21T05:44:18","guid":{"rendered":"http:\/\/www.chan.ink\/?p=594"},"modified":"2024-12-18T16:01:35","modified_gmt":"2024-12-18T08:01:35","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":"http:\/\/www.chan.ink\/index.php\/2024\/11\/21\/python%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e9%aa%8c%e8%af%81%e7%a0%81\/","title":{"rendered":"\u3010Python\u3011Python\u751f\u6210\u968f\u673a\u56fe\u7247\u9a8c\u8bc1\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801\uff0c\u9700\u8981\u4f7f\u7528PIL\u6a21\u5757.<br><br>\u5b89\u88c5\uff1a<br>pip3 install pillow<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\u57fa\u672c\u4f7f\u7528\n1. \u521b\u5efa\u56fe\u7247\n\nfrom PIL import Image\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\n \n# \u5728\u56fe\u7247\u67e5\u770b\u5668\u4e2d\u6253\u5f00\n# img.show() \n \n# \u4fdd\u5b58\u5728\u672c\u5730\nwith open('code.png','wb') as f:\n    img.save(f,format='png')\n2. \u521b\u5efa\u753b\u7b14\uff0c\u7528\u4e8e\u5728\u56fe\u7247\u4e0a\u753b\u4efb\u610f\u5185\u5bb9\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\ndraw = ImageDraw.Draw(img, mode='RGB')\n3. \u753b\u70b9\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\ndraw = ImageDraw.Draw(img, mode='RGB')\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5750\u6807\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272\ndraw.point(&#91;100, 100], fill=\"red\")\ndraw.point(&#91;300, 300], fill=(255, 255, 255))\n4. \u753b\u7ebf\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\ndraw = ImageDraw.Draw(img, mode='RGB')\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807\u548c\u7ed3\u675f\u5750\u6807\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272\ndraw.line((100,100,100,300), fill='red')\ndraw.line((100,100,300,100), fill=(255, 255, 255))\n5. \u753b\u5706\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\ndraw = ImageDraw.Draw(img, mode='RGB')\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\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5f00\u59cb\u89d2\u5ea6\n# \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u7ed3\u675f\u89d2\u5ea6\n# \u7b2c\u56db\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272\ndraw.arc((100,100,300,300),0,90,fill=\"red\")\n6. \u5199\u6587\u672c\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\ndraw = ImageDraw.Draw(img, mode='RGB')\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5199\u5165\u5185\u5bb9\n# \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272\ndraw.text(&#91;0,0],'python',\"red\")\n7. \u7279\u6b8a\u5b57\u4f53\u6587\u5b57\nimg = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))\ndraw = ImageDraw.Draw(img, mode='RGB')\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5b57\u4f53\u5927\u5c0f\nfont = ImageFont.truetype(\"kumo.ttf\", 28)\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u8d77\u59cb\u5750\u6807\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u5199\u5165\u5185\u5bb9\n# \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272\n# \u7b2c\u56db\u4e2a\u53c2\u6570\uff1a\u8868\u793a\u989c\u8272\ndraw.text(&#91;0, 0], 'python', \"red\", font=font)\n\u56fe\u7247\u9a8c\u8bc1\u7801\n\nimport random\n \ndef check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):\n    code = &#91;]\n    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))\n    draw = ImageDraw.Draw(img, mode='RGB')\n \n    def rndChar():\n        \"\"\"\n        \u751f\u6210\u968f\u673a\u5b57\u6bcd   \n        :return:\n        \"\"\"\n        return chr(random.randint(65, 90))\n \n    def rndColor():\n        \"\"\"\n        \u751f\u6210\u968f\u673a\u989c\u8272\n        :return:\n        \"\"\"\n        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))\n \n    # \u5199\u6587\u5b57\n    font = ImageFont.truetype(font_file, font_size)\n    for i in range(char_length):\n        char = rndChar()\n        code.append(char)\n        h = random.randint(0, 4)\n        draw.text(&#91;i * width \/ char_length, h], char, font=font, fill=rndColor())\n \n    # \u5199\u5e72\u6270\u70b9\n    for i in range(40):\n        draw.point(&#91;random.randint(0, width), random.randint(0, height)], fill=rndColor())\n \n    # \u5199\u5e72\u6270\u5706\u5708\n    for i in range(40):\n        draw.point(&#91;random.randint(0, width), random.randint(0, height)], fill=rndColor())\n        x = random.randint(0, width)\n        y = random.randint(0, height)\n        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())\n \n    # \u753b\u5e72\u6270\u7ebf\n    for i in range(5):\n        x1 = random.randint(0, width)\n        y1 = random.randint(0, height)\n        x2 = random.randint(0, width)\n        y2 = random.randint(0, height)\n \n        draw.line((x1, y1, x2, y2), fill=rndColor())\n \n    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)\n    return img,''.join(code)\n \n \nif __name__ == '__main__':\n    # 1. \u76f4\u63a5\u6253\u5f00\n    # img,code = check_code()\n    # img.show()\n \n    # 2. \u5199\u5165\u6587\u4ef6\n    # img,code = check_code()\n    # with open('code.png','wb') as f:\n    #     img.save(f,format='png')\n \n    # 3. \u5199\u5165\u5185\u5b58(Python3)\n    # from io import BytesIO\n    # stream = BytesIO()\n    # img.save(stream, 'png')\n    # stream.getvalue()\n \n    # 4. \u5199\u5165\u5185\u5b58\uff08Python2\uff09\n    # import StringIO\n    # stream = StringIO.StringIO()\n    # img.save(stream, 'png')\n    # stream.getvalue()\n \n    pass<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f6c\u8f7d\u81f3\u6b66\u6c9b\u9f50\uff1a<a href=\"https:\/\/www.cnblogs.com\/wupeiqi\/articles\/5812291.html\">https:\/\/www.cnblogs.com\/wupeiqi\/articles\/5812291.html<\/a><app-translate-content _nghost-ng-c341168634=\"\" class=\"eusoft-translate-container-span-en ng-star-inserted\"><span _ngcontent-ng-c341168634=\"\" class=\"eusoft-eudic-chrome-extension-translate-content\"><\/span><\/app-translate-content><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u751f\u6210\u968f\u673a\u9a8c\u8bc1\u7801\uff0c\u9700\u8981\u4f7f\u7528PIL\u6a21\u5757. \u5b89\u88c5\uff1apip3 install pillow \u8f6c\u8f7d\u81f3\u6b66\u6c9b\u9f50\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-594","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/comments?post=594"}],"version-history":[{"count":3,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":1109,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/594\/revisions\/1109"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}