author | Mario Ferraro <fadinlight@gmail.com> |
Sun, 15 Nov 2009 22:12:20 +0100 | |
changeset 3093 | d1be59b6b627 |
parent 2309 | be1b94099f2d |
permissions | -rwxr-xr-x |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
2 |
# |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
3 |
# Copyright 2007 Google Inc. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
4 |
# |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
8 |
# |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
10 |
# |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
16 |
# |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
17 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
18 |
"""Stub version of the images API.""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
19 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
20 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
21 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
22 |
import logging |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
23 |
import StringIO |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
24 |
|
1278
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
25 |
try: |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
26 |
import PIL |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
27 |
from PIL import _imaging |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
28 |
from PIL import Image |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
29 |
except ImportError: |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
30 |
import _imaging |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
31 |
import Image |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
32 |
|
686
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
33 |
from google.appengine.api import apiproxy_stub |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
34 |
from google.appengine.api import images |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
from google.appengine.api.images import images_service_pb |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
from google.appengine.runtime import apiproxy_errors |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
38 |
|
2309
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
39 |
def _ArgbToRgbaTuple(argb): |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
40 |
"""Convert from a single ARGB value to a tuple containing RGBA. |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
41 |
|
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
42 |
Args: |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
43 |
argb: Signed 32 bit integer containing an ARGB value. |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
44 |
|
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
45 |
Returns: |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
46 |
RGBA tuple. |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
47 |
""" |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
48 |
unsigned_argb = argb % 0x100000000 |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
49 |
return ((unsigned_argb >> 16) & 0xFF, |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
50 |
(unsigned_argb >> 8) & 0xFF, |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
51 |
unsigned_argb & 0xFF, |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
52 |
(unsigned_argb >> 24) & 0xFF) |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
53 |
|
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
54 |
|
686
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
55 |
class ImagesServiceStub(apiproxy_stub.APIProxyStub): |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
"""Stub version of images API to be used with the dev_appserver.""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
57 |
|
686
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
58 |
def __init__(self, service_name='images'): |
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
59 |
"""Preloads PIL to load all modules in the unhardened environment. |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
60 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
61 |
Args: |
686
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
62 |
service_name: Service name expected for all calls. |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
63 |
""" |
686
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
64 |
super(ImagesServiceStub, self).__init__(service_name) |
df109be0567c
Load ../../google_appengine_1.1.7/ into
Sverre Rabbelier <srabbelier@gmail.com>
parents:
109
diff
changeset
|
65 |
Image.init() |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
66 |
|
2273
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
67 |
def _Dynamic_Composite(self, request, response): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
68 |
"""Implementation of ImagesService::Composite. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
69 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
70 |
Based off documentation of the PIL library at |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
71 |
http://www.pythonware.com/library/pil/handbook/index.htm |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
72 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
73 |
Args: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
74 |
request: ImagesCompositeRequest, contains image request info. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
75 |
response: ImagesCompositeResponse, contains transformed image. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
76 |
""" |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
77 |
width = request.canvas().width() |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
78 |
height = request.canvas().height() |
2309
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
79 |
color = _ArgbToRgbaTuple(request.canvas().color()) |
be1b94099f2d
Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2273
diff
changeset
|
80 |
canvas = Image.new("RGBA", (width, height), color) |
2273
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
81 |
sources = [] |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
82 |
if (not request.canvas().width() or request.canvas().width() > 4000 or |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
83 |
not request.canvas().height() or request.canvas().height() > 4000): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
84 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
85 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
86 |
if not request.image_size(): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
87 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
88 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
89 |
if not request.options_size(): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
90 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
91 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
92 |
if request.options_size() > images.MAX_COMPOSITES_PER_REQUEST: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
93 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
94 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
95 |
for image in request.image_list(): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
96 |
sources.append(self._OpenImage(image.content())) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
97 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
98 |
for options in request.options_list(): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
99 |
if (options.anchor() < images.TOP_LEFT or |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
100 |
options.anchor() > images.BOTTOM_RIGHT): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
101 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
102 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
103 |
if options.source_index() >= len(sources) or options.source_index() < 0: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
104 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
105 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
106 |
if options.opacity() < 0 or options.opacity() > 1: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
107 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
108 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
109 |
source = sources[options.source_index()] |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
110 |
x_anchor = (options.anchor() % 3) * 0.5 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
111 |
y_anchor = (options.anchor() / 3) * 0.5 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
112 |
x_offset = int(options.x_offset() + x_anchor * (width - source.size[0])) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
113 |
y_offset = int(options.y_offset() + y_anchor * (height - source.size[1])) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
114 |
alpha = options.opacity() * 255 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
115 |
mask = Image.new("L", source.size, alpha) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
116 |
canvas.paste(source, (x_offset, y_offset), mask) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
117 |
response_value = self._EncodeImage(canvas, request.canvas().output()) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
118 |
response.mutable_image().set_content(response_value) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
119 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
120 |
def _Dynamic_Histogram(self, request, response): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
121 |
"""Trivial implementation of ImagesService::Histogram. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
122 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
123 |
Based off documentation of the PIL library at |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
124 |
http://www.pythonware.com/library/pil/handbook/index.htm |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
125 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
126 |
Args: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
127 |
request: ImagesHistogramRequest, contains the image. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
128 |
response: ImagesHistogramResponse, contains histogram of the image. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
129 |
""" |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
130 |
image = self._OpenImage(request.image().content()) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
131 |
img_format = image.format |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
132 |
if img_format not in ("BMP", "GIF", "ICO", "JPEG", "PNG", "TIFF"): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
133 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
134 |
images_service_pb.ImagesServiceError.NOT_IMAGE) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
135 |
image = image.convert("RGBA") |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
136 |
red = [0] * 256 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
137 |
green = [0] * 256 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
138 |
blue = [0] * 256 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
139 |
for pixel in image.getdata(): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
140 |
red[int((pixel[0] * pixel[3]) / 255)] += 1 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
141 |
green[int((pixel[1] * pixel[3]) / 255)] += 1 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
142 |
blue[int((pixel[2] * pixel[3]) / 255)] += 1 |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
143 |
histogram = response.mutable_histogram() |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
144 |
for value in red: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
145 |
histogram.add_red(value) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
146 |
for value in green: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
147 |
histogram.add_green(value) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
148 |
for value in blue: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
149 |
histogram.add_blue(value) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
150 |
|
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
151 |
def _Dynamic_Transform(self, request, response): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
152 |
"""Trivial implementation of ImagesService::Transform. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
153 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
154 |
Based off documentation of the PIL library at |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
155 |
http://www.pythonware.com/library/pil/handbook/index.htm |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
156 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
157 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
158 |
request: ImagesTransformRequest, contains image request info. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
159 |
response: ImagesTransformResponse, contains transformed image. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
160 |
""" |
2273
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
161 |
original_image = self._OpenImage(request.image().content()) |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
162 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
163 |
new_image = self._ProcessTransforms(original_image, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
164 |
request.transform_list()) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
165 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
166 |
response_value = self._EncodeImage(new_image, request.output()) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
167 |
response.mutable_image().set_content(response_value) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
168 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
169 |
def _EncodeImage(self, image, output_encoding): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
170 |
"""Encode the given image and return it in string form. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
171 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
172 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
173 |
image: PIL Image object, image to encode. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
174 |
output_encoding: ImagesTransformRequest.OutputSettings object. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
175 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
176 |
Returns: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
177 |
str with encoded image information in given encoding format. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
178 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
179 |
image_string = StringIO.StringIO() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
180 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
181 |
image_encoding = "PNG" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
182 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
183 |
if (output_encoding.mime_type() == images_service_pb.OutputSettings.JPEG): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
184 |
image_encoding = "JPEG" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
185 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
186 |
image = image.convert("RGB") |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
187 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
188 |
image.save(image_string, image_encoding) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
189 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
190 |
return image_string.getvalue() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
191 |
|
2273
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
192 |
def _OpenImage(self, image): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
193 |
"""Opens an image provided as a string. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
194 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
195 |
Args: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
196 |
image: image data to be opened |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
197 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
198 |
Raises: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
199 |
apiproxy_errors.ApplicationError if the image cannot be opened or if it |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
200 |
is an unsupported format. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
201 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
202 |
Returns: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
203 |
Image containing the image data passed in. |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
204 |
""" |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
205 |
if not image: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
206 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
207 |
images_service_pb.ImagesServiceError.NOT_IMAGE) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
208 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
209 |
image = StringIO.StringIO(image) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
210 |
try: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
211 |
image = Image.open(image) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
212 |
except IOError: |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
213 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
214 |
images_service_pb.ImagesServiceError.BAD_IMAGE_DATA) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
215 |
|
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
216 |
img_format = image.format |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
217 |
if img_format not in ("BMP", "GIF", "ICO", "JPEG", "PNG", "TIFF"): |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
218 |
raise apiproxy_errors.ApplicationError( |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
219 |
images_service_pb.ImagesServiceError.NOT_IMAGE) |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
220 |
return image |
e4cb9c53db3e
Load /Users/solydzajs/Desktop/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1278
diff
changeset
|
221 |
|
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
222 |
def _ValidateCropArg(self, arg): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
223 |
"""Check an argument for the Crop transform. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
224 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
225 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
226 |
arg: float, argument to Crop transform to check. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
227 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
228 |
Raises: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
229 |
apiproxy_errors.ApplicationError on problem with argument. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
230 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
231 |
if not isinstance(arg, float): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
232 |
raise apiproxy_errors.ApplicationError( |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
233 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
234 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
235 |
if not (0 <= arg <= 1.0): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
236 |
raise apiproxy_errors.ApplicationError( |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
237 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
238 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
239 |
def _CalculateNewDimensions(self, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
240 |
current_width, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
241 |
current_height, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
242 |
req_width, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
243 |
req_height): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
244 |
"""Get new resize dimensions keeping the current aspect ratio. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
245 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
246 |
This uses the more restricting of the two requested values to determine |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
247 |
the new ratio. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
248 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
249 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
250 |
current_width: int, current width of the image. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
251 |
current_height: int, current height of the image. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
252 |
req_width: int, requested new width of the image. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
253 |
req_height: int, requested new height of the image. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
254 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
255 |
Returns: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
256 |
tuple (width, height) which are both ints of the new ratio. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
257 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
258 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
259 |
width_ratio = float(req_width) / current_width |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
260 |
height_ratio = float(req_height) / current_height |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
261 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
262 |
if req_width == 0 or (width_ratio > height_ratio and req_height != 0): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
263 |
return int(height_ratio * current_width), req_height |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
264 |
else: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
265 |
return req_width, int(width_ratio * current_height) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
266 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
267 |
def _Resize(self, image, transform): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
268 |
"""Use PIL to resize the given image with the given transform. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
269 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
270 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
271 |
image: PIL.Image.Image object to resize. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
272 |
transform: images_service_pb.Transform to use when resizing. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
273 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
274 |
Returns: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
275 |
PIL.Image.Image with transforms performed on it. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
276 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
277 |
Raises: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
278 |
BadRequestError if the resize data given is bad. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
279 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
280 |
width = 0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
281 |
height = 0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
282 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
283 |
if transform.has_width(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
284 |
width = transform.width() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
285 |
if width < 0 or 4000 < width: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
286 |
raise apiproxy_errors.ApplicationError( |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
287 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
288 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
289 |
if transform.has_height(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
290 |
height = transform.height() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
291 |
if height < 0 or 4000 < height: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
292 |
raise apiproxy_errors.ApplicationError( |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
293 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
294 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
295 |
current_width, current_height = image.size |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
296 |
new_width, new_height = self._CalculateNewDimensions(current_width, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
297 |
current_height, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
298 |
width, |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
299 |
height) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
300 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
301 |
return image.resize((new_width, new_height), Image.ANTIALIAS) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
302 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
303 |
def _Rotate(self, image, transform): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
304 |
"""Use PIL to rotate the given image with the given transform. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
305 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
306 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
307 |
image: PIL.Image.Image object to rotate. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
308 |
transform: images_service_pb.Transform to use when rotating. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
309 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
310 |
Returns: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
311 |
PIL.Image.Image with transforms performed on it. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
312 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
313 |
Raises: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
314 |
BadRequestError if the rotate data given is bad. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
315 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
316 |
degrees = transform.rotate() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
317 |
if degrees < 0 or degrees % 90 != 0: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
318 |
raise apiproxy_errors.ApplicationError( |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
319 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
320 |
degrees %= 360 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
321 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
322 |
degrees = 360 - degrees |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
323 |
return image.rotate(degrees) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
324 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
325 |
def _Crop(self, image, transform): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
326 |
"""Use PIL to crop the given image with the given transform. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
327 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
328 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
329 |
image: PIL.Image.Image object to crop. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
330 |
transform: images_service_pb.Transform to use when cropping. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
331 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
332 |
Returns: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
333 |
PIL.Image.Image with transforms performed on it. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
334 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
335 |
Raises: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
336 |
BadRequestError if the crop data given is bad. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
337 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
338 |
left_x = 0.0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
339 |
top_y = 0.0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
340 |
right_x = 1.0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
341 |
bottom_y = 1.0 |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
342 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
343 |
if transform.has_crop_left_x(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
344 |
left_x = transform.crop_left_x() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
345 |
self._ValidateCropArg(left_x) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
346 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
347 |
if transform.has_crop_top_y(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
348 |
top_y = transform.crop_top_y() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
349 |
self._ValidateCropArg(top_y) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
350 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
351 |
if transform.has_crop_right_x(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
352 |
right_x = transform.crop_right_x() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
353 |
self._ValidateCropArg(right_x) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
354 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
355 |
if transform.has_crop_bottom_y(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
356 |
bottom_y = transform.crop_bottom_y() |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
357 |
self._ValidateCropArg(bottom_y) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
358 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
359 |
width, height = image.size |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
360 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
361 |
box = (int(transform.crop_left_x() * width), |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
362 |
int(transform.crop_top_y() * height), |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
363 |
int(transform.crop_right_x() * width), |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
364 |
int(transform.crop_bottom_y() * height)) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
365 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
366 |
return image.crop(box) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
367 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
368 |
def _ProcessTransforms(self, image, transforms): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
369 |
"""Execute PIL operations based on transform values. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
370 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
371 |
Args: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
372 |
image: PIL.Image.Image instance, image to manipulate. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
373 |
trasnforms: list of ImagesTransformRequest.Transform objects. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
374 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
375 |
Returns: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
376 |
PIL.Image.Image with transforms performed on it. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
377 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
378 |
Raises: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
379 |
BadRequestError if we are passed more than one of the same type of |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
380 |
transform. |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
381 |
""" |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
382 |
new_image = image |
1278
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
383 |
if len(transforms) > images.MAX_TRANSFORMS_PER_REQUEST: |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
384 |
raise apiproxy_errors.ApplicationError( |
a7766286a7be
Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
686
diff
changeset
|
385 |
images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA) |
109
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
386 |
for transform in transforms: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
387 |
if transform.has_width() or transform.has_height(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
388 |
new_image = self._Resize(new_image, transform) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
389 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
390 |
elif transform.has_rotate(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
391 |
new_image = self._Rotate(new_image, transform) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
392 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
393 |
elif transform.has_horizontal_flip(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
394 |
new_image = new_image.transpose(Image.FLIP_LEFT_RIGHT) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
395 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
396 |
elif transform.has_vertical_flip(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
397 |
new_image = new_image.transpose(Image.FLIP_TOP_BOTTOM) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
398 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
399 |
elif (transform.has_crop_left_x() or |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
400 |
transform.has_crop_top_y() or |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
401 |
transform.has_crop_right_x() or |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
402 |
transform.has_crop_bottom_y()): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
403 |
new_image = self._Crop(new_image, transform) |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
404 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
405 |
elif transform.has_autolevels(): |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
406 |
logging.info("I'm Feeling Lucky autolevels will be visible once this " |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
407 |
"application is deployed.") |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
408 |
else: |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
409 |
logging.warn("Found no transformations found to perform.") |
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
410 |
|
620f9b141567
Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
411 |
return new_image |