|
| 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 full_tutorial] |
| 24 | +# [START imports] |
| 25 | +import argparse |
| 26 | +import io |
| 27 | + |
| 28 | +from google.cloud import vision |
| 29 | +from PIL import Image, ImageDraw |
| 30 | +# [END imports] |
| 31 | + |
| 32 | + |
| 33 | +def get_crop_hint(path): |
| 34 | + # [START get_crop_hint] |
| 35 | + """Detect crop hints on a single image and return the first result.""" |
| 36 | + vision_client = vision.Client() |
| 37 | + |
| 38 | + with io.open(path, 'rb') as image_file: |
| 39 | + content = image_file.read() |
| 40 | + |
| 41 | + image = vision_client.image(content=content) |
| 42 | + |
| 43 | + # Return bounds for the first crop hint using an aspect ratio of 1.77. |
| 44 | + return image.detect_crop_hints({1.77})[0].bounds.vertices |
| 45 | + # [END get_crop_hint] |
| 46 | + |
| 47 | + |
| 48 | +def draw_hint(image_file): |
| 49 | + """Draw a border around the image using the hints in the vector list.""" |
| 50 | + # [START draw_hint] |
| 51 | + vects = get_crop_hint(image_file) |
| 52 | + |
| 53 | + im = Image.open(image_file) |
| 54 | + draw = ImageDraw.Draw(im) |
| 55 | + draw.line([vects[0].x_coordinate, vects[0].y_coordinate, |
| 56 | + vects[1].x_coordinate, vects[1].y_coordinate], |
| 57 | + fill='red', width=3) |
| 58 | + draw.line([vects[1].x_coordinate, vects[1].y_coordinate, |
| 59 | + vects[2].x_coordinate, vects[2].y_coordinate], |
| 60 | + fill='red', width=3) |
| 61 | + draw.line([vects[2].x_coordinate, vects[2].y_coordinate, |
| 62 | + vects[3].x_coordinate, vects[3].y_coordinate], |
| 63 | + fill='red', width=3) |
| 64 | + draw.line([vects[3].x_coordinate, vects[3].y_coordinate, |
| 65 | + vects[0].x_coordinate, vects[0].y_coordinate], |
| 66 | + fill='red', width=3) |
| 67 | + im.save('output-hint.jpg', 'JPEG') |
| 68 | + # [END draw_hint] |
| 69 | + |
| 70 | + |
| 71 | +def crop_to_hint(image_file): |
| 72 | + """Crop the image using the hints in the vector list.""" |
| 73 | + # [START crop_to_hint] |
| 74 | + vects = get_crop_hint(image_file) |
| 75 | + |
| 76 | + im = Image.open(image_file) |
| 77 | + im2 = im.crop((vects[0].x_coordinate, vects[0].y_coordinate, |
| 78 | + vects[2].x_coordinate - 1, vects[2].y_coordinate - 1)) |
| 79 | + im2.save('output-crop.jpg', 'JPEG') |
| 80 | + # [END crop_to_hint] |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + # [START run_crop] |
| 85 | + parser = argparse.ArgumentParser() |
| 86 | + parser.add_argument('image_file', help='The image you\'d like to crop.') |
| 87 | + parser.add_argument('mode', help='Set to "crop" or "draw".') |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + parser = argparse.ArgumentParser() |
| 91 | + |
| 92 | + if args.mode == 'crop': |
| 93 | + crop_to_hint(args.image_file) |
| 94 | + elif args.mode == 'draw': |
| 95 | + draw_hint(args.image_file) |
| 96 | + # [END run_crop] |
| 97 | +# [END full_tutorial] |
0 commit comments