@@ -32,46 +32,49 @@ To create and run the sample, copy the following code into the code editor.
32
32
import os
33
33
import sys
34
34
import requests
35
- # If you are using a Jupyter notebook, uncomment the following line .
35
+ # If you are using a Jupyter notebook, uncomment the following lines .
36
36
# %matplotlib inline
37
- import matplotlib.pyplot as plt
37
+ # import matplotlib.pyplot as plt
38
38
from PIL import Image
39
39
from io import BytesIO
40
40
41
41
# Add your Computer Vision subscription key and endpoint to your environment variables.
42
- if ' COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
43
- subscription_key = os.environ[' COMPUTER_VISION_SUBSCRIPTION_KEY' ]
44
- else :
45
- print (" \n Set the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n **Restart your shell or IDE for changes to take effect.**" )
46
- sys.exit()
47
-
48
- if ' COMPUTER_VISION_ENDPOINT' in os.environ:
49
- endpoint = os.environ[' COMPUTER_VISION_ENDPOINT' ]
42
+ subscription_key = os.environ[' COMPUTER_VISION_SUBSCRIPTION_KEY' ]
43
+ endpoint = os.environ[' COMPUTER_VISION_ENDPOINT' ]
50
44
51
45
thumbnail_url = endpoint + " vision/v2.1/generateThumbnail"
52
46
53
47
# Set image_url to the URL of an image that you want to analyze.
54
48
image_url = " https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"
55
49
50
+ # Construct URL
56
51
headers = {' Ocp-Apim-Subscription-Key' : subscription_key}
57
52
params = {' width' : ' 50' , ' height' : ' 50' , ' smartCropping' : ' true' }
58
53
data = {' url' : image_url}
59
- response = requests.post(thumbnail_url, headers = headers,
60
- params = params, json = data)
54
+ # Call API
55
+ response = requests.post(thumbnail_url, headers = headers, params = params, json = data)
61
56
response.raise_for_status()
62
57
58
+ # Open the image from bytes
63
59
thumbnail = Image.open(BytesIO(response.content))
64
60
65
- # Display the thumbnail.
66
- plt.imshow(thumbnail)
67
- plt.axis(" off" )
68
-
69
61
# Verify the thumbnail size.
70
62
print (" Thumbnail is {0} -by-{1} " .format(* thumbnail.size))
63
+
64
+ # Save thumbnail to file
65
+ thumbnail.save(' thumbnail.png' )
66
+
67
+ # Display image
68
+ thumbnail.show()
69
+
70
+ # Optional. Display the thumbnail from Jupyter.
71
+ # plt.imshow(thumbnail)
72
+ # plt.axis("off")
71
73
```
72
74
73
75
Next, do the following:
74
- 1 . Optionally, replace the value of ` image_url ` with the URL of a different image for which you want to generate a thumbnail.
76
+
77
+ 1 . (Optional) Replace the value of ` image_url ` with the URL of your own image.
75
78
1 . Save the code as a file with an ` .py ` extension. For example, ` get-thumbnail.py ` .
76
79
1 . Open a command prompt window.
77
80
1 . At the prompt, use the ` python ` command to run the sample. For example, ` python get-thumbnail.py ` .
0 commit comments