Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb0c460

Browse files
author
Bill Prin
committed
Add Scipy example
1 parent 0fbc92b commit eb0c460

File tree

10 files changed

+103
-0
lines changed

10 files changed

+103
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ secrets.tar
1919
junit.xml
2020
credentials.dat
2121
.nox
22+
appengine/flexible/scipy/assets/resized_google_logo.jpg

appengine/flexible/scipy/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SciPy on App Engine Flexible
2+
3+
This sample demonstrates using SciPy to resize an image on App Engine Flexible.

appengine/flexible/scipy/app.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
runtime: python
2+
vm: true
3+
entrypoint: gunicorn -b :$PORT main:app
4+
5+
runtime_config:
6+
python_version: 3
21.1 KB
Loading
Loading

appengine/flexible/scipy/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
import os
17+
18+
from flask import Flask
19+
from scipy.misc import imread
20+
import scipy.misc
21+
22+
app = Flask(__name__)
23+
24+
25+
# [START scipy]
26+
@app.route('/')
27+
def resize():
28+
"""Demonstrates using scipy to resize an image."""
29+
app_path = os.path.dirname(os.path.realpath(__file__))
30+
image_path = os.path.join(app_path, 'assets/google_logo.jpg')
31+
img = imread(image_path)
32+
img_tinted = scipy.misc.imresize(img, (300, 300))
33+
output_image_path = os.path.join(
34+
app_path, 'assets/resized_google_logo.jpg')
35+
# Write the tinted image back to disk
36+
scipy.misc.imsave(output_image_path, img_tinted)
37+
return "Image resized."
38+
# [END scipy]
39+
40+
41+
@app.errorhandler(500)
42+
def server_error(e):
43+
logging.exception('An error occurred during a request.')
44+
return """
45+
An internal error occurred: <pre>{}</pre>
46+
See logs for full stacktrace.
47+
""".format(e), 500
48+
49+
50+
if __name__ == '__main__':
51+
# This is used when running locally. Gunicorn is used to run the
52+
# application on Google App Engine. See entrypoint in app.yaml.
53+
app.run(host='127.0.0.1', port=8080, debug=True)

appengine/flexible/scipy/main_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import filecmp
16+
import os
17+
18+
import main
19+
20+
21+
def test_index():
22+
main.app.testing = True
23+
client = main.app.test_client()
24+
test_path = os.path.dirname(os.path.realpath(__file__))
25+
asset_path = os.path.join(
26+
test_path, 'assets/resized_google_logo.jpg')
27+
fixtured_path = os.path.join(
28+
test_path, 'fixtures/assets/resized_google_logo.jpg')
29+
try:
30+
os.remove(asset_path)
31+
except OSError:
32+
pass # if doesn't exist
33+
r = client.get('/')
34+
assert filecmp.cmp(asset_path, fixtured_path)
35+
assert r.status_code == 200
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==0.11.1
2+
gunicorn==19.6.0
3+
scipy==0.18.0
4+
Pillow==3.3.1

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ requests==2.11.1
4040
requests[security]==2.11.1
4141
responses==0.5.1
4242
rsa==3.4.2
43+
scipy==0.18.0
4344
sendgrid==3.2.10
4445
simplejson==3.8.2
4546
six==1.10.0

0 commit comments

Comments
 (0)