{"id":1963,"date":"2018-09-21T09:23:22","date_gmt":"2018-09-21T00:23:22","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=1963"},"modified":"2018-09-22T12:32:00","modified_gmt":"2018-09-22T03:32:00","slug":"blog104","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog104\/","title":{"rendered":"(104) \u300cPIL\u3067\u5199\u771f\u306b\u65e5\u4ed8\u3092\u5165\u308c\u308b\u300d\u3092WEB\u30b5\u30fc\u30d3\u30b9\u5316"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>Python\u306f WEB\u3068\u306e\u89aa\u548c\u6027\u304c\u9ad8\u3044\u3002<\/p>\n<p>\u3088\u3063\u3066&#8230;<br \/>\n\u5148\u306b\u66f8\u3044\u305f <a href=\"https:\/\/www.dogrow.net\/python\/blog102\/\" target=\"_blank\">(102) PIL\u3067\u5199\u771f\u306b\u65e5\u4ed8\u3092\u5165\u308c\u308b<\/a> \u3092WEB\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<p>\u305d\u308c\u3060\u3051&#8230;<\/p>\n<h1 class=\"my_h\">2. \u4ed5\u69d8<\/h1>\n<p>1) WEB\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3002<br \/>\n2) CGI\u3067 Python\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8d77\u52d5\u3059\u308b\u3002<br \/>\n3) \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e EXIF\u60c5\u5831\u304b\u3089\u64ae\u5f71\u65e5\u3092\u62bd\u51fa\u3059\u308b\u3002<br \/>\n4) \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u5e45 640px\u306b\u30ea\u30b5\u30a4\u30ba\u3059\u308b\u3002<br \/>\n5) \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u53f3\u4e0b\u306b\u65e5\u4ed8\u3092\u57cb\u3081\u8fbc\u3080\u3002<br \/>\n6) \u51e6\u7406\u5f8c\u306e\u753b\u50cf\u3092 WEB\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3059\u308b\u3002<br \/>\n<span class=\"my_fc_crimson\">7) \u4eca\u56de\u306f\u6392\u4ed6\u5236\u5fa1\u306f\u8003\u3048\u306a\u3044\u3002<\/span><\/p>\n<h1 class=\"my_h\">3. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<p>\u51fa\u6765\u4e0a\u304c\u3063\u305f\u7269\u306f\u3053\u3061\u3089\u3002<br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0104\/\" target=\"_blank\">https:\/\/www.dogrow.net\/python\/sample\/0104\/<\/a><\/p>\n<p>\u51e6\u7406\u306e\u8a73\u7d30\u306f <a href=\"https:\/\/www.dogrow.net\/python\/blog102\/\" target=\"_blank\">(102) PIL\u3067\u5199\u771f\u306b\u65e5\u4ed8\u3092\u5165\u308c\u308b<\/a> \u306b\u8a18\u3057\u3066\u3042\u308b\u3002<\/p>\n<p><b>index.cgi<\/b><br \/>\n\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5f8c\u3001\u5f53\u8a72\u30d5\u30a1\u30a4\u30eb\u306b\u5b9f\u884c\u5c5e\u6027\u3092\u4ed8\u4e0e\u3059\u308b\u3053\u3068\u3002<br \/>\n\u51fa\u529b\u30d5\u30a1\u30a4\u30eb\u540d\u306f\u306a\u308b\u3079\u304f\u6563\u3089\u3070\u308b\u3088\u3046\u306b\u6642\u523b\u5024\u3068\u30d7\u30ed\u30bb\u30b9ID\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002\uff08\u203b\u4e0d\u5b8c\u5168\u306a\u6392\u4ed6\uff09<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/local\/bin\/python\r\n# -*- coding: utf-8 -*-\r\nprint 'Content-type: text\/html'\r\n\r\nimport os\r\nimport cgi\r\nfrom datetime import datetime\r\nimport mylib\r\n\r\nprint &quot;&quot;&quot;\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;utf-8&quot;&gt;\r\n&lt;title&gt;\u30b5\u30f3\u30d7\u30eb No.104&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&quot;&quot;&quot;\r\n\r\nform = cgi.FieldStorage()\r\nif form.has_key('upfile'):     # form\u304b\u3089\u6295\u7a3f\u3055\u308c\u305f\u60c5\u5831 upfile \u3042\u308a\uff1f\r\n    item = form&#x5B;'upfile']\r\n    if item.file:              # upfile \u306f\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\uff1f\r\n        img_path = '.\/%s_%d_%s' % (datetime.now().strftime(&quot;%s&quot;), os.getpid(), item.filename)\r\n        mylib.save_img(item, img_path)       # \u6295\u7a3f\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\r\n        if True == mylib.exec_put_date(img_path) :   # \u6295\u7a3f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306b\u65e5\u4ed8\u3092\u66f8\u304f\r\n            print('&lt;img src=&quot;' + img_path + '&quot;&gt;')    # \u51fa\u6765\u4e0a\u304c\u3063\u305f\u753b\u50cf\u3092\u8868\u793a\r\n        else:\r\n            os.remove(img_path)   # \u5931\u6557\u3057\u305f\u306a\u3089\u3070\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb\u3092\u524a\u9664\r\n\r\n# \u6295\u7a3f\u30d5\u30a9\u30fc\u30e0\u3092\u8868\u793a\r\nprint &quot;&quot;&quot;\r\n&lt;form enctype=&quot;multipart\/form-data&quot; action=&quot;.\/index.cgi&quot; method=&quot;post&quot;&gt;\r\n  &lt;input type=&quot;file&quot; name=&quot;upfile&quot;&gt;\r\n  &lt;input type=&quot;submit&quot; value=&quot;\u5b9f\u884c&quot;&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n&quot;&quot;&quot;\r\n<\/pre>\n<p><b>mylib.py<\/b><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom PIL import Image, ImageDraw, ImageFont\r\n\r\n########################################################################\r\nclass MyError(Exception):\r\n    pass\r\n\r\n########################################################################\r\ndef save_img( item, save_path ):\r\n    fout = file(save_path, 'wb')\r\n    while True:\r\n        chunk = item.file.read(1000000)\r\n        if not chunk:\r\n            break\r\n        fout.write(chunk)\r\n    fout.close()\r\n\r\n########################################################################\r\ndef resize_img( img ):\r\n    w,h = img.size\r\n    w_new = 640\r\n    h_new = h * w_new \/ w\r\n    imgR = img.resize((w_new, h_new), resample=Image.ANTIALIAS)\r\n    return imgR\r\n\r\n########################################################################\r\ndef exec_put_date( filepath ):\r\n    ret = False\r\n    try:\r\n        img = Image.open(filepath)\r\n        exif = img._getexif()\r\n        if None == exif :\r\n            raise MyError('No exif')\r\n\r\n        if False == 36867 in exif :\r\n            raise MyError('No DateTimeOriginal in exif')\r\n\r\n        text_datetime = exif&#x5B;36867]\r\n        text_date = text_datetime.split()&#x5B;0]\r\n        text_date = text_date.replace(':','-')\r\n\r\n        img = resize_img(img)\r\n        pos_x = img.size&#x5B;0] - 150\r\n        pos_y = img.size&#x5B;1] - 30\r\n\r\n        obj_draw = ImageDraw.Draw(img)\r\n        obj_font = ImageFont.truetype(&quot;.\/ipagp.ttf&quot;, 24)\r\n        obj_draw.text((pos_x+1, pos_y+1), text_date, fill=(0, 0, 0),     font=obj_font)\r\n        obj_draw.text((pos_x,   pos_y),   text_date, fill=(255, 160, 0), font=obj_font)\r\n\r\n        img.save(filepath)\r\n        ret = True\r\n\r\n    except MyError as e:\r\n        print('&lt;p style=&quot;color:#f00&quot;&gt;%s&lt;\/p&gt;' % e.message)\r\n\r\n    except IOError:\r\n        print('&lt;p style=&quot;color:#f00&quot;&gt;Invalid file&lt;\/p&gt;')\r\n\r\n    return ret\r\n<\/pre>\n<p>\u3067\u304d\u305f\uff01<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2018\/09\/001-3.png\" alt=\"\" \/><\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 Python\u306f WEB\u3068\u306e\u89aa\u548c\u6027\u304c\u9ad8\u3044\u3002 \u3088\u3063\u3066&#8230; \u5148\u306b\u66f8\u3044\u305f (102) PIL\u3067\u5199\u771f\u306b\u65e5\u4ed8\u3092\u5165\u308c\u308b \u3092WEB\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3002 \u305d\u308c\u3060\u3051&#8230; 2. \u4ed5\u69d8 1)\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog104\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1963","post","type-post","status-publish","format-standard","hentry","category-pil"],"views":2779,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/comments?post=1963"}],"version-history":[{"count":14,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1963\/revisions"}],"predecessor-version":[{"id":1986,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1963\/revisions\/1986"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=1963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=1963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=1963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}