|
| 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) |
0 commit comments