20
20
python crop_hints.py resources/cropme.jpg draw
21
21
python crop_hints.py resources/cropme.jpg crop
22
22
"""
23
- # [START full_tutorial ]
24
- # [START imports ]
23
+ # [START vision_crop_hints_tutorial ]
24
+ # [START vision_crop_hints_tutorial_imports ]
25
25
import argparse
26
26
import io
27
27
28
28
from google .cloud import vision
29
29
from google .cloud .vision import types
30
30
from PIL import Image , ImageDraw
31
- # [END imports ]
31
+ # [END vision_crop_hints_tutorial_imports ]
32
32
33
33
34
34
def get_crop_hint (path ):
35
- # [START get_crop_hint ]
35
+ # [START vision_crop_hints_tutorial_get_crop_hints ]
36
36
"""Detect crop hints on a single image and return the first result."""
37
37
client = vision .ImageAnnotatorClient ()
38
38
@@ -49,14 +49,14 @@ def get_crop_hint(path):
49
49
50
50
# Get bounds for the first crop hint using an aspect ratio of 1.77.
51
51
vertices = hints [0 ].bounding_poly .vertices
52
- # [END get_crop_hint ]
52
+ # [END vision_crop_hints_tutorial_get_crop_hints ]
53
53
54
54
return vertices
55
55
56
56
57
57
def draw_hint (image_file ):
58
58
"""Draw a border around the image using the hints in the vector list."""
59
- # [START draw_hint ]
59
+ # [START vision_crop_hints_tutorial_draw_crop_hints ]
60
60
vects = get_crop_hint (image_file )
61
61
62
62
im = Image .open (image_file )
@@ -67,23 +67,23 @@ def draw_hint(image_file):
67
67
vects [2 ].x , vects [2 ].y ,
68
68
vects [3 ].x , vects [3 ].y ], None , 'red' )
69
69
im .save ('output-hint.jpg' , 'JPEG' )
70
- # [END draw_hint ]
70
+ # [END vision_crop_hints_tutorial_draw_crop_hints ]
71
71
72
72
73
73
def crop_to_hint (image_file ):
74
74
"""Crop the image using the hints in the vector list."""
75
- # [START crop_to_hint ]
75
+ # [START vision_crop_hints_tutorial_crop_to_hints ]
76
76
vects = get_crop_hint (image_file )
77
77
78
78
im = Image .open (image_file )
79
79
im2 = im .crop ([vects [0 ].x , vects [0 ].y ,
80
80
vects [2 ].x - 1 , vects [2 ].y - 1 ])
81
81
im2 .save ('output-crop.jpg' , 'JPEG' )
82
- # [END crop_to_hint ]
82
+ # [END vision_crop_hints_tutorial_crop_to_hints ]
83
83
84
84
85
85
if __name__ == '__main__' :
86
- # [START run_crop ]
86
+ # [START vision_crop_hints_tutorial_run_application ]
87
87
parser = argparse .ArgumentParser ()
88
88
parser .add_argument ('image_file' , help = 'The image you\' d like to crop.' )
89
89
parser .add_argument ('mode' , help = 'Set to "crop" or "draw".' )
@@ -95,5 +95,5 @@ def crop_to_hint(image_file):
95
95
crop_to_hint (args .image_file )
96
96
elif args .mode == 'draw' :
97
97
draw_hint (args .image_file )
98
- # [END run_crop ]
99
- # [END full_tutorial ]
98
+ # [END vision_crop_hints_tutorial_run_application ]
99
+ # [END vision_crop_hints_tutorial ]
0 commit comments