Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add image resize

  • Loading branch information...
commit 23e1d02e98f04d59833b00fcfb87487d5e46d876 1 parent bfc8a96
MIZUNO Hiroki authored
56 main/home_image.py
View
@@ -0,0 +1,56 @@
+from google.appengine.api import images
+
+
+class Spec(object):
+ def __init__(self, count, margin):
+ self.count = count
+ self.margin = margin
+
+class HomeImage(object):
+ def __init__(self,width, height, x, y):
+ self.width = width
+ self.height = height
+ self.x = x
+ self.y = y
+
+ def image_width(self):
+ return (self.width * self.x.count +
+ self.x.margin * (self.x.count - 1))
+
+ def image_height(self):
+ return (self.height * self.y.count +
+ self.y.margin * (self.y.count - 1))
+
+ def split(self, image):
+ self.__resize(image)
+ return self.__split(image)
+
+ def __resize(self,image):
+ image.resize(self.image_width(),
+ self.image_height())
+
+ def __split(self,image):
+ data = image.execute_transforms(output_encoding=images.PNG)
+ imgs = []
+ for x in xrange(0,self.x.count):
+ for y in xrange(0,self.y.count):
+ img = images.Image(data)
+ img.crop(x * (1.0 / self.x.count),
+ y * (1.0 / self.y.count),
+ ( x + 1 ) * (1.0 / self.x.count),
+ ( y + 1 ) * (1.0 / self.y.count))
+ imgs.append(img)
+ return imgs
+
+
+
+if __name__ == '__main__':
+ HomeImage = HomeImage(54,54,
+ Spec(4,0),
+ Spec(4,0))
+
+ with open('miku.jpg', 'r') as f:
+ data = f.read()
+ img = HomeImage.split(images.Image(data))
+ thumbnail = img.execute_transforms(output_encoding=images.PNG)
+ print thumbnail
BIN  main/miku.jpg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1  main/miku2.jpg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 main/templates/index.html
View
@@ -5,6 +5,9 @@
<title>Top Page - main</title>
</head>
<body>
-{{ message }}
+<form action="/miku" method="POST" enctype="multipart/form-data" >
+ <input type="file" name="image" />
+ <input type="submit" />
+</form>
</body>
</html>
2  main/urls.py
View
@@ -11,9 +11,11 @@ def make_rules():
return [
EndpointPrefix('main/', [
Rule('/', endpoint='index'),
+ Rule('/miku', endpoint='miku'),
]),
]
all_views = {
'main/index': 'main.views.index',
+ 'main/miku': 'main.views.miku',
}
18 main/views.py
View
@@ -24,9 +24,23 @@
"""
from kay.utils import render_to_response
-
-
+from google.appengine.api import images
+import home_image
+from werkzeug import Response
# Create your views here.
def index(request):
return render_to_response('main/index.html', {'message': 'Hello'})
+
+HomeImage = home_image.HomeImage(54,54,
+ home_image.Spec(4,0),
+ home_image.Spec(4,0))
+
+def miku(request):
+ data = request.files['image'].stream.read()
+ img = HomeImage.split(images.Image(data))[0]
+ png = img.execute_transforms(output_encoding=images.PNG)
+ r = Response()
+ r.content_type = 'image/png'
+ r.data = png
+ return r
Please sign in to comment.
Something went wrong with that request. Please try again.