2020 python crop_hints.py resources/cropme.jpg draw
2121 python crop_hints.py resources/cropme.jpg crop
2222"""
23- # [START full_tutorial ]
24- # [START imports ]
23+ # [START vision_crop_hints_tutorial ]
24+ # [START vision_crop_hints_tutorial_imports ]
2525import argparse
2626import io
2727
2828from google .cloud import vision
2929from google .cloud .vision import types
3030from PIL import Image , ImageDraw
31- # [END imports ]
31+ # [END vision_crop_hints_tutorial_imports ]
3232
3333
3434def get_crop_hint (path ):
35- # [START get_crop_hint ]
35+ # [START vision_crop_hints_tutorial_get_crop_hints ]
3636 """Detect crop hints on a single image and return the first result."""
3737 client = vision .ImageAnnotatorClient ()
3838
@@ -49,14 +49,14 @@ def get_crop_hint(path):
4949
5050 # Get bounds for the first crop hint using an aspect ratio of 1.77.
5151 vertices = hints [0 ].bounding_poly .vertices
52- # [END get_crop_hint ]
52+ # [END vision_crop_hints_tutorial_get_crop_hints ]
5353
5454 return vertices
5555
5656
5757def draw_hint (image_file ):
5858 """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 ]
6060 vects = get_crop_hint (image_file )
6161
6262 im = Image .open (image_file )
@@ -67,23 +67,23 @@ def draw_hint(image_file):
6767 vects [2 ].x , vects [2 ].y ,
6868 vects [3 ].x , vects [3 ].y ], None , 'red' )
6969 im .save ('output-hint.jpg' , 'JPEG' )
70- # [END draw_hint ]
70+ # [END vision_crop_hints_tutorial_draw_crop_hints ]
7171
7272
7373def crop_to_hint (image_file ):
7474 """Crop the image using the hints in the vector list."""
75- # [START crop_to_hint ]
75+ # [START vision_crop_hints_tutorial_crop_to_hints ]
7676 vects = get_crop_hint (image_file )
7777
7878 im = Image .open (image_file )
7979 im2 = im .crop ([vects [0 ].x , vects [0 ].y ,
8080 vects [2 ].x - 1 , vects [2 ].y - 1 ])
8181 im2 .save ('output-crop.jpg' , 'JPEG' )
82- # [END crop_to_hint ]
82+ # [END vision_crop_hints_tutorial_crop_to_hints ]
8383
8484
8585if __name__ == '__main__' :
86- # [START run_crop ]
86+ # [START vision_crop_hints_tutorial_run_application ]
8787 parser = argparse .ArgumentParser ()
8888 parser .add_argument ('image_file' , help = 'The image you\' d like to crop.' )
8989 parser .add_argument ('mode' , help = 'Set to "crop" or "draw".' )
@@ -95,5 +95,5 @@ def crop_to_hint(image_file):
9595 crop_to_hint (args .image_file )
9696 elif args .mode == 'draw' :
9797 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