Skip to content

Commit 2fceb68

Browse files
authored
Update python-thumb.md
1 parent d5696c3 commit 2fceb68

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

articles/cognitive-services/Computer-vision/QuickStarts/python-thumb.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,46 +32,49 @@ To create and run the sample, copy the following code into the code editor.
3232
import os
3333
import sys
3434
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.
3636
# %matplotlib inline
37-
import matplotlib.pyplot as plt
37+
# import matplotlib.pyplot as plt
3838
from PIL import Image
3939
from io import BytesIO
4040

4141
# 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("\nSet 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']
5044

5145
thumbnail_url = endpoint + "vision/v2.1/generateThumbnail"
5246

5347
# Set image_url to the URL of an image that you want to analyze.
5448
image_url = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"
5549

50+
# Construct URL
5651
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
5752
params = {'width': '50', 'height': '50', 'smartCropping': 'true'}
5853
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)
6156
response.raise_for_status()
6257

58+
# Open the image from bytes
6359
thumbnail = Image.open(BytesIO(response.content))
6460

65-
# Display the thumbnail.
66-
plt.imshow(thumbnail)
67-
plt.axis("off")
68-
6961
# Verify the thumbnail size.
7062
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")
7173
```
7274

7375
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.
7578
1. Save the code as a file with an `.py` extension. For example, `get-thumbnail.py`.
7679
1. Open a command prompt window.
7780
1. At the prompt, use the `python` command to run the sample. For example, `python get-thumbnail.py`.

0 commit comments

Comments
 (0)