{"id":286,"date":"2019-05-25T23:18:34","date_gmt":"2019-05-25T14:18:34","guid":{"rendered":"https:\/\/blog.hgrs.me\/?p=286"},"modified":"2020-04-13T00:04:37","modified_gmt":"2020-04-12T15:04:37","slug":"python%e3%81%a7%e3%82%a4%e3%83%b3%e3%83%a1%e3%83%a2%e3%83%aa%e3%82%b9%e3%83%88%e3%83%aa%e3%83%bc%e3%83%a0%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e3%80%81%e4%b8%ad%e9%96%93%e3%83%87%e3%83%bc","status":"publish","type":"post","link":"https:\/\/blog.hgrs.me\/archives\/286.html","title":{"rendered":"Python\u3067\u30aa\u30f3\u30e1\u30e2\u30ea\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u5229\u7528\u3057\u3066\u3001\u4e2d\u9593\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u305b\u305a\u30d5\u30a1\u30a4\u30eb\u3092\u6271\u3046"},"content":{"rendered":"

\u306f\u3058\u3081\u306b<\/h2>\n

\u30d5\u30a9\u30f3\u30c8\u3092\u8efd\u91cf\u5316\u3059\u308bWeb\u30a2\u30d7\u30ea<\/a>\u3092\u4f5c\u3063\u305f\u969b\u306b\u3001\u30b5\u30fc\u30d0\u5074\u30673\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u3092zip\u306b\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u6642\u3001\u30b5\u30fc\u30d0\u5074\u306e\u30b9\u30c8\u30ec\u30fc\u30b8\u306b\u306f\u4fdd\u5b58\u3067\u304d\u306a\u3044\u306e\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u305b\u305a\u306b\u3001\u30c7\u30fc\u30bf\u3092\u9001\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u305d\u3093\u306a\u3068\u304d\u306e\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n

Python\u3067\u306e\u30b9\u30c8\u30ea\u30fc\u30e0<\/h2>\n

\u307e\u305a\u3001\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u3053\u3061\u3089<\/a>\u3067\u3059\u3002<\/p>\n

Python\u3067\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u6271\u3046\u30e2\u30b8\u30e5\u30fc\u30eb\u306fio<\/code>\u3067\u3059\u3002\u7279\u306b\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u30b9\u30c8\u30ec\u30fc\u30b8\u306b\u4fdd\u5b58\u305b\u305a\u306b\u30aa\u30f3\u30e1\u30e2\u30ea\u3067\u6271\u3048\u308b\u30b9\u30c8\u30ea\u30fc\u30e0\u306b\u306f\u3001\u6587\u5b57\u5217\u3092\u6271\u3046io.StringIO<\/code>\u3068\u3001\u30d0\u30a4\u30ca\u30ea\u3092\u6271\u3046io.BytesIO<\/code>\u306e2\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n

\u30ed\u30fc\u30ab\u30eb\u3067\u4f55\u304b\u3084\u308b\u5834\u5408\u306f\u3001\u4e2d\u9593\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u3082\u9010\u4e00\u4fdd\u5b58\u3057\u3066\u3001\u30c7\u30d0\u30c3\u30b0\u306a\u3069\u306b\u5229\u7528\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u672c\u756a\u74b0\u5883\u3067\u5168\u3066\u4fdd\u5b58\u3057\u3066\u305f\u3089\u3001\u30b9\u30c8\u30ec\u30fc\u30b8\u306e\u5bb9\u91cf\u304c\u306a\u304f\u306a\u3063\u3066\u304f\u306e\u3067\uff08\u7279\u306b\u30d5\u30a9\u30f3\u30c8\u307f\u305f\u3044\u306b\u91cd\u3081\u306e\u30c7\u30fc\u30bf\u3084\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u306e\u5834\u5408\uff09\u3001\u30aa\u30f3\u30e1\u30e2\u30ea\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u3046\u307e\u304f\u6d3b\u7528\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n

\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b<\/h2>\n

\u30c6\u30ad\u30b9\u30c8\u30e2\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb\u306e\u3088\u3046\u306b\u6271\u3046<\/h3>\n

\u307e\u305a\u3001StringIO<\/code>\u3067\u3059\u3002\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u666e\u901a\u306e\u6587\u5b57\u5217\u3092\u3001open<\/code>\u3067\u958b\u3044\u305f\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u3088\u3046\u306b\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u304c\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n

from io import StringIO\n\nwith StringIO() as bs:\n    bs.write('\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\\n')\n    bs.write('\u3055\u3088\u306a\u3089\u4e16\u754c')\n    content = bs.getvalue()\n\nprint(type(content))\nprint(content)<\/code><\/pre>\n

\u3053\u308c\u306e\u51fa\u529b\u7d50\u679c\u306f\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n

<class 'str'>\n\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\n\u3055\u3088\u306a\u3089\u4e16\u754c<\/code><\/pre>\n

\u30d5\u30a1\u30a4\u30eb\u30b9\u30c8\u30ea\u30fc\u30e0\u3068\u540c\u69d8\u306bwrite<\/code>\u3092\u4f7f\u3044\u3001\u6587\u5b57\u5217\u3092\u66f8\u304d\u8fbc\u3093\u3067\u3044\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305d\u3057\u3066\u6700\u7d42\u7684\u306a\u6587\u5b57\u5217\u3092\u5f97\u308b\u305f\u3081\u306b\u306fgetvalue<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u5f97\u3089\u308c\u308b\u306e\u306f\u6587\u5b57\u5217\uff08str<\/code>\uff09\u3067\u3059\u3002<\/p>\n

\u30d0\u30a4\u30ca\u30ea\u30e2\u30fc\u30c9\u30d5\u30a1\u30a4\u30eb\u306e\u3088\u3046\u306b\u6271\u3046<\/h3>\n

\u6b21\u306b\u3001BytesIO<\/code>\u3067\u3059\u3002\u3053\u308c\u3092\u6271\u3046\u3068\u3001\u69d8\u3005\u306a\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3092\u30aa\u30f3\u30e1\u30e2\u30ea\u3067\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u304c\u3053\u3061\u3089\u3067\u3059\u3002\u52d5\u4f5c\u306e\u78ba\u8a8d\u306e\u305f\u3081\u3001\u6700\u5f8c\u306b\u7d50\u5c40\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u4fdd\u5b58\u3059\u308b\u3068\u304b\u3044\u3046\u672c\u672b\u8ee2\u5012\u306a\u3053\u3068\u3057\u3066\u307e\u3059\u304c\u3001\u3053\u308c\u3050\u3089\u3044\u3057\u304b\u307f\u3066\u53d6\u308c\u308b\u78ba\u8a8d\u65b9\u6cd5\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u8a31\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

from io import BytesIO\nfrom PIL import Image\n\nwith BytesIO() as bs:\n    img = Image.new('L', (512, 512), 0)\n    img.save(bs, 'png')\n    content = bs.getvalue()\n\nwith open('img.png', mode='wb') as fs:\n    fs.write(content)<\/code><\/pre>\n

\u753b\u50cf\u3068\u304b\u304c\u898b\u3066\u308f\u304b\u308a\u3084\u3059\u3044\u304b\u306a\u3068\u601d\u3063\u305f\u306e\u3067\u3001\u753b\u50cf\u3092\u4fdd\u5b58\u3057\u3066\u307f\u307e\u3057\u305f\u3002BytesIO<\/code>\u3067\u4f5c\u6210\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306bPillow\u306esave<\/code>\u3092\u4f7f\u3063\u3066\u3001\u753b\u50cf\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002\u305f\u3060\u3001\u3053\u306e\u6642\u70b9\u3067\u306f\u307e\u3060\u3001\u30e1\u30e2\u30ea\u4e0a\u306b\u5b58\u5728\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u3053\u308c\u3092\u666e\u901a\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u3088\u3046\u306b\u69d8\u3005\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u7b49\u306b\u6e21\u3059\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u5358\u7d14\u306b\u4fdd\u5b58\u3059\u308b\u305f\u3081\u3001open<\/code>\u3067write<\/code>\u3057\u307e\u3059\u3002<\/p>\n

\u3059\u308b\u3068\u540c\u3058\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306bimg.png<\/code>\u3068\u3044\u3046\u771f\u3063\u9ed2\u306a\u753b\u50cf\u306e\u51fa\u6765\u4e0a\u304c\u308a\u3067\u3059\u3002<\/p>\n

\u304a\u308f\u308a\u306b<\/h2>\n

\u4e2d\u9593\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u305b\u305a\u306b\u6271\u3048\u308b\u306e\u306f\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n

zip\u306b\u3059\u308b\u306e\u306f\u307e\u305f\u3061\u3087\u3063\u3068\u3084\u3084\u3053\u3057\u3044\u306e\u3067\u3001\u5225\u3067\u66f8\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u306f\u3058\u3081\u306b \u30d5\u30a9\u30f3\u30c8\u3092\u8efd\u91cf\u5316\u3059\u308bWeb\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u305f\u969b\u306b\u3001\u30b5\u30fc\u30d0\u5074\u30673\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u3092zip\u306b\u3057\u3066\u30c0\u30a6\u30f3…<\/p>\n","protected":false},"author":1,"featured_media":287,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,28],"_links":{"self":[{"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/posts\/286"}],"collection":[{"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":0,"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/media\/287"}],"wp:attachment":[{"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hgrs.me\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}