Permalink
Browse files

add image resize

  • Loading branch information...
1 parent bfc8a96 commit 23e1d02e98f04d59833b00fcfb87487d5e46d876 @mzp committed Feb 3, 2010
Showing with 79 additions and 3 deletions.
  1. +56 −0 main/home_image.py
  2. BIN main/miku.jpg
  3. +1 −0 main/miku2.jpg
  4. +4 −1 main/templates/index.html
  5. +2 −0 main/urls.py
  6. +16 −2 main/views.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
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -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>
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',
}
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

0 comments on commit 23e1d02

Please sign in to comment.