Last active
August 29, 2015 13:56
-
-
Save utgwkk/9322509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
from google.appengine.api import images | |
from google.appengine.ext.webapp import template | |
import webapp2 as webapp | |
import os | |
from StringIO import StringIO | |
try: from PIL import Image | |
except ImportError: import Image | |
def show(parameters, handler): | |
path = os.path.join(os.path.dirname(__file__), 'index.htm') | |
handler.response.write(template.render(path, parameters)) | |
class TopPage(webapp.RequestHandler): | |
def get(self): | |
show({'mode': None}, self) | |
class Generate(webapp.RequestHandler): | |
def post(self): | |
output = '' | |
errmsg = None | |
mode = 'err' | |
try: img = Image.open(StringIO(self.request.get('imgdata'))) | |
except IOError: errmsg = '画像データが不正です' | |
else: | |
try: | |
w = img.size[0] | |
h = img.size[1] | |
if w > 128 or h > 128: | |
raise images.LargeImageError | |
except images.NotImageError: errmsg = '画像データが不正です' | |
except images.LargeImageError: errmsg = '画像データは縦横128ピクセルを超えてはいけません' | |
else: | |
for y in range(h): | |
for x in range(w): | |
p = img.getpixel((x, y)) | |
output += '<span style="color: #%02x%02x%02x;">■</span>'%(p[0], p[1], p[2]) | |
output += '<br>\n' | |
mode = 'gen' | |
finally: show({'mode': mode, 'output_data': output, 'errmsg': errmsg}, self) | |
app = webapp.WSGIApplication([('/', TopPage),('/gen', Generate)],debug=True) | |
if __name__ == '__main__': run_wsgi_app(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application: img2square | |
version: 3 | |
runtime: python27 | |
api_version: 1 | |
threadsafe: no | |
handlers: | |
- url: .* | |
script: _img2square.app | |
libraries: | |
- name: webapp2 | |
version: latest | |
- name: PIL | |
version: latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Image to Square</title> | |
<script type="text/javascript"> | |
function preview(){ | |
prev = window.open('', 'p'); | |
prev.document.open(); | |
prev.document.write('<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"></head><body>'+document.f.out.value+'</body></html>'); | |
prev.document.close(); | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Image to Square</h1> | |
{%if mode == 'err'%}<h2>{{errmsg}}</h2>{%endif%} | |
{%if mode == 'gen'%}<form name="f"> | |
<textarea cols="100" rows="10" name="out">{{output_data}}</textarea> | |
<input type="button" value="プレビュー" onclick="preview()"> | |
</form> | |
{%else%}<form action="/gen" method="post" enctype="multipart/form-data"> | |
<input type="file" name="imgdata"><input type="submit" value="送信"> | |
</form> | |
<ul> | |
<li>大きさが128x128以内の画像ファイルをHTMLでドット絵を打ったように再現します</li> | |
<li>早い話が、ドット絵風HTMLを書き出します</li> | |
<li>透過PNGは対応してません</li> | |
</ul> | |
<h2>更新情報</h2> | |
<h3>2014/03/03</h3> | |
<p>webapp2 と PIL を用いて書きなおしてみたよ。処理速度とプレビューの速度が早くなったよ。やったね。</p> | |
<hr>{%endif%} | |
<p><a href="/">トップに戻る</a></p> | |
<p><a href="http://utgw.net/">つくったひとのサイト</a></p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment