Note for running this in local development server. You need to use install a older version of Pillow or PIL in order to use the image library. I tested this with Pillow 2.9 as suggested in here
Last active
April 23, 2018 09:28
-
-
Save edwardfung123/7a01e6e9d045002dead0a7d7f279dbc3 to your computer and use it in GitHub Desktop.
Creating a square image in Google AppEngine Python Standard Environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def create_square_image(img): | |
| '''Create a square image by padding the top-bottom or left-right from the `img`. | |
| The implementatio relies on the GAE's images library. | |
| There is no example for this NICE images library... use the source, Luke. | |
| https://cloud.google.com/appengine/docs/standard/python/refdocs/modules/google/appengine/api/images#composite | |
| Params: | |
| img, google.appengine.api.images.Image, the original non-square image. | |
| Return: | |
| google.appengine.api.images.Image, the result square image | |
| ''' | |
| side = max(img.width, img.height) | |
| white = 0xffffffff # Google said it is ARGB order. So 0xffff0000 for red. (o: | |
| # JPEG should be better for product photos. | |
| new_img = images.composite(( | |
| (img._image_data, 0, 0, 1.0, images.CENTER_CENTER), # first image | |
| ), | |
| width=side, height=side, color=white, | |
| output_encoding=images.JPEG, quality=100) | |
| new_img = Image(new_img) | |
| return new_img |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment