|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Outputs a cropped image or an image highlighting crop regions on an image. |
| 18 | +
|
| 19 | +Examples: |
| 20 | + python crop_hints.py resources/cropme.jpg draw |
| 21 | + python crop_hints.py resources/cropme.jpg crop |
| 22 | +""" |
| 23 | +# [START vision_crop_hints_tutorial] |
| 24 | +# [START vision_crop_hints_tutorial_imports] |
| 25 | +import argparse |
| 26 | +import io |
| 27 | + |
| 28 | +from google.cloud import vision |
| 29 | +from google.cloud.vision import types |
| 30 | +from PIL import Image, ImageDraw |
| 31 | +# [END vision_crop_hints_tutorial_imports] |
| 32 | + |
| 33 | + |
| 34 | +def get_crop_hint(path): |
| 35 | + # [START vision_crop_hints_tutorial_get_crop_hints] |
| 36 | + """Detect crop hints on a single image and return the first result.""" |
| 37 | + client = vision.ImageAnnotatorClient() |
| 38 | + |
| 39 | + with io.open(path, 'rb') as image_file: |
| 40 | + content = image_file.read() |
| 41 | + |
| 42 | + image = types.Image(content=content) |
| 43 | + |
| 44 | + crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77]) |
| 45 | + image_context = types.ImageContext(crop_hints_params=crop_hints_params) |
| 46 | + |
| 47 | + response = client.crop_hints(image=image, image_context=image_context) |
| 48 | + hints = response.crop_hints_annotation.crop_hints |
| 49 | + |
| 50 | + # Get bounds for the first crop hint using an aspect ratio of 1.77. |
| 51 | + vertices = hints[0].bounding_poly.vertices |
| 52 | + # [END vision_crop_hints_tutorial_get_crop_hints] |
| 53 | + |
| 54 | + return vertices |
| 55 | + |
| 56 | + |
| 57 | +def draw_hint(image_file): |
| 58 | + """Draw a border around the image using the hints in the vector list.""" |
| 59 | + # [START vision_crop_hints_tutorial_draw_crop_hints] |
| 60 | + vects = get_crop_hint(image_file) |
| 61 | + |
| 62 | + im = Image.open(image_file) |
| 63 | + draw = ImageDraw.Draw(im) |
| 64 | + draw.polygon([ |
| 65 | + vects[0].x, vects[0].y, |
| 66 | + vects[1].x, vects[1].y, |
| 67 | + vects[2].x, vects[2].y, |
| 68 | + vects[3].x, vects[3].y], None, 'red') |
| 69 | + im.save('output-hint.jpg', 'JPEG') |
| 70 | + print('Saved new image to output-hint.jpg') |
| 71 | + # [END vision_crop_hints_tutorial_draw_crop_hints] |
| 72 | + |
| 73 | + |
| 74 | +def crop_to_hint(image_file): |
| 75 | + """Crop the image using the hints in the vector list.""" |
| 76 | + # [START vision_crop_hints_tutorial_crop_to_hints] |
| 77 | + vects = get_crop_hint(image_file) |
| 78 | + |
| 79 | + im = Image.open(image_file) |
| 80 | + im2 = im.crop([vects[0].x, vects[0].y, |
| 81 | + vects[2].x - 1, vects[2].y - 1]) |
| 82 | + im2.save('output-crop.jpg', 'JPEG') |
| 83 | + print('Saved new image to output-crop.jpg') |
| 84 | + # [END vision_crop_hints_tutorial_crop_to_hints] |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + # [START vision_crop_hints_tutorial_run_application] |
| 89 | + parser = argparse.ArgumentParser() |
| 90 | + parser.add_argument('image_file', help='The image you\'d like to crop.') |
| 91 | + parser.add_argument('mode', help='Set to "crop" or "draw".') |
| 92 | + args = parser.parse_args() |
| 93 | + |
| 94 | + if args.mode == 'crop': |
| 95 | + crop_to_hint(args.image_file) |
| 96 | + elif args.mode == 'draw': |
| 97 | + draw_hint(args.image_file) |
| 98 | + # [END vision_crop_hints_tutorial_run_application] |
| 99 | +# [END vision_crop_hints_tutorial] |
0 commit comments