{"id":244,"date":"2017-08-15T09:52:36","date_gmt":"2017-08-15T01:52:36","guid":{"rendered":"https:\/\/himmel.tech\/wp\/?p=244"},"modified":"2017-08-15T09:52:36","modified_gmt":"2017-08-15T01:52:36","slug":"django-%e4%bd%bf%e7%94%a8%e5%b0%8f%e8%ae%b0-hymmnos-picture-generator","status":"publish","type":"post","link":"https:\/\/himmel.tech\/wp\/?p=244","title":{"rendered":"Django \u4f7f\u7528\u5c0f\u8bb0 &#038;&#038; Hymmnos Picture Generator"},"content":{"rendered":"<p>Django\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u57fa\u4e8ePython\u7684Web\u540e\u7aef\u6846\u67b6\uff0c\u5927\u81f4\u8ddfPHP\u7684\u4f5c\u7528\u76f8\u4f3c\uff0c\u5373\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\u7136\u540e\u5c06\u6570\u636e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002<\/p>\n<p>\u76f8\u6bd4\u4e8ePHP\uff0cDjango\u66f4\u63a5\u8fd1\u4e8e\u201c\u540e\u53f0\u201d\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u7684\u6267\u884c\u540e\u53f0\u4ee3\u7801\uff0c\u800cPHP\u9700\u8981\u540e\u53f0\u4ee3\u7801\u5177\u6709CGI\u63a5\u53e3\u6216\u76f4\u63a5\u8d77\u8c03\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff08\u5c3d\u7ba1\u6709\u65f6\u5019\u8fd9\u6837\u66f4\u5feb\uff09\u3002<\/p>\n<p>\u8fd9\u6b21\u8981\u505a\u7684\u662f\u4e00\u4e2a\u5c0f\u7684Web\u5e94\u7528\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u89e3\u51b3\u7f51\u7edc\u4e2d\u5854\u8bed\u663e\u793a\u4e0d\u4fbf\u7684\u95ee\u9898\uff0c\u9879\u76ee\u73b0\u5728\u653e\u5728<a href=\"https:\/\/github.com\/sjx95\/Hymmnos-Picture-Generator\">https:\/\/github.com\/sjx95\/Hymmnos-Picture-Generator<\/a>\u3002\u8be5\u7a0b\u5e8f\u4eceURL\u4e2dGET\u4e00\u6bb5\uff08\u76ee\u524d\u662f\u53e5\uff09\u5854\u8bed\uff0c\u7136\u540e\u6e32\u67d3\u6210\u56fe\u7247\u5e76\u8fd4\u56de\u3002\u8fd9\u6837\u9700\u8981\u5728OO\u7a7a\u95f4\u4e4b\u7c7b\u7684\u5730\u65b9\u53ea\u9700\u8981\u63d2\u5165\u4e00\u5f20URL\u4e3a\u7c7b\u4f3c http:\/\/hymmnos.himmel.tech\/pic.png?text=Hymmnos \u7684\u56fe\u7247\u5c31\u53ef\u4ee5\u4e86\u3002\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/hymmnos.himmel.tech\/pic.png?text=Hymmnos\" width=\"181\" height=\"53\" \/><\/p>\n<p><!--more--><\/p>\n<h2>\u601d\u8def\u6574\u7406<\/h2>\n<ul>\n<li>\u5904\u7406URL\u8def\u7531\u89c4\u5219\uff0c\u5c06\u8bf7\u6c42\u4ea4\u7ed9\u76f8\u5e94\u7684\u4ee3\u7801\u6765\u5904\u7406<\/li>\n<li>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5f15\u5bfc\u9875\u9762<\/li>\n<li>\u83b7\u53d6\u8bf7\u6c42\u56fe\u7247\u65f6\u4f20\u9012\u8fdb\u6765\u7684GET\u53c2\u6570<\/li>\n<li>\u4f7f\u7528PIL\uff08Pillow\uff09\u7684ImageFont\u529f\u80fd\u5c06\u6587\u5b57\u753b\u5728\u56fe\u7247\u4e0a<\/li>\n<li>\u5c06\u56fe\u7247\u8fdb\u884cPNG\u7f16\u7801\u5e76\u5199\u5165\u4e34\u65f6\u6587\u4ef6<\/li>\n<li>\u4ece\u4e34\u65f6\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668<\/li>\n<\/ul>\n<h2>\u76f8\u5e94\u8f6f\u4ef6\u548c\u5305<\/h2>\n<ul>\n<li>PyCharm \uff08\u4f7f\u7528\u5b66\u751f\u8bb8\u53ef\uff09<\/li>\n<li>Python2.7<\/li>\n<li>Django<\/li>\n<li>Pillow<\/li>\n<\/ul>\n<h2>\u6d41\u7a0b<\/h2>\n<p>\u5b8c\u6210\u9875\u9762\u663e\u793a\u7684\u529f\u80fd\uff0c\u5927\u81f4\u4e0a\u53ef\u4ee5\u53c2\u8003 http:\/\/www.cnblogs.com\/feixuelove1009\/p\/5823135.html \u8fd9\u7bc7\u6587\u7ae0\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u9762\u5173\u4e8e\u9759\u6001\u6587\u4ef6\u7684\u505a\u6cd5\u4e0d\u592a\u5408\u9002\uff0c\u6700\u597d\u53c2\u7167Django\u5b98\u65b9\u7684\u8bf4\u660e\uff0c\u76f4\u63a5\u5728myapp\u6587\u4ef6\u5939\u4e0b\u65b0\u5efastatic\u6587\u4ef6\u5939\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u4fee\u6539setting.py\u3002<\/p>\n<p>\u7136\u540e\u662f\u5177\u4f53\u7684\u5904\u7406\u903b\u8f91\uff0c\u4e0d\u5e9f\u8bdd\u4e86\u76f4\u63a5\u8d34\u4ee3\u7801\uff0c\u5148\u662fURL\u8def\u7531\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># urls.py\n\nurlpatterns = [\n    # url(r'^admin\/', admin.site.urls),\n    url(r'^pic.html$', hymmnos2picture.views.index),\n    url(r'^pic.png', hymmnos2picture.views.pic),\n]<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c&#8221;\/pic.html&#8221;\u8fd9\u4e2a\u9875\u9762\u88ab\u4e22\u7ed9\u4e86index\u51fd\u6570\u5904\u7406\uff0c\u7136\u540e&#8221;\/pic.png*&#8221;\u8fd9\u4e2a\u9875\u9762\u88ab\u4e22\u7ed9\u4e86pic\u51fd\u6570\u5904\u7406\u3002\u4e0b\u9762\u6211\u4eec\u770b\u770b\u8fd9\u4e24\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># views.py\n\n# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.shortcuts import render\nfrom django.shortcuts import HttpResponse\nfrom PIL import Image, ImageDraw, ImageFont\nfrom tempfile import TemporaryFile\n\n\ndef index(request):\n    return render(None, 'hymmnos2picture_welcome.html')\n\n\ndef pic(request):\n    if request.method == \"GET\":\n        if request.GET.has_key(\"text\"):\n            text = request.GET.get(\"text\", None)\n            size = 48\n            # \u751f\u6210\u56fe\u7247\n            font = ImageFont.truetype(\"hymmnos2picture\/static\/hymmnos.ttf\", size)\n            image = Image.new('1', font.getsize(text))\n            draw = ImageDraw.Draw(image)\n            draw.text((0,0), text, font=font, fill='#66ccff')\n            # \u5199\u5165\u4e34\u65f6\u6587\u4ef6\n            tmp_file = TemporaryFile()\n            image.save(tmp_file, 'png')\n            tmp_file.seek(0);\n            image_data = tmp_file.read()\n            # \u628a\u56fe\u7247\u6587\u4ef6\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\n            return HttpResponse(image_data, content_type=\"image\/png\")  # \u6ce8\u610f\u65e7\u7248\u7684\u8d44\u6599\u4f7f\u7528mimetype,\u73b0\u5728\u5df2\u7ecf\u6539\u4e3acontent_type\n    return HttpResponse(\"Illegal Parameter\")<\/pre>\n<p>index\u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u4e86\u4e00\u4e2a\u9759\u6001\u7684HTML\u9875\u9762\uff08\u7528render\u662f\u4e3a\u4e86\u65b9\u4fbf\u4e4b\u540e\u53ef\u80fd\u7684\u6269\u5c55\uff09\uff0cpic\u51fd\u6570\uff0c\u989d\u6211\u89c9\u5f97\u8fd9\u4e2a\u4ee3\u7801\u5e94\u8be5\u633a\u597d\u61c2\u7684\uff0c\u5c31\u4e0d\u591a\u505a\u89e3\u91ca\u4e86\u3002\u4e0d\u8fc7\u8981\u8bb0\u5f97\u4e34\u65f6\u6587\u4ef6\u5199\u5b8c\u4e4b\u540e\u8981\u628a\u6587\u4ef6\u6307\u9488\u201c\u5f52\u96f6\u201d\uff0c\u5426\u5219\u540e\u9762\u8bfb\u4e0d\u51fa\u6765\u7684\u3002<\/p>\n<p>\u7136\u540e python manage.py runserver localhost:8000 \u5c31\u53ef\u4ee5\u5728\u672c\u5730\u6d4b\u8bd5\u5566\uff0c\u81f3\u4e8e\u90e8\u7f72\uff0c\u6211\u8fd8\u6ca1\u7814\u7a76\uff0c\u5c31\u76f4\u63a5\u7528\u8fd9\u79cd\u8c03\u8bd5\u65b9\u5f0f\u90e8\u7f72\u5728\u670d\u52a1\u5668\u4e0a\u4e86\u2026\u2026\u53cd\u6b63\u6548\u7387\u5dee\u70b9\u5c31\u5dee\u70b9\u4e86\uff0c\u53c8\u6ca1\u6709\u51e0\u4e2a\u4eba\u7528\uff08\u6ed1\u7a3d\uff09\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u76f8\u6bd4\u4e8ePHP\u9ebb\u70e6\u7684C\/C++\u8c03\u7528\uff0cPython\u5728\u505a\u4e00\u4e9b\u540e\u53f0\u5904\u7406\u7684\u65f6\u5019\u5c31\u65b9\u4fbf\u5f88\u591a\u3002\u4e0d\u8fc7\u7531\u4e8eMySQL-Python\u7684\u5751\u4f3c\u4e4e\u6bd4\u8f83\u591a\uff0c\u4e0a\u6b21\u5199\u4e00\u4e2a\u6570\u636e\u5c55\u793a\u9875\u9762\uff0c\u9047\u5230\u4e86\u67d0\u5e93\u5927\u5c0f\u5199\u7b49\u4e00\u7cfb\u5217\u95ee\u9898\uff0c\u6700\u7ec8\u653e\u5f03\u4e86Django\uff0c\u4f7f\u7528PHP\u5feb\u901f\u5199\u4e86\u4e00\u4e0b\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u89c9\u5f97\u8fd9\u4e24\u8005\u5404\u6709\u4f18\u52bf\u5427\uff0c\u7075\u6d3b\u9009\u62e9\uff0c\u7075\u6d3b\u9009\u62e9\u2026\u2026<\/p>\n<p>\u54e6\uff0c\u5bf9\u4e86\uff0c\u989c\u8272\u3001\u5b57\u53f7\u5565\u7684\u81ea\u5b9a\u4e49\u63a5\u4e0b\u6765\u51e0\u4e2a\u66f4\u65b0\u4f1a\u505a\uff0c\u5148\u51d1\u5408\u7528\u5427\u2026\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u57fa\u4e8ePython\u7684Web\u540e\u7aef\u6846\u67b6\uff0c\u5927\u81f4\u8ddfPHP\u7684\u4f5c\u7528\u76f8\u4f3c\uff0c\u5373\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c\u67d0\u4e9b\u4ee3\u7801 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/himmel.tech\/wp\/?p=244\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cDjango \u4f7f\u7528\u5c0f\u8bb0 &#038;&#038; Hymmnos Picture Generator\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p83CFH-3W","_links":{"self":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=244"}],"version-history":[{"count":2,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/244\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}